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

Source Code for Module PyFoam.FoamInformation

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/FoamInformation.py 7870 2012-02-15T17:53:40.344304Z bgschaid  $  
  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 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 environ.has_key(name): 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 not environ.has_key("WM_MPLIB"): 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 not environ.has_key("WM_PROJECT_VERSION") and not useConfigurationIfNoInstallation: 48 return "" 49 else: 50 if environ.has_key("WM_PROJECT_VERSION"): 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 environ.has_key("WM_PROJECT_INST_DIR"): 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(1[0-9]+)$") 152 153 if environ.has_key("WM_PROJECT_INST_DIR"): 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 environ.has_key("WM_PROJECT_VERSION"): 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 try: 218 # Certan bashrc-s fail if this is set 219 del environ["FOAM_INST_DIR"] 220 except KeyError: 221 pass 222 223 if not path.exists(script): 224 error("Can not execute",script,"it does not exist") 225 226 try: 227 if environ.has_key("SHELL"): 228 shell=environ["SHELL"] 229 230 if(path.basename(shell).find("python")==0): 231 # this assumes that the 'shell' is a PyFoam-Script on a cluster 232 shell=config().get("Paths","bash") 233 environ["SHELL"]=shell 234 235 allowedShells = [ "bash", "zsh"] 236 if not path.basename(shell) in allowedShells: 237 error("Currently only implemented for the shells",allowedShells,", not for",shell) 238 239 cmd="" 240 postCmd="" 241 if forceArchOption!=None: 242 cmd+="export WM_ARCH_OPTION="+forceArchOption+"; " 243 postCmd+=" WM_ARCH_OPTION="+forceArchOption 244 if compileOption!=None: 245 cmd+="export WM_COMPILE_OPTION="+compileOption+"; " 246 postCmd+=" WM_COMPILE_OPTION="+compileOption 247 cmd+=". "+script+postCmd+'; echo "Starting The Dump Of Variables"; export' 248 except KeyError,name: 249 error("Can't do it, because shell variable",name,"is undefined") 250 251 if sys.version_info<(2,6): 252 raus,rein = popen4(cmd) 253 else: 254 p = Popen(cmd, shell=True, 255 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 256 (rein,raus)=(p.stdin,p.stdout) 257 258 lines=raus.readlines() 259 rein.close() 260 raus.close() 261 262 # clumsy but avoids more complicated expressions 263 exp=re.compile('export (.+)="(.*)"\n') 264 exp2=re.compile("export (.+)='(.*)'\n") 265 266 cnt=0 267 268 for l in lines: 269 m=exp.match(l) 270 if not m: 271 m=exp2.match(l) 272 if m: 273 cnt+=1 274 environ[m.groups()[0]]=m.groups()[1]
275