Package PyFoam :: Module FoamInformation
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.FoamInformation

  1  #  ICE Revision: $Id$  
  2  """Getting Information about the Foam-Installation (like the installation directory)""" 
  3   
  4  from os import environ,path,listdir 
  5  import sys 
  6   
  7  if sys.version_info<(2,6): 
  8      from popen2 import popen4 
  9  else: 
 10      from subprocess import Popen,PIPE,STDOUT 
 11   
 12  import re 
 13   
 14  from PyFoam.Error import error,warning 
 15   
 16  from PyFoam import configuration as config 
 17   
18 -def getPathFromEnviron(name):
19 """Gets a path from an environment variable 20 @return: the path 21 @rtype: string 22 @param name: the name of the environment variable""" 23 24 tmp="" 25 if name in environ: 26 tmp=path.normpath(environ[name]) 27 28 return tmp
29
30 -def foamTutorials():
31 """@return: directory in which the tutorials reside""" 32 33 return getPathFromEnviron("FOAM_TUTORIALS")
34
35 -def foamMPI():
36 """@return: the used MPI-Implementation""" 37 if "WM_MPLIB" not in environ: 38 return () 39 else: 40 vStr=environ["WM_MPLIB"] 41 return vStr
42
43 -def foamVersionString(useConfigurationIfNoInstallation=False):
44 """@return: string for the Foam-version as found 45 in $WM_PROJECT_VERSION""" 46 47 if "WM_PROJECT_VERSION" not in environ and not useConfigurationIfNoInstallation: 48 return "" 49 else: 50 if "WM_PROJECT_VERSION" in environ: 51 vStr=environ["WM_PROJECT_VERSION"] 52 else: 53 vStr="" 54 55 if vStr=="" and useConfigurationIfNoInstallation: 56 vStr=config().get("OpenFOAM","Version") 57 58 return vStr
59
60 -def foamVersion(useConfigurationIfNoInstallation=False):
61 """@return: tuple that represents the Foam-version as found 62 in $WM_PROJECT_VERSION""" 63 64 vStr=foamVersionString(useConfigurationIfNoInstallation=useConfigurationIfNoInstallation) 65 66 if vStr=="": 67 return () 68 else: 69 res=[] 70 71 for el in vStr.split("."): 72 for e in el.split("-"): 73 try: 74 res.append(int(e)) 75 except: 76 res.append(e) 77 78 return tuple(res)
79
80 -def foamVersionNumber(useConfigurationIfNoInstallation=False):
81 """@return: tuple that represents the Foam-Version-Number (without 82 strings""" 83 84 ver=foamVersion(useConfigurationIfNoInstallation=useConfigurationIfNoInstallation) 85 86 nr=[] 87 88 for e in ver: 89 if type(e)==int: 90 nr.append(e) 91 else: 92 break 93 94 return tuple(nr)
95
96 -def oldAppConvention():
97 """Returns true if the version of OpenFOAM is older than 1.5 and 98 it therefor uses the 'old' convention to call utilities ("dot, case") 99 """ 100 return foamVersionNumber()<(1,5)
101
102 -def oldTutorialStructure():
103 """Returns true if the version of OpenFOAM is older than 1.6 and 104 it therefor uses the 'old' (flat) structure for the tutorials 105 """ 106 return foamVersionNumber()<(1,6)
107
108 -def findInstalledVersions(basedir,valid):
109 versions=set() 110 111 try: 112 candidates=listdir(basedir) 113 except OSError: 114 return versions 115 116 for f in candidates: 117 m=valid.match(f) 118 if m: 119 dname=path.join(basedir,f) 120 if path.isdir(dname): 121 name=m.groups(1)[0] 122 dotDir=path.join(dname,".OpenFOAM-"+name) 123 etcDir=path.join(dname,"etc") 124 if path.isdir(etcDir) and path.exists(path.join(etcDir,"bashrc")): 125 versions.add(m.groups(1)[0]) 126 elif path.isdir(dotDir) and path.exists(path.join(dotDir,"bashrc")): 127 versions.add(m.groups(1)[0]) 128 129 return versions
130
131 -def findBaseDir(newDir):
132 if "WM_PROJECT_INST_DIR" in environ: 133 basedir=environ["WM_PROJECT_INST_DIR"] 134 else: 135 basedir=path.expanduser(config().get("OpenFOAM","Installation")) 136 137 for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation"): 138 if path.exists(path.join(bdir,"OpenFOAM-"+newDir)): 139 return (bdir,"OpenFOAM-") 140 elif path.exists(path.join(bdir,"openfoam"+newDir)): 141 return (bdir,"openfoam") 142 143 error("Can't find basedir for OpenFOAM-version",newDir)
144
145 -def foamInstalledVersions():
146 """@return: A list with the installed versions of OpenFOAM""" 147 148 versions=set() 149 150 valid=re.compile("^OpenFOAM-([0-9]\.[0-9].*)$") 151 valid2=re.compile("^openfoam([0-9]+)$") 152 153 if "WM_PROJECT_INST_DIR" in environ: 154 basedir=environ["WM_PROJECT_INST_DIR"] 155 else: 156 basedir=path.expanduser(config().get("OpenFOAM","Installation")) 157 158 if not path.exists(basedir) or not path.isdir(basedir): 159 warning("Basedir",basedir,"does not exist or is not a directory") 160 return [] 161 162 for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation"): 163 for val in [valid,valid2]: 164 versions |= findInstalledVersions(bdir,val) 165 166 return list(versions)
167
168 -def changeFoamVersion(new,force64=False,force32=False,compileOption=None):
169 """Changes the used FoamVersion. Only valid during the runtime of 170 the interpreter (the script or the Python session) 171 @param new: The new Version 172 @param force64: Forces the 64-bit-version to be chosen 173 @param force32: Forces the 32-bit-version to be chosen 174 @param compileOption: Forces Debug or Opt""" 175 176 if not new in foamInstalledVersions(): 177 error("Version",new,"is not an installed version: ",foamInstalledVersions()) 178 179 old=None 180 if "WM_PROJECT_VERSION" in environ: 181 old=environ["WM_PROJECT_VERSION"] 182 if new==old: 183 warning(new,"is already being used") 184 else: 185 warning("No OpenFOAM-Version installed") 186 187 basedir,prefix=findBaseDir(new) 188 189 if path.exists(path.join(basedir,prefix+new,"etc")): 190 script=path.join(basedir,prefix+new,"etc","bashrc") 191 else: 192 script=path.join(basedir,prefix+new,".OpenFOAM-"+new,"bashrc") 193 194 forceArchOption=None 195 if force64: 196 forceArchOption="64" 197 elif force32: 198 forceArchOption="32" 199 200 injectVariables(script, 201 forceArchOption=forceArchOption, 202 compileOption=compileOption) 203 204 try: 205 if old==environ["WM_PROJECT_VERSION"]: 206 warning("Problem while changing to version",new,"old version still used:",environ["WM_PROJECT_VERSION"]) 207 except KeyError: 208 pass
209
210 -def injectVariables(script,forceArchOption=None,compileOption=None):
211 """Executes a script in a subshell and changes the current 212 environment with the enivironment after the execution 213 @param script: the script that is executed 214 @param forceArchOption: To which architecture Option should be forced 215 @param compileOption: to which value the WM_COMPILE_OPTION should be forced""" 216 217 # Certan bashrc-s fail if these are set 218 for v in ["FOAM_INST_DIR", 219 "WM_THIRD_PARTY_DIR", 220 "WM_PROJECT_USER_DIR", 221 "OPAL_PREFIX"]: 222 try: 223 del environ[v] 224 except KeyError: 225 pass 226 227 if not path.exists(script): 228 error("Can not execute",script,"it does not exist") 229 230 try: 231 if "SHELL" in environ: 232 shell=environ["SHELL"] 233 234 if(path.basename(shell).find("python")==0): 235 # this assumes that the 'shell' is a PyFoam-Script on a cluster 236 shell=config().get("Paths","bash") 237 environ["SHELL"]=shell 238 239 allowedShells = [ "bash", "zsh"] 240 if not path.basename(shell) in allowedShells: 241 error("Currently only implemented for the shells",allowedShells,", not for",shell) 242 243 cmd="" 244 postCmd="" 245 if forceArchOption!=None: 246 cmd+="export WM_ARCH_OPTION="+forceArchOption+"; " 247 postCmd+=" WM_ARCH_OPTION="+forceArchOption 248 if compileOption!=None: 249 cmd+="export WM_COMPILE_OPTION="+compileOption+"; " 250 postCmd+=" WM_COMPILE_OPTION="+compileOption 251 cmd+=". "+script+postCmd+'; echo "Starting The Dump Of Variables"; export' 252 except KeyError: 253 # Instead of 'KeyError as name'. This is compatible with 2.4-3.2 254 # http://docs.pythonsprints.com/python3_porting/py-porting.html#handling-exceptions 255 name = sys.exc_info()[1] 256 error("Can't do it, because shell variable",name,"is undefined") 257 258 if sys.version_info<(2,6): 259 raus,rein = popen4(cmd) 260 else: 261 p = Popen(cmd, shell=True, 262 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 263 (rein,raus)=(p.stdin,p.stdout) 264 265 lines=raus.readlines() 266 rein.close() 267 raus.close() 268 269 # clumsy but avoids more complicated expressions 270 exp=re.compile('export (.+)="(.*)"\n') 271 exp2=re.compile("export (.+)='(.*)'\n") 272 273 cnt=0 274 275 for l in lines: 276 m=exp.match(str(l)) 277 if not m: 278 m=exp2.match(str(l)) 279 if m: 280 cnt+=1 281 environ[m.groups()[0]]=m.groups()[1]
282 283 # Should work with Python3 and Python2 284