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 PyFoam.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 name in environ:
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 "WM_MPLIB" not in environ:
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 "WM_PROJECT_VERSION" not in environ and not useConfigurationIfNoInstallation:
48 return ""
49 else:
50 if "WM_PROJECT_VERSION" in environ:
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 versions={}
110
111 basedir=path.abspath(basedir)
112
113 try:
114 candidates=listdir(basedir)
115 except OSError:
116 return versions
117
118 for f in candidates:
119 m=valid.match(f)
120 if m:
121 dname=path.join(basedir,f)
122 if path.isdir(dname):
123 name=m.groups(1)[0]
124 dotDir=path.join(dname,".OpenFOAM-"+name)
125 etcDir=path.join(dname,"etc")
126 if path.isdir(etcDir) and path.exists(path.join(etcDir,"bashrc")):
127 versions[(forkName,m.groups(1)[0])]=dname
128 elif path.isdir(dotDir) and path.exists(path.join(dotDir,"bashrc")):
129 versions[(forkName,m.groups(1)[0])]=dname
130
131 return versions
132
133 __foamInstallations=None
134
136 installed=foamInstalledVersions()
137 found=[]
138
139 for fork,version in installed.keys():
140 if newVersion==version:
141 found.append((fork,version))
142 elif newVersion==(fork+"-"+version):
143 found.append((fork,version))
144
145 if len(found)==0:
146 error("Can't find basedir for OpenFOAM-version",newVersion,"in",
147 ", ".join([ a[0]+"-"+a[1] for a in installed.keys() ]))
148 elif len(found)==1:
149 return found[0][0],found[0][1],installed[found[0]]
150 else:
151 error("Requested version:",newVersion,"Matches found:",
152 ", ".join([ a[0]+"-"+a[1] for a in found ]))
153
155 """@return: A list with the installed versions of OpenFOAM"""
156 global __foamInstallations
157
158 if __foamInstallations:
159 return __foamInstallations
160
161 __foamInstallations={}
162
163 "^OpenFOAM-([0-9]\.[0-9].*)$","^openfoam([0-9]+)$"
164 forks=config().getList("OpenFOAM","Forks")
165
166 for fork in forks:
167 currentFork=foamFork()
168
169 if "WM_PROJECT_INST_DIR" in environ and currentFork==fork:
170 basedir=environ["WM_PROJECT_INST_DIR"]
171 else:
172 basedir=path.expanduser(config().get("OpenFOAM","Installation-"+fork))
173
174 if not path.exists(basedir) or not path.isdir(basedir):
175 warning("Basedir",basedir,"for fork",fork,"does not exist or is not a directory")
176 continue
177
178 for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation"):
179 for val in [re.compile(s) for s in config().getList("OpenFOAM","DirPatterns-"+fork)]:
180 __foamInstallations.update(findInstalledVersions(bdir,val,fork))
181
182 return __foamInstallations
183
185 """The currently used fork of OpenFOAM/Foam"""
186 try:
187 return environ["WM_FORK"]
188 except KeyError:
189 return "openfoam"
190
191 -def changeFoamVersion(new,
192 force64=False,
193 force32=False,
194 compileOption=None,
195 foamCompiler=None,
196 wmCompiler=None):
197 """Changes the used FoamVersion. Only valid during the runtime of
198 the interpreter (the script or the Python session)
199 @param new: The new Version
200 @param force64: Forces the 64-bit-version to be chosen
201 @param force32: Forces the 32-bit-version to be chosen
202 @param compileOption: Forces Debug or Opt
203 @param wmCompiler: Force new value for WM_COMPILER
204 @param foamCompiler: Force system or OpenFOAM-Compiler"""
205
206 newFork,newVersion,basedir=findInstallationDir(new)
207
208 old=None
209 oldFork=foamFork()
210 if "WM_PROJECT_VERSION" in environ:
211 old=environ["WM_PROJECT_VERSION"]
212 if newVersion==old and newFork==oldFork:
213 warning(old+"-"+foamFork(),"is already being used")
214 else:
215 warning("No OpenFOAM-Version installed")
216
217 if path.exists(path.join(basedir,"etc")):
218 script=path.join(basedir,"etc","bashrc")
219 else:
220 script=path.join(basedir,".OpenFOAM-"+new,"bashrc")
221
222 forceArchOption=None
223 if force64:
224 forceArchOption="64"
225 elif force32:
226 forceArchOption="32"
227
228 injectVariables(script,
229 forceArchOption=forceArchOption,
230 compileOption=compileOption,
231 foamCompiler=foamCompiler,
232 wmCompiler=wmCompiler)
233
234 try:
235 if old==environ["WM_PROJECT_VERSION"] and oldFork==foamFork():
236 warning("Problem while changing to version",new,"old version still used:",foamFork()+"-"+environ["WM_PROJECT_VERSION"])
237 except KeyError:
238 pass
239
240 -def injectVariables(script,
241 forceArchOption=None,
242 compileOption=None,
243 foamCompiler=None,
244 wmCompiler=None):
245 """Executes a script in a subshell and changes the current
246 environment with the enivironment after the execution
247 @param script: the script that is executed
248 @param forceArchOption: To which architecture Option should be forced
249 @param compileOption: to which value the WM_COMPILE_OPTION should be forced
250 @param wmCompiler: Force new value for WM_COMPILER
251 @param foamCompiler: Force system or OpenFOAM-Compiler"""
252
253
254 for v in ["FOAM_INST_DIR",
255 "WM_THIRD_PARTY_DIR",
256 "WM_PROJECT_USER_DIR",
257 "OPAL_PREFIX"]:
258 try:
259 del environ[v]
260 except KeyError:
261 pass
262
263 if not path.exists(script):
264 error("Can not execute",script,"it does not exist")
265
266 try:
267 if "SHELL" in environ:
268 shell=environ["SHELL"]
269
270 if(path.basename(shell).find("python")==0):
271
272 shell=config().get("Paths","bash")
273 environ["SHELL"]=shell
274
275 allowedShells = [ "bash", "zsh"]
276 if not path.basename(shell) in allowedShells:
277 error("Currently only implemented for the shells",allowedShells,", not for",shell)
278
279 cmd=""
280 postCmd=""
281 if forceArchOption!=None:
282 cmd+="export WM_ARCH_OPTION="+forceArchOption+"; "
283 postCmd+=" WM_ARCH_OPTION="+forceArchOption
284 if compileOption!=None:
285 cmd+="export WM_COMPILE_OPTION="+compileOption+"; "
286 postCmd+=" WM_COMPILE_OPTION="+compileOption
287 if foamCompiler!=None:
288 cmd+="export foamCompiler="+foamCompiler+"; "
289 postCmd+=" foamCompiler="+foamCompiler
290 if wmCompiler!=None:
291 cmd+="export WM_COMPILER="+wmCompiler+"; "
292 postCmd+=" WM_COMPILER="+wmCompiler
293 cmd+=". "+script+postCmd+'; echo "Starting The Dump Of Variables"; export'
294 except KeyError:
295
296
297 name = sys.exc_info()[1]
298 error("Can't do it, because shell variable",name,"is undefined")
299
300 if sys.version_info<(2,6):
301 raus,rein = popen4(cmd)
302 else:
303 p = Popen(cmd, shell=True,
304 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
305 (rein,raus)=(p.stdin,p.stdout)
306
307 lines=raus.readlines()
308 rein.close()
309 raus.close()
310
311
312 exp=re.compile('export (.+)="(.*)"\n')
313 exp2=re.compile("export (.+)='(.*)'\n")
314
315 cnt=0
316
317 for l in lines:
318 m=exp.match(str(l))
319 if not m:
320 m=exp2.match(str(l))
321 if m:
322 cnt+=1
323 environ[m.groups()[0]]=m.groups()[1]
324
325
326