Package mbuild :: Module base
[frames] | no frames]

Source Code for Module mbuild.base

  1  #!/usr/bin/env python 
  2  # -*- python -*- 
  3  # Mark Charney  
  4  #BEGIN_LEGAL 
  5  # 
  6  #Copyright (c) 2016 Intel Corporation 
  7  # 
  8  #  Licensed under the Apache License, Version 2.0 (the "License"); 
  9  #  you may not use this file except in compliance with the License. 
 10  #  You may obtain a copy of the License at 
 11  # 
 12  #      http://www.apache.org/licenses/LICENSE-2.0 
 13  # 
 14  #  Unless required by applicable law or agreed to in writing, software 
 15  #  distributed under the License is distributed on an "AS IS" BASIS, 
 16  #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 17  #  See the License for the specific language governing permissions and 
 18  #  limitations under the License. 
 19  #   
 20  #END_LEGAL 
 21  """Base functionality: messages, verbosity, python version checking""" 
 22   
 23  import os 
 24  import sys 
 25  import traceback 
 26   
 27  _mbuild_verbose_level = 1 
28 -def verbose(level=0):
29 """Return True if the configured message level supplied is >= the 30 level arguement 31 @param level: int 32 @param level: the verbosity level at which this function should return True 33 34 @rtype: bool 35 @return: True iff the level argument is >= current verbosity level 36 """ 37 global _mbuild_verbose_level 38 if _mbuild_verbose_level >= level: 39 return True 40 return False
41 -def set_verbosity(v):
42 """Set the global verbosity level. 0=quiet, 99=very very noisy""" 43 global _mbuild_verbose_level 44 _mbuild_verbose_level = v
45
46 -def get_verbosity():
47 """Return the global verbosity level. 0=quiet, 99=very very noisy""" 48 global _mbuild_verbose_level 49 return _mbuild_verbose_level
50
51 -def bracket(s,m=''):
52 """add a bracket around s and append m. 53 @rtype: string 54 @return: a bracketed string s and a suffixed message m 55 """ 56 return '[%s] %s' % (s,str(m))
57
58 -def error_msg(s,t):
59 """Emit '[s] t' to stderr with a newline""" 60 sys.stderr.write(bracket(s,t) + "\n")
61
62 -def msg(s, pad=''):
63 """Emit s to stdout with a newline""" 64 sys.stdout.write(pad) 65 sys.stdout.write(s) 66 sys.stdout.write("\n")
67
68 -def msgn(s, pad=''):
69 """Emit s to stdout without a newline""" 70 sys.stdout.write(pad) 71 sys.stdout.write(s)
72
73 -def msgb(s,t='',pad=''):
74 """a bracketed string s sent to stdout, followed by a string t""" 75 msg(bracket(s,t), pad=pad)
76
77 -def vmsgb(v,s,t='',pad=''):
78 """If verbosity v is sufficient, emit a bracketed string s sent to 79 stdout, followed by a string t""" 80 if verbose(v): 81 msg(bracket(s,t),pad=pad)
82
83 -def cond_die(v, cmd, msg):
84 """Conditionally die, if v is not zero. Print the msg and the cmd. 85 @type v: int 86 @param v: we die if v is not 0 87 88 @type cmd: string 89 @param cmd: a command to print 90 91 @type msg: string 92 @param msg: a message to print before the command 93 """ 94 if v != 0: 95 s = msg + "\n [CMD] " + cmd 96 die(s)
97
98 -def die(m,s=''):
99 """Emit an error message m (and optionally s) and exit with a return value 1""" 100 msgb("MBUILD ERROR", "%s %s\n\n" % (m,s) ) 101 traceback.print_exc(file=sys.stdout) 102 sys.exit(1)
103 -def warn(m):
104 """Emit an warning message""" 105 msgb("MBUILD WARNING", m)
106
107 -def get_python_version():
108 """Return the python version as an integer 109 @rtype: int 110 @return: major * 100000 + minor + 1000 + fixlevel 111 """ 112 tuple = sys.version_info 113 major = int(tuple[0]) 114 minor = int(tuple[1]) 115 fix = int(tuple[2]) 116 vnum = major *100000 + minor * 1000 + fix 117 return vnum
118
119 -def get_python_version_tuple():
120 """Return the python version as a tuple (major,minor,fixlevel) 121 @rtype: tuple 122 @return: (major,minor,fixlevel) 123 """ 124 125 tuple = sys.version_info 126 major = int(tuple[0]) 127 minor = int(tuple[1]) 128 fix = int(tuple[2]) 129 return (major,minor,fix)
130
131 -def check_python_version(maj,minor,fix=0):
132 """Return true if the current python version at least the one 133 specified by the arguments. 134 @rtype: bool 135 @return: True/False 136 """ 137 t = get_python_version_tuple() 138 if t[0] > maj: 139 return True 140 if t[0] == maj and t[1] > minor: 141 return True 142 if t[0] == maj and t[1] == minor and t[2] >= fix: 143 return True 144 return False
145 146 147 148 try: 149 if check_python_version(2,4) == False: 150 die("MBUILD error: Need Python version 2.4 or later.") 151 except: 152 die("MBUILD error: Need Python version 2.4 or later.") 153 154 import platform # requires python 2.3 155 _on_mac = False 156 _on_native_windows = False 157 _on_windows = False # cygwin or native windows 158 _on_cygwin = False 159 _on_linux = False 160 _on_freebsd = False 161 _operating_system_name = platform.system() 162 if _operating_system_name.find('CYGWIN') != -1: 163 _on_cygwin = True 164 _on_windows = True 165 elif _operating_system_name == 'Microsoft' or _operating_system_name == 'Windows': 166 _on_native_windows = True 167 _on_windows = True 168 elif _operating_system_name == 'Linux': 169 _on_linux = True 170 elif _operating_system_name == 'FreeBSD': 171 _on_freebsd = True 172 elif _operating_system_name == 'Darwin': 173 _on_mac = True 174 else: 175 die("Could not detect operating system type: " + _operating_system_name) 176
177 -def on_native_windows():
178 """ 179 @rtype: bool 180 @return: True iff on native windows win32/win64 181 """ 182 global _on_native_windows 183 return _on_native_windows
184
185 -def on_windows():
186 """ 187 @rtype: bool 188 @return: True iff on windows cygwin/win32/win64 189 """ 190 global _on_windows 191 return _on_windows 192