1
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
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
26 """@return: directory in which the tutorials reside"""
27
28 return getPathFromEnviron("FOAM_TUTORIALS")
29
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
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
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
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 versions.append(m.groups(1)[0])
89
90 return versions
91
93 """Changes the used FoamVersion. Only valid during the runtime of
94 the interpreter (the script or the Python session)
95 @param new: The new Version"""
96
97 if not new in foamInstalledVersions():
98 error("Version",new,"is not an installed version: ",foamInstalledVersions())
99
100 if environ.has_key("WM_PROJECT_VERSION"):
101 if new==environ["WM_PROJECT_VERSION"]:
102 warning(new,"is already being used")
103 return
104 else:
105 warning("No OpenFOAM-Version installed")
106
107 try:
108
109 if environ.has_key("SHELL"):
110 shell=environ["SHELL"]
111
112 if(path.basename(shell)=="python"):
113
114 shell=config().get("Paths","bash")
115 environ["SHELL"]=shell
116
117 if(path.basename(shell)!="bash"):
118 error("Currently only implemented for bash-shell, not for",shell)
119
120 if environ.has_key("WM_PROJECT_INST_DIR"):
121 basedir=environ["WM_PROJECT_INST_DIR"]
122 else:
123 basedir=path.expanduser(config().get("OpenFOAM","Installation"))
124
125 cmd=". "+path.join(basedir,"OpenFOAM-"+new,".OpenFOAM-"+new,"bashrc")+'; echo "Starting The Dump Of Variables"; export'
126 except KeyError,name:
127 error("Can't do it, because shell variable",name,"is undefined")
128
129 raus,rein = popen4(cmd)
130 lines=raus.readlines()
131 rein.close()
132 raus.close()
133
134 exp=re.compile('export (.+)="(.*)"\n')
135
136 cnt=0
137
138 for l in lines:
139 m=exp.match(l)
140 if m:
141 cnt+=1
142 environ[m.groups()[0]]=m.groups()[1]
143 if new!=environ["WM_PROJECT_VERSION"]:
144 error("Problem while changing to version",new,"old version still used:",environ["WM_PROJECT_VERSION"])
145