1
2 """
3 Application class that implements pyFoamBuildHelper
4 """
5
6 from .PyFoamApplication import PyFoamApplication
7 from PyFoam.Basics.GeneralVCSInterface import whichVCS,getVCS
8 from PyFoam.Error import FatalErrorPyFoamException
9
10 from optparse import OptionGroup
11 from os import environ,path
12 from platform import uname
13 import os,subprocess
14
15 from PyFoam.ThirdParty.six import print_
16
17 import sys
18
20 - def __init__(self,
21 args=None,
22 **kwargs):
23 description="""\
24 This application helps with updating a
25 project and the projects it depends on. A phase (or a desired output)
26 has to be specified with an options.
27
28 If no arguments are given then it assumes that the current directory
29 is the root directory of the project to be compiled and that the only
30 project it depends on is the OpenFOAM-installation (as found in the
31 WM_PROJECT_DIR-environment variable). Arguments are assumed to be
32 other projects that are injected between these two
33 """
34
35 PyFoamApplication.__init__(self,
36 nr=0,
37 exactNr=False,
38 args=args,
39 usage="%prog [options] <directories> [arguments]",
40 description=description,
41 interspersed=True,
42 **kwargs)
43
45 projects=OptionGroup(self.parser,
46 "Projects",
47 "Which projects should be automatically considered")
48 self.parser.add_option_group(projects)
49
50 projects.add_option("--no-openfoam",
51 dest="openfoam",
52 default=True,
53 action="store_false",
54 help="Do not consider the OpenFOAM-directory")
55 projects.add_option("--no-current-directory",
56 dest="currentDir",
57 default=True,
58 action="store_false",
59 help="Do not consider the current directory")
60
61 phases=OptionGroup(self.parser,
62 "Action",
63 "What to do")
64 self.parser.add_option_group(phases)
65
66 phases.add_option("--info",
67 dest="actions",
68 action="append_const",
69 const="info",
70 help="Only print the informations gathered")
71 phases.add_option("--name-of-the-build",
72 dest="actions",
73 action="append_const",
74 const="name",
75 help="The unique name of the build to identify it in testing environments")
76 phases.add_option("--update",
77 dest="actions",
78 action="append_const",
79 const="update",
80 help="Update the sources in all directories from their servers")
81 phases.add_option("--build",
82 dest="actions",
83 action="append_const",
84 const="build",
85 help="Build all projects in the correct order")
86
87 parameters=OptionGroup(self.parser,
88 "Parameters",
89 "Additional parameters")
90 self.parser.add_option_group(parameters)
91 parameters.add_option("--timeout",
92 dest="timeout",
93 action="store",
94 type="int",
95 default=None,
96 help="Default timeout (in seconds) for the update from the repositories. If none specified the default of the VCS in question is used. Only applicable if the VCS supports it")
97
99 if not self.opts.actions:
100 self.error("No action defined")
101
102 dirs=[]
103 if self.opts.openfoam and "WM_PROJECT_DIR" in environ:
104 dirs.append(environ["WM_PROJECT_DIR"])
105 dirs+=self.parser.getArgs()
106 if self.opts.currentDir:
107 dirs.append(path.curdir)
108
109 fullDirs=[]
110 for d in dirs:
111 if path.isdir(d):
112 fullDirs.append(path.abspath(d))
113
114 info=dict(list(zip(fullDirs,[{} for i in range(len(fullDirs))])))
115
116 for d in fullDirs:
117 info[d]["writable"]=os.access(d,os.W_OK)
118
119 info[d]["isFoam"]=(d==fullDirs[0] and self.opts.openfoam)
120
121 info[d]["vcs"]=whichVCS(d)
122
123 if path.exists(path.join(d,"Allwmake")):
124 info[d]["make"]="Allwmake"
125 elif path.exists(path.join(d,"Makefile")):
126 info[d]["make"]="make"
127 else:
128 info[d]["make"]="wmake"
129
130 if info[d]["vcs"]=="":
131 info[d]["branch"]="unknown"
132 else:
133 vcs=getVCS(info[d]["vcs"],
134 d,
135 tolerant=True)
136 if vcs:
137 try:
138 info[d]["branch"]=vcs.branchName()
139 except FatalErrorPyFoamException:
140 info[d]["branch"]="notImplemented"
141
142 try:
143 info[d]["revision"]=vcs.getRevision()
144 except FatalErrorPyFoamException:
145 info[d]["revision"]="notImplemented"
146 else:
147 info[d]["branch"]="noVCS"
148 info[d]["revision"]="noVCS"
149
150 for action in self.opts.actions:
151 if action=="info":
152 print_("Project directories:\n")
153 for i,d in enumerate(fullDirs):
154 print_("%2d. %s" % (i+1,d))
155 print_(" ",info[d])
156 print_()
157
158 self.setData({'order' : fullDirs,
159 'info' : info})
160 elif action=="name":
161 name=""
162 if self.opts.openfoam:
163 name+="%s-%s_%s_%s_%s" % (environ["WM_PROJECT"],
164 environ["WM_PROJECT_VERSION"],
165 environ["WM_ARCH"],
166 environ["WM_OPTIONS"],
167 environ["WM_MPLIB"])
168 else:
169 name+="%s_%s" % (uname()[0],
170 uname()[-1])
171 name += "_branch-%s" % info[fullDirs[-1]]["branch"]
172
173 print_(name)
174 self.setData({'name' : name,
175 'info' : info,
176 'order' : fullDirs})
177 elif action=="update":
178 success=True
179 for d in fullDirs:
180 if info[d]["writable"]:
181 print_("Attempting to update",d)
182 print_()
183 vcs=getVCS(info[d]["vcs"],
184 d,
185 tolerant=True)
186 if vcs:
187 try:
188 if not vcs.update(timeout=self.opts.timeout):
189 success=False
190 except FatalErrorPyFoamException:
191 e = sys.exc_info()[1]
192 print_("Problem:",e)
193 success=False
194 else:
195 print_("Not under version control ... skipping")
196 success=False
197 else:
198 print_(d,"not writable .... skipping update")
199
200 print_()
201 if not success:
202 self.error("Problem during updating")
203 elif action=="build":
204 success=True
205 oldDir=os.getcwd()
206
207 for d in fullDirs:
208 if info[d]["writable"]:
209 print_("Attempting to build",d)
210 print_()
211 makeCommand={"make" :["make"],
212 "wmake" :["wmake"],
213 "Allwmake":["./Allwmake"]}[info[d]["make"]]
214
215 print_("Changing to",d,"and executing"," ".join(makeCommand))
216 print_()
217 os.chdir(d)
218 erg=subprocess.call(makeCommand)
219 if erg:
220 print_()
221 print_("Result of build command:",erg)
222 success=False
223 else:
224 print_(d,"not writable .... skipping build")
225
226 print_()
227
228 os.chdir(oldDir)
229
230 if not success:
231 self.error("Problem during building")
232
233 else:
234 self.error("Unimplemented action",action)
235
236
237