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

Source Code for Module PyFoam.FoamInformation

  1  #  ICE Revision: $Id: FoamInformation.py 10943 2009-10-09 07:31:48Z 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 foamVersion():
44 """@return: tuple that represents the Foam-version as found 45 in $WM_PROJECT_VERSION""" 46 47 if not environ.has_key("WM_PROJECT_VERSION"): 48 return () 49 else: 50 vStr=environ["WM_PROJECT_VERSION"] 51 res=[] 52 53 for el in vStr.split("."): 54 for e in el.split("-"): 55 try: 56 res.append(int(e)) 57 except: 58 res.append(e) 59 60 return tuple(res)
61
62 -def foamVersionNumber():
63 """@return: tuple that represents the Foam-Version-Number (without 64 strings""" 65 66 ver=foamVersion() 67 68 nr=[] 69 70 for e in ver: 71 if type(e)==int: 72 nr.append(e) 73 else: 74 break 75 76 return tuple(nr)
77
78 -def oldAppConvention():
79 """Returns true if the version of OpenFOAM is older than 1.5 and 80 it therefor uses the 'old' convention to call utilities ("dot, case") 81 """ 82 return foamVersionNumber()<(1,5)
83
84 -def oldTutorialStructure():
85 """Returns true if the version of OpenFOAM is older than 1.6 and 86 it therefor uses the 'old' (flat) structure for the tutorials 87 """ 88 return foamVersionNumber()<(1,6)
89
90 -def foamInstalledVersions():
91 """@return: A list with the installed versions of OpenFOAM""" 92 93 versions=[] 94 95 valid=re.compile("^OpenFOAM-([0-9]\.[0-9].*)$") 96 97 if environ.has_key("WM_PROJECT_INST_DIR"): 98 basedir=environ["WM_PROJECT_INST_DIR"] 99 else: 100 basedir=path.expanduser("~/OpenFOAM") 101 102 for f in listdir(basedir): 103 m=valid.match(f) 104 if m: 105 dname=path.join(basedir,f) 106 if path.isdir(dname): 107 name=m.groups(1)[0] 108 dotDir=path.join(dname,".OpenFOAM-"+name) 109 etcDir=path.join(dname,"etc") 110 if path.isdir(etcDir) and path.exists(path.join(etcDir,"bashrc")): 111 versions.append(m.groups(1)[0]) 112 elif path.isdir(dotDir) and path.exists(path.join(dotDir,"bashrc")): 113 versions.append(m.groups(1)[0]) 114 115 return versions
116
117 -def changeFoamVersion(new,force64=False,force32=False,compileOption=None):
118 """Changes the used FoamVersion. Only valid during the runtime of 119 the interpreter (the script or the Python session) 120 @param new: The new Version 121 @param force64: Forces the 64-bit-version to be chosen 122 @param force32: Forces the 32-bit-version to be chosen 123 @param compileOption: Forces Debug or Opt""" 124 125 if not new in foamInstalledVersions(): 126 error("Version",new,"is not an installed version: ",foamInstalledVersions()) 127 128 if environ.has_key("WM_PROJECT_VERSION"): 129 if new==environ["WM_PROJECT_VERSION"]: 130 warning(new,"is already being used") 131 else: 132 warning("No OpenFOAM-Version installed") 133 134 if environ.has_key("WM_PROJECT_INST_DIR"): 135 basedir=environ["WM_PROJECT_INST_DIR"] 136 else: 137 basedir=path.expanduser(config().get("OpenFOAM","Installation")) 138 139 if path.exists(path.join(basedir,"OpenFOAM-"+new,"etc")): 140 script=path.join(basedir,"OpenFOAM-"+new,"etc","bashrc") 141 else: 142 script=path.join(basedir,"OpenFOAM-"+new,".OpenFOAM-"+new,"bashrc") 143 144 forceArchOption=None 145 if force64: 146 forceArchOption="64" 147 elif force32: 148 forceArchOption="32" 149 150 injectVariables(script, 151 forceArchOption=forceArchOption, 152 compileOption=compileOption) 153 154 if new!=environ["WM_PROJECT_VERSION"]: 155 error("Problem while changing to version",new,"old version still used:",environ["WM_PROJECT_VERSION"])
156
157 -def injectVariables(script,forceArchOption=None,compileOption=None):
158 """Executes a script in a subshell and changes the current 159 environment with the enivironment after the execution 160 @param script: the script that is executed 161 @param forceArchOption: To which architecture Option should be forced 162 @param compileOption: to which value the WM_COMPILE_OPTION should be forced""" 163 164 if not path.exists(script): 165 error("Can not execute",script,"it does not exist") 166 167 try: 168 if environ.has_key("SHELL"): 169 shell=environ["SHELL"] 170 171 if(path.basename(shell)=="python"): 172 # this assumes that the 'shell' is a PyFoam-Script on a cluster 173 shell=config().get("Paths","bash") 174 environ["SHELL"]=shell 175 176 allowedShells = [ "bash", "zsh"] 177 if not path.basename(shell) in allowedShells: 178 error("Currently only implemented for the shells",allowedShells,", not for",shell) 179 180 cmd="" 181 if forceArchOption!=None: 182 cmd+="export WM_ARCH_OPTION="+forceArchOption+"; " 183 if compileOption!=None: 184 cmd+="export WM_COMPILE_OPTION="+compileOption+"; " 185 cmd+=". "+script+'; echo "Starting The Dump Of Variables"; export' 186 except KeyError,name: 187 error("Can't do it, because shell variable",name,"is undefined") 188 189 if sys.version_info<(2,6): 190 raus,rein = popen4(cmd) 191 else: 192 p = Popen(cmd, shell=True, 193 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 194 (rein,raus)=(p.stdin,p.stdout) 195 196 lines=raus.readlines() 197 rein.close() 198 raus.close() 199 200 exp=re.compile('export (.+)="(.*)"\n') 201 202 cnt=0 203 204 for l in lines: 205 m=exp.match(l) 206 if m: 207 cnt+=1 208 environ[m.groups()[0]]=m.groups()[1]
209