Package nMOLDYN :: Package Core :: Module Misc
[hide private]
[frames] | no frames]

Source Code for Module nMOLDYN.Core.Misc

  1  """This modules implements the functions and procedures that can not be classified anywhere else in  
  2  the library. 
  3   
  4  There should not be too much stuff here in order to not mess up the code. 
  5   
  6  Functions: 
  7      * findNestedDirectories : parses recursively a directory tree appending each nested subdirectory found in a list. 
  8      * findExecutable        : searches for an executable in OS dependant classical paths. 
  9  """ 
 10   
 11  # The python distribution modules 
 12  import glob 
 13  import os 
 14  import re 
 15  import stat 
 16  import subprocess 
 17  import sys 
 18   
19 -def findNestedDirectories(root, dirList):
20 """Parses recursively the directory tree starting from directory |rootDir| appending all the subdirectories found in 21 |dirList| list. 22 23 @param root: a string specifying the directory from which the directory tree will be generated. 24 @type root: string 25 26 @param dirList: a complete list of all the subdirectories found starting from directory |rootDir|. 27 @type dirList: list 28 29 @note: take care this is a recursive function. 30 """ 31 32 for r, d, f in os.walk(root): 33 if r not in dirList: 34 dirList.append(r)
35 36 if sys.platform == 'win32': 37 scannedDirectories = [] 38 for d in [sys.prefix, os.environ['PROGRAMFILES']]: 39 findNestedDirectories(d, scannedDirectories) 40 else: 41 scannedDirectories = [sys.prefix] 42 scannedDirectories.extend(os.environ['PATH'].split(os.pathsep)) 43
44 -def findExecutable(name):
45 """Searches for an executable in OS dependant classical paths. 46 47 @param name: a string specifying the name of the executable. 48 @type name: string 49 50 @return: a string specifying the absolute name of the executable |name] if it could be found 51 an empty string otherwise. 52 @rtype: string 53 """ 54 55 # extlist = [''] 56 # if sys.platform == 'win32': 57 # pathext = os.environ['PATHEXT'].lower().split(os.pathsep) 58 # base, ext = os.path.splitext(name) 59 # if ext.lower() not in pathext: 60 # extlist = pathext 61 if sys.platform == 'win32': 62 name += '.exe' 63 64 for directory in scannedDirectories: 65 full_execname = os.path.join(directory, name) 66 if os.path.isfile(full_execname): 67 mode = stat.S_IMODE(os.stat(full_execname)[stat.ST_MODE]) 68 if mode & 0111 > 0: 69 return full_execname 70 else: 71 return ''
72
73 -def determineNumberOfCPUs():
74 """ Number of virtual or physical CPUs on this system, i.e. 75 user/real as output by time(1) when called with an optimally scaling userspace-only program""" 76 77 # Python 2.6+ 78 try: 79 import multiprocessing 80 return multiprocessing.cpu_count() 81 except (ImportError,NotImplementedError): 82 pass 83 84 # POSIX 85 try: 86 res = int(os.sysconf('SC_NPROCESSORS_ONLN')) 87 88 if res > 0: 89 return res 90 except (AttributeError,ValueError): 91 pass 92 93 # Windows 94 try: 95 res = int(os.environ['NUMBER_OF_PROCESSORS']) 96 97 if res > 0: 98 return res 99 except (KeyError, ValueError): 100 pass 101 102 # BSD 103 try: 104 sysctl = subprocess.Popen(['sysctl', '-n', 'hw.ncpu'], stdout=subprocess.PIPE) 105 scStdout = sysctl.communicate()[0] 106 res = int(scStdout) 107 108 if res > 0: 109 return res 110 except (OSError, ValueError): 111 pass 112 113 # Linux 114 try: 115 res = file('/proc/cpuinfo','r').read().count('processor\t:') 116 117 if res > 0: 118 return res 119 except IOError: 120 pass 121 122 # Solaris 123 try: 124 pseudoDevices = os.listdir('/devices/pseudo/') 125 expr = re.compile('^cpuid@[0-9]+$') 126 127 res = 0 128 for pd in pseudoDevices: 129 if expr.match(pd) != None: 130 res += 1 131 132 if res > 0: 133 return res 134 except OSError: 135 pass 136 137 # Other UNIXes (heuristic) 138 try: 139 try: 140 dmesg = file('/var/run/dmesg.boot','r').read() 141 except IOError: 142 dmesgProcess = subprocess.Popen(['dmesg'], stdout=subprocess.PIPE) 143 dmesg = dmesgProcess.communicate()[0] 144 145 res = 0 146 while '\ncpu' + str(res) + ':' in dmesg: 147 res += 1 148 149 if res > 0: 150 return res 151 except OSError: 152 pass 153 154 raise Exception('Can not determine number of CPUs on this system')
155
156 -def cpuInfo():
157 """Retrieves the total numbers of processors, the number of loaded and free processors on the host 158 machine. 159 160 @return: the total number of processors on the host machine. 161 @rtype: integer 162 163 @return: the number of free processors on the host machine. 164 @rtype: integer 165 166 @return: the number of total processors on the host machine. 167 @rtype: integer 168 """ 169 170 if sys.platform == 'win32': 171 from win32com.client import GetObject 172 173 nProcs = 0 174 loadAvg = 0.0 175 176 wmi = GetObject('winmgmts:') 177 cpu = wmi.InstancesOf('Win32_Processor') 178 for c in cpu: 179 nProcs += c.Properties_('NumberOfCores').Value 180 loadAvg += c.Properties_('LoadPercentage').Value 181 loadAvg /= nProcs 182 183 nLoadedProcs = int(nProcs * loadAvg / 100.0) 184 185 else: 186 p1 = subprocess.Popen(['netstat', '-an'], stdout = subprocess.PIPE) 187 p2 = subprocess.Popen(['grep', ':161'], stdin = p1.stdout, stdout = subprocess.PIPE) 188 output = p2.communicate()[0].strip() 189 if output: 190 p1 = subprocess.Popen(['snmpwalk', '-v', '2c', '-c', 'public', '%s' % m, 'hrProcessorTable'], stdout = subprocess.PIPE) 191 p2 = subprocess.Popen(['wc', '-l'], stdin = p1.stdout, stdot = subprocess.PIPE) 192 nProcs = int(p2.communicate()[0].strip()) 193 194 p1 = subprocess.Popen(['snmpget', '-v', '2c', '-c', 'public', '%s' % m, 'laLoad.2'], stdout = subprocess.PIPE) 195 output2 = p1.communicate()[0].strip() 196 loadavg = float(output2.split('=')[-1].strip()) 197 else: 198 if sys.platform == 'darwin': 199 try: 200 nProcs = int(subprocess.Popen('sysctl -n hw.ncpu', stdout = subprocess.PIPE).stdout.read()) 201 except: 202 nProcs = 1 203 204 else: 205 nProcs = file('/proc/cpuinfo','r').read().count('processor\t:') 206 207 nLoadedProcs = min(nProcs,int(os.getloadavg()[1] + 0.5)) 208 209 nFreeProcs = max(0,nProcs - nLoadedProcs) 210 211 return nProcs, nLoadedProcs, nFreeProcs
212