-
Notifications
You must be signed in to change notification settings - Fork 4
/
mixdown
executable file
·283 lines (249 loc) · 13 KB
/
mixdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#! /usr/bin/env python
# Copyright (c) 2010-2014, Lawrence Livermore National Security, LLC
# Produced at Lawrence Livermore National Laboratory
# LLNL-CODE-462894
# All rights reserved.
#
# This file is part of MixDown. Please read the COPYRIGHT file
# for Our Notice and the LICENSE file for the GNU Lesser General Public
# License.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License (as published by
# the Free Software Foundation) version 3 dated June 2007.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import multiprocessing, os, sys, tarfile, time, urllib
from md import commands, defines, exceptions, importer, logger, options, overrides, profiler, project, utilityFunctions
#--------------------------------Main---------------------------------
def main():
logger.setLogger("console")
logger.writeMessage("MixDown - A tool to simplify building\n")
try:
success = True
partialImport = False
mdOptions = options.Options()
mdProject = None
if not mdOptions.processCommandline(sys.argv):
sys.exit(1)
timeStarted = time.time()
if mdOptions.importMode:
mdProject, partialImport = importer.importTargets(mdOptions)
if mdProject == None or partialImport:
success = False
elif mdOptions.profileMode:
success = profiler.profile(mdOptions)
else:
if mdOptions.cleanMixDown:
cleanMixDown(mdOptions)
mdProject = setup(mdOptions)
if mdProject == None:
success = False
else:
mdOptions.setStatusLogPath(mdOptions.defines[defines.mdPrefix[0]], mdProject.name)
if mdOptions.restart:
readSuccess, statusLogExisted = mdProject.readStatusLog(mdOptions)
if statusLogExisted:
if not readSuccess:
logger.writeError("Reading previous project status log failed, starting build at beginning.")
else:
mdProject.determineStartPoint()
#Define Hierarchy: options defines < project defines < override group defines < command line defines
mdOptions.defines.combine(mdProject.defines)
if mdOptions.overrideGroup:
mdOptions.defines.combine(mdOptions.overrideGroup)
mdOptions.defines.combine(mdOptions.commandLineDefines)
if mdOptions.threadCount == 1:
for target in reversed(mdProject.targets):
commands.buildTarget(target, mdOptions, mdProject)
if target.success == False:
success = False
if not mdOptions.continueBuilding:
break
else:
lock = multiprocessing.RLock()
jobQueue = multiprocessing.Queue()
resultQueue = multiprocessing.Queue()
maxDepth = mdProject.targets[len(mdProject.targets)-1].dependencyDepth
for currDepth in range(maxDepth, -1, -1):
processes = []
jobCount = 0
for target in reversed(mdProject.targets):
if target.dependencyDepth == currDepth:
jobQueue.put(target)
jobCount += 1
if jobCount < mdOptions.threadCount:
processCount = jobCount
else:
processCount = mdOptions.threadCount
for _ in range(processCount):
jobQueue.put(None)
for _ in range(processCount):
p = multiprocessing.Process(target=commands.buildTargetThreaded, args=(jobQueue,resultQueue,mdOptions,mdProject,lock,))
processes.append(p)
for p in processes:
p.start()
for p in processes:
p.join()
while not resultQueue.empty():
resultTarget = resultQueue.get(False)
mdProject.replaceTarget(resultTarget)
for target in reversed(mdProject.targets):
if target.dependencyDepth == currDepth and not target.success:
if jobCount != 1:
logger.writeError(target.name + " failed. Due to threading, you may need to scroll up to read error message.")
success = False
if not success:
break
timeFinished = time.time()
timeElapsed = timeFinished - timeStarted
if mdOptions.profileMode:
message = "Profile"
else:
message = "Project"
if mdProject != None:
message += " " + mdProject.name
if mdProject != None and not mdOptions.importMode and not mdOptions.cleanMode and not mdOptions.profileMode:
mdProject.writeStatusLog(mdOptions)
if mdOptions.continueBuilding:
targetsSkippedCommandLine = []
targetsSuccess = []
targetsDependancyFailed = []
targetsFailed = []
for t in mdProject.targets:
if not mdOptions.targetSpecifiedToBuild(t.name):
targetsSkippedCommandLine.append(t.name)
elif t.success == True:
targetsSuccess.append(t.name)
elif t.skippedDueToDependanciesFailing:
targetsDependancyFailed.append(t.name)
else:
targetsFailed.append(t.name)
if len(targetsSuccess) > 0:
logger.writeMessage("\nSuccess:\n{0}".format(" ".join(targetsSuccess)))
if len(targetsFailed) > 0:
logger.writeMessage("\nFailed:\n{0}".format(" ".join(targetsFailed)))
if len(targetsDependancyFailed) > 0:
logger.writeMessage("\nSkipped due to failed dependancy:\n{0}".format(" ".join(targetsDependancyFailed)))
if len(targetsSkippedCommandLine) > 0:
logger.writeMessage("\nSkipped due to command line option:\n{0}".format(" ".join(targetsSkippedCommandLine)))
if not success:
message += " failed."
else:
message += " success."
if mdOptions.importMode:
logger.writeMessage("\nProject " + mdProject.name + " has been imported to file " + mdProject.path)
elif mdOptions.cleanMode:
logger.writeMessage("\n" + mdProject.name + " has been cleaned in " + os.path.abspath(mdOptions.buildDir))
elif mdOptions.profileMode:
logger.writeMessage("\nComputer has been profiled to file " + os.path.abspath(mdOptions.overrideFile))
else:
logger.writeMessage("\n" + mdProject.name + " has been built in " + os.path.abspath(mdOptions.buildDir))
logger.writeMessage(mdProject.name + " has been installed to " + os.path.abspath(mdOptions.defines[defines.mdPrefix[0]]))
message = "\nTotal time " + logger.secondsToHMS(timeElapsed) + "\n" + message
logger.writeMessage(message)
if partialImport:
logger.writeError("\nProject partially imported. Please refer to comments in created MixDown project file to determine reason.")
except exceptions.ToolNotInstalledException, e:
logger.writeError("%s not found." % e.tool)
logger.writeError("This project requires that %s is installed on your system." % e.tool)
success = False
finally:
logger.close()
if success:
sys.exit()
else:
sys.exit(1)
#--------------------------------Setup---------------------------------
def setup(mdOptions):
logger.setLogger(mdOptions.logger, mdOptions.logDir)
if not mdOptions.prefixDefined and not mdOptions.cleanMode:
logger.writeMessage("No prefix defined, defaulting to '" + mdOptions.defines[defines.mdPrefix[0]] + "'")
if mdOptions.projectFile == "":
logger.writeMessage("Project file was not specified on command-line.\nTrying to find project file...")
cwd = os.path.abspath(os.getcwd())
projectFiles = utilityFunctions.findFilesWithExtension(cwd, ".md")
if len(projectFiles) == 0:
logger.writeError("No project file ('.md') found in current working directory. Must specifiy on command line.", filePath=cwd)
sys.exit(1)
if len(projectFiles) > 1:
logger.writeError("More than one project file ('.md') found in current working directory. Must specifiy on command line.", filePath=cwd)
sys.exit(1)
mdOptions.projectFile = projectFiles[0]
logger.writeMessage("Found project file: " + mdOptions.projectFile + "\n")
if mdOptions.overrideFile == "" and len(mdOptions.overrideGroupNames) > 0:
logger.writeMessage("Override file was not specified while override group name was specified.\nTrying to find override file...")
cwd = os.path.abspath(os.getcwd())
overrideFiles = utilityFunctions.findFilesWithExtension(cwd, ".mdo")
if len(overrideFiles) == 0:
logger.writeError("No override file ('.mdo') found in current working directory. Must specifiy '-o' on command line.", filePath=cwd)
sys.exit(1)
if len(overrideFiles) > 1:
logger.writeError("More than one override file ('.mdo') found in current working directory. Must specifiy '-o' on command line.", filePath=cwd)
sys.exit(1)
mdOptions.overrideFile = overrideFiles[0]
logger.writeMessage("Found override file: " + mdOptions.overrideFile + "\n")
if mdOptions.verbose:
logger.writeMessage(str(mdOptions))
if mdOptions.overrideFile != "":
overrideGroups = overrides.readGroups(mdOptions.overrideFile)
if overrideGroups == None:
sys.exit(1)
finalGroup = overrides.selectGroups(overrideGroups, mdOptions.overrideGroupNames)
if finalGroup == None:
logger.writeError("Selecting Override group failed.")
sys.exit(1)
else:
mdOptions.overrideGroup = finalGroup
defines.setOverrideDefines(mdOptions.defines, finalGroup)
if not mdOptions.validate():
return None
mdProject = project.Project(mdOptions.projectFile)
if not mdProject.read():
return None
if not mdProject.addSkipStepFromOptions(mdOptions):
return None
mdProject.setTargetFieldsAsDefines(mdOptions.defines)
if not mdProject.examine(mdOptions):
return None
if not mdProject.validate(mdOptions):
return None
if mdOptions.cleanMode:
for currTarget in mdProject.targets:
currTarget.path = currTarget.determineOutputPath(mdOptions)
else:
cleaningOutputReported = False
for currTarget in mdProject.targets:
if currTarget.outputPath != "" and os.path.exists(currTarget.outputPath):
if cleaningOutputReported:
logger.writeMessage("Cleaning MixDown and Target output directories...")
cleaningOutputReported = True
utilityFunctions.removeDir(currTarget.outputPath)
prefixDefine = mdOptions.defines[defines.mdPrefix[0]]
if prefixDefine != "":
#TODO: only add lib64 if on 64bit machines
libraryPaths = os.path.join(prefixDefine, "lib64") + ":" + os.path.join(prefixDefine, "lib")
if os.environ.has_key("LD_LIBRARY_PATH"):
originalLibraryPath = str.strip(os.environ["LD_LIBRARY_PATH"])
if originalLibraryPath != "":
libraryPaths += ":" + originalLibraryPath
os.environ["LD_LIBRARY_PATH"] = libraryPaths
return mdProject
def cleanMixDown(mdOptions):
try:
logger.writeMessage("Cleaning MixDown directories...\n")
utilityFunctions.removeDir(mdOptions.buildDir)
utilityFunctions.removeDir(mdOptions.downloadDir)
utilityFunctions.removeDir(mdOptions.logDir)
except IOError, e:
logger.writeError(e, exitProgram=True)
#----------------------------------------------------------------------
if __name__ == "__main__":
main()