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

Source Code for Module PyFoam.FoamInformation

  1  #  ICE Revision: $Id: FoamInformation.py 9158 2008-08-04 08:00:55Z bgschaid $  
  2  """Getting Information about the Foam-Installation (like the installation directory)""" 
  3   
  4  from os import environ,path,listdir 
  5  from popen2 import popen4 
  6   
  7  import re 
  8   
  9  from Error import error,warning 
 10   
 11  from PyFoam import configuration as config 
 12   
13 -def getPathFromEnviron(name):
14 """Gets a path from an environment variable 15 @return: the path 16 @rtype: string 17 @param name: the name of the environment variable""" 18 19 tmp="" 20 if environ.has_key(name): 21 tmp=path.normpath(environ[name]) 22 23 return tmp
24
25 -def foamTutorials():
26 """@return: directory in which the tutorials reside""" 27 28 return getPathFromEnviron("FOAM_TUTORIALS")
29
30 -def foamMPI():
31 """@return the used MPI-Implementation""" 32 if not environ.has_key("WM_MPLIB"): 33 return () 34 else: 35 vStr=environ["WM_MPLIB"] 36 return vStr
37
38 -def foamVersion():
39 """@return: tuple that represents the Foam-version as found 40 in $WM_PROJECT_VERSION""" 41 42 if not environ.has_key("WM_PROJECT_VERSION"): 43 return () 44 else: 45 vStr=environ["WM_PROJECT_VERSION"] 46 res=[] 47 48 for el in vStr.split("."): 49 for e in el.split("-"): 50 try: 51 res.append(int(e)) 52 except: 53 res.append(e) 54 55 return tuple(res)
56
57 -def foamVersionNumber():
58 """@return: tuple that represents the Foam-Version-Number (without 59 strings""" 60 61 ver=foamVersion() 62 63 nr=[] 64 65 for e in ver: 66 if type(e)==int: 67 nr.append(e) 68 else: 69 break 70 71 return tuple(nr)
72
73 -def oldAppConvention():
74 """Returns true if the version of OpenFOAM is older than 1.5 and 75 it therefor uses the 'old' convention to call utilities ("dot, case") 76 """ 77 return foamVersionNumber()<(1,5)
78
79 -def foamInstalledVersions():
80 """@return: A list with the installed versions of OpenFOAM""" 81 82 versions=[] 83 84 valid=re.compile("^OpenFOAM-([0-9]\.[0-9].*)$") 85 86 if environ.has_key("WM_PROJECT_INST_DIR"): 87 basedir=environ["WM_PROJECT_INST_DIR"] 88 else: 89 basedir=path.expanduser("~/OpenFOAM") 90 91 for f in listdir(basedir): 92 m=valid.match(f) 93 if m: 94 dname=path.join(basedir,f) 95 if path.isdir(dname): 96 name=m.groups(1)[0] 97 dotDir=path.join(dname,".OpenFOAM-"+name) 98 etcDir=path.join(dname,"etc") 99 if path.isdir(etcDir) and path.exists(path.join(etcDir,"bashrc")): 100 versions.append(m.groups(1)[0]) 101 elif path.isdir(dotDir) and path.exists(path.join(dotDir,"bashrc")): 102 versions.append(m.groups(1)[0]) 103 104 return versions
105
106 -def changeFoamVersion(new):
107 """Changes the used FoamVersion. Only valid during the runtime of 108 the interpreter (the script or the Python session) 109 @param new: The new Version""" 110 111 if not new in foamInstalledVersions(): 112 error("Version",new,"is not an installed version: ",foamInstalledVersions()) 113 114 if environ.has_key("WM_PROJECT_VERSION"): 115 if new==environ["WM_PROJECT_VERSION"]: 116 warning(new,"is already being used") 117 return 118 else: 119 warning("No OpenFOAM-Version installed") 120 121 if environ.has_key("WM_PROJECT_INST_DIR"): 122 basedir=environ["WM_PROJECT_INST_DIR"] 123 else: 124 basedir=path.expanduser(config().get("OpenFOAM","Installation")) 125 126 if path.exists(path.join(basedir,"OpenFOAM-"+new,"etc")): 127 script=path.join(basedir,"OpenFOAM-"+new,"etc","bashrc") 128 else: 129 script=path.join(basedir,"OpenFOAM-"+new,".OpenFOAM-"+new,"bashrc") 130 131 injectVariables(script) 132 133 if new!=environ["WM_PROJECT_VERSION"]: 134 error("Problem while changing to version",new,"old version still used:",environ["WM_PROJECT_VERSION"])
135
136 -def injectVariables(script):
137 """Executes a script in a subshell and changes the current 138 environment with the enivironment after the execution 139 @param script: the script that is executed""" 140 141 if not path.exists(script): 142 error("Can not execute",script,"it does not exist") 143 144 try: 145 if environ.has_key("SHELL"): 146 shell=environ["SHELL"] 147 148 if(path.basename(shell)=="python"): 149 # this assumes that the 'shell' is a PyFoam-Script on a cluster 150 shell=config().get("Paths","bash") 151 environ["SHELL"]=shell 152 153 if(path.basename(shell)!="bash"): 154 error("Currently only implemented for bash-shell, not for",shell) 155 156 cmd=". "+script+'; echo "Starting The Dump Of Variables"; export' 157 except KeyError,name: 158 error("Can't do it, because shell variable",name,"is undefined") 159 160 raus,rein = popen4(cmd) 161 lines=raus.readlines() 162 rein.close() 163 raus.close() 164 165 exp=re.compile('export (.+)="(.*)"\n') 166 167 cnt=0 168 169 for l in lines: 170 m=exp.match(l) 171 if m: 172 cnt+=1 173 environ[m.groups()[0]]=m.groups()[1]
174