1
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
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
31 """@return: directory in which the tutorials reside"""
32
33 return getPathFromEnviron("FOAM_TUTORIALS")
34
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
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
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
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
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
109 """@return: A list with the installed versions of OpenFOAM"""
110
111 versions=[]
112
113 valid=re.compile("^OpenFOAM-([0-9]\.[0-9].*)$")
114
115 if environ.has_key("WM_PROJECT_INST_DIR"):
116 basedir=environ["WM_PROJECT_INST_DIR"]
117 else:
118 basedir=path.expanduser(config().get("OpenFOAM","Installation"))
119
120 if not path.exists(basedir) or not path.isdir(basedir):
121 warning("Basedir",basedir,"does not exist or is not a directory")
122 return []
123
124 for f in listdir(basedir):
125 m=valid.match(f)
126 if m:
127 dname=path.join(basedir,f)
128 if path.isdir(dname):
129 name=m.groups(1)[0]
130 dotDir=path.join(dname,".OpenFOAM-"+name)
131 etcDir=path.join(dname,"etc")
132 if path.isdir(etcDir) and path.exists(path.join(etcDir,"bashrc")):
133 versions.append(m.groups(1)[0])
134 elif path.isdir(dotDir) and path.exists(path.join(dotDir,"bashrc")):
135 versions.append(m.groups(1)[0])
136
137 return versions
138
140 """Changes the used FoamVersion. Only valid during the runtime of
141 the interpreter (the script or the Python session)
142 @param new: The new Version
143 @param force64: Forces the 64-bit-version to be chosen
144 @param force32: Forces the 32-bit-version to be chosen
145 @param compileOption: Forces Debug or Opt"""
146
147 if not new in foamInstalledVersions():
148 error("Version",new,"is not an installed version: ",foamInstalledVersions())
149
150 if environ.has_key("WM_PROJECT_VERSION"):
151 if new==environ["WM_PROJECT_VERSION"]:
152 warning(new,"is already being used")
153 else:
154 warning("No OpenFOAM-Version installed")
155
156 if environ.has_key("WM_PROJECT_INST_DIR"):
157 basedir=environ["WM_PROJECT_INST_DIR"]
158 else:
159 basedir=path.expanduser(config().get("OpenFOAM","Installation"))
160
161 if path.exists(path.join(basedir,"OpenFOAM-"+new,"etc")):
162 script=path.join(basedir,"OpenFOAM-"+new,"etc","bashrc")
163 else:
164 script=path.join(basedir,"OpenFOAM-"+new,".OpenFOAM-"+new,"bashrc")
165
166 forceArchOption=None
167 if force64:
168 forceArchOption="64"
169 elif force32:
170 forceArchOption="32"
171
172 injectVariables(script,
173 forceArchOption=forceArchOption,
174 compileOption=compileOption)
175
176 if new!=environ["WM_PROJECT_VERSION"]:
177 error("Problem while changing to version",new,"old version still used:",environ["WM_PROJECT_VERSION"])
178
180 """Executes a script in a subshell and changes the current
181 environment with the enivironment after the execution
182 @param script: the script that is executed
183 @param forceArchOption: To which architecture Option should be forced
184 @param compileOption: to which value the WM_COMPILE_OPTION should be forced"""
185
186 if not path.exists(script):
187 error("Can not execute",script,"it does not exist")
188
189 try:
190 if environ.has_key("SHELL"):
191 shell=environ["SHELL"]
192
193 if(path.basename(shell).find("python")==0):
194
195 shell=config().get("Paths","bash")
196 environ["SHELL"]=shell
197
198 allowedShells = [ "bash", "zsh"]
199 if not path.basename(shell) in allowedShells:
200 error("Currently only implemented for the shells",allowedShells,", not for",shell)
201
202 cmd=""
203 if forceArchOption!=None:
204 cmd+="export WM_ARCH_OPTION="+forceArchOption+"; "
205 if compileOption!=None:
206 cmd+="export WM_COMPILE_OPTION="+compileOption+"; "
207 cmd+=". "+script+'; echo "Starting The Dump Of Variables"; export'
208 except KeyError,name:
209 error("Can't do it, because shell variable",name,"is undefined")
210
211 if sys.version_info<(2,6):
212 raus,rein = popen4(cmd)
213 else:
214 p = Popen(cmd, shell=True,
215 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
216 (rein,raus)=(p.stdin,p.stdout)
217
218 lines=raus.readlines()
219 rein.close()
220 raus.close()
221
222 exp=re.compile('export (.+)="(.*)"\n')
223
224 cnt=0
225
226 for l in lines:
227 m=exp.match(l)
228 if m:
229 cnt+=1
230 environ[m.groups()[0]]=m.groups()[1]
231