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

Source Code for Module PyFoam.FoamInformation

  1  #  ICE Revision: $Id: FoamInformation.py 8713 2008-04-10 07:48:32Z 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 foamInstalledVersions():
74 """@return: A list with the installed versions of OpenFOAM""" 75 76 versions=[] 77 78 valid=re.compile("^OpenFOAM-([0-9]\.[0-9].*)$") 79 80 if environ.has_key("WM_PROJECT_INST_DIR"): 81 basedir=environ["WM_PROJECT_INST_DIR"] 82 else: 83 basedir=path.expanduser("~/OpenFOAM") 84 85 for f in listdir(basedir): 86 m=valid.match(f) 87 if m: 88 dname=path.join(basedir,f) 89 if path.isdir(dname): 90 name=m.groups(1)[0] 91 dotDir=path.join(dname,".OpenFOAM-"+name) 92 if path.isdir(dotDir) and path.exists(path.join(dotDir,"bashrc")): 93 versions.append(m.groups(1)[0]) 94 95 return versions
96
97 -def changeFoamVersion(new):
98 """Changes the used FoamVersion. Only valid during the runtime of 99 the interpreter (the script or the Python session) 100 @param new: The new Version""" 101 102 if not new in foamInstalledVersions(): 103 error("Version",new,"is not an installed version: ",foamInstalledVersions()) 104 105 if environ.has_key("WM_PROJECT_VERSION"): 106 if new==environ["WM_PROJECT_VERSION"]: 107 warning(new,"is already being used") 108 return 109 else: 110 warning("No OpenFOAM-Version installed") 111 112 if environ.has_key("WM_PROJECT_INST_DIR"): 113 basedir=environ["WM_PROJECT_INST_DIR"] 114 else: 115 basedir=path.expanduser(config().get("OpenFOAM","Installation")) 116 117 script=path.join(basedir,"OpenFOAM-"+new,".OpenFOAM-"+new,"bashrc") 118 119 injectVariables(script) 120 121 if new!=environ["WM_PROJECT_VERSION"]: 122 error("Problem while changing to version",new,"old version still used:",environ["WM_PROJECT_VERSION"])
123
124 -def injectVariables(script):
125 """Executes a script in a subshell and changes the current 126 environment with the enivironment after the execution 127 @param script: the script that is executed""" 128 129 if not path.exists(script): 130 error("Can not execute",script,"it does not exist") 131 132 try: 133 if environ.has_key("SHELL"): 134 shell=environ["SHELL"] 135 136 if(path.basename(shell)=="python"): 137 # this assumes that the 'shell' is a PyFoam-Script on a cluster 138 shell=config().get("Paths","bash") 139 environ["SHELL"]=shell 140 141 if(path.basename(shell)!="bash"): 142 error("Currently only implemented for bash-shell, not for",shell) 143 144 cmd=". "+script+'; echo "Starting The Dump Of Variables"; export' 145 except KeyError,name: 146 error("Can't do it, because shell variable",name,"is undefined") 147 148 raus,rein = popen4(cmd) 149 lines=raus.readlines() 150 rein.close() 151 raus.close() 152 153 exp=re.compile('export (.+)="(.*)"\n') 154 155 cnt=0 156 157 for l in lines: 158 m=exp.match(l) 159 if m: 160 cnt+=1 161 environ[m.groups()[0]]=m.groups()[1]
162