12 from __future__
import absolute_import
13 from __future__
import print_function
14 import sys, os, subprocess
15 from project_modules.xmlerror
import XMLError
16 from project_modules.stagedef
import StageDef
18 from larbatch_utilities
import convert_str
19 from larbatch_utilities
import get_ups_products
28 def __init__(self, project_element, default_first_input_list, default_input_lists, check=True):
74 if 'name' in dict(project_element.attributes):
75 self.
name = str(project_element.attributes[
'name'].firstChild.data)
77 raise XMLError(
'Project name not specified.')
81 num_events_elements = project_element.getElementsByTagName(
'numevents')
82 for num_events_element
in num_events_elements:
83 if num_events_element.parentNode == project_element:
84 self.
num_events = int(num_events_element.firstChild.data)
86 raise XMLError(
'Number of events not specified.')
90 num_jobs_elements = project_element.getElementsByTagName(
'numjobs')
91 for num_jobs_element
in num_jobs_elements:
92 if num_jobs_element.parentNode == project_element:
93 self.
num_jobs = int(num_jobs_element.firstChild.data)
97 max_files_per_job_elements = project_element.getElementsByTagName(
'maxfilesperjob')
98 for max_files_per_job_element
in max_files_per_job_elements:
99 if max_files_per_job_element.parentNode == project_element:
104 ups_elements = project_element.getElementsByTagName(
'ups')
105 for ups_element
in ups_elements:
106 self.ups.append(str(ups_element.firstChild.data))
110 if len(self.
ups) == 0:
115 os_elements = project_element.getElementsByTagName(
'os')
116 for os_element
in os_elements:
117 if os_element.parentNode == project_element:
118 self.
os = str(os_element.firstChild.data)
119 self.
os =
''.
join(self.os.split())
123 resource_elements = project_element.getElementsByTagName(
'resource')
124 for resource_element
in resource_elements:
125 if resource_element.parentNode == project_element:
126 self.
resource = str(resource_element.firstChild.data)
131 role_elements = project_element.getElementsByTagName(
'role')
132 for role_element
in role_elements:
133 if role_element.parentNode == project_element:
134 self.
role = str(role_element.firstChild.data)
138 lines_elements = project_element.getElementsByTagName(
'lines')
139 for lines_element
in lines_elements:
140 if lines_element.parentNode == project_element:
141 self.
lines = str(lines_element.firstChild.data)
145 server_elements = project_element.getElementsByTagName(
'server')
146 for server_element
in server_elements:
147 if server_element.parentNode == project_element:
148 self.
server = str(server_element.firstChild.data)
152 site_elements = project_element.getElementsByTagName(
'site')
153 for site_element
in site_elements:
154 if site_element.parentNode == project_element:
155 self.
site = str(site_element.firstChild.data)
156 self.
site =
''.
join(self.site.split())
160 blacklist_elements = project_element.getElementsByTagName(
'blacklist')
161 for blacklist_element
in blacklist_elements:
162 if blacklist_element.parentNode == project_element:
163 self.
blacklist = str(blacklist_element.firstChild.data)
168 cpu_elements = project_element.getElementsByTagName(
'cpu')
169 for cpu_element
in cpu_elements:
170 if cpu_element.parentNode == project_element:
171 self.
cpu = int(cpu_element.firstChild.data)
175 disk_elements = project_element.getElementsByTagName(
'disk')
176 for disk_element
in disk_elements:
177 if disk_element.parentNode == project_element:
178 self.
disk = str(disk_element.firstChild.data)
179 self.
disk =
''.
join(self.disk.split())
183 memory_elements = project_element.getElementsByTagName(
'memory')
184 for memory_element
in memory_elements:
185 if memory_element.parentNode == project_element:
186 self.
memory = int(memory_element.firstChild.data)
190 merge_elements = project_element.getElementsByTagName(
'merge')
191 for merge_element
in merge_elements:
192 if merge_element.parentNode == project_element:
193 if merge_element.firstChild:
194 self.
merge = str(merge_element.firstChild.data)
200 anamerge_elements = project_element.getElementsByTagName(
'anamerge')
201 for anamerge_element
in anamerge_elements:
202 if anamerge_element.parentNode == project_element:
203 if anamerge_element.firstChild:
204 self.
anamerge = str(anamerge_element.firstChild.data)
210 larsoft_elements = project_element.getElementsByTagName(
'larsoft')
215 tag_elements = larsoft_elements[0].getElementsByTagName(
'tag')
216 if tag_elements
and tag_elements[0].firstChild !=
None:
217 self.
release_tag = str(tag_elements[0].firstChild.data)
221 qual_elements = larsoft_elements[0].getElementsByTagName(
'qual')
223 self.
release_qual = str(qual_elements[0].firstChild.data)
228 local_elements = larsoft_elements[0].getElementsByTagName(
'local')
230 local = str(local_elements[0].firstChild.data)
231 if larbatch_posix.isdir(local):
238 version_elements = project_element.getElementsByTagName(
'version')
240 self.
version = str(version_elements[0].firstChild.data)
248 raise IOError(
"Local release directory/tarball %s does not exist." % self.
local_release_tar)
252 file_type_elements = project_element.getElementsByTagName(
'filetype')
253 if file_type_elements:
254 self.
file_type = str(file_type_elements[0].firstChild.data)
258 run_type_elements = project_element.getElementsByTagName(
'runtype')
259 if run_type_elements:
260 self.
run_type = str(run_type_elements[0].firstChild.data)
264 run_number_elements = project_element.getElementsByTagName(
'runnumber')
265 if run_number_elements:
266 self.
run_number = int(run_number_elements[0].firstChild.data)
270 script_elements = project_element.getElementsByTagName(
'script')
271 for script_element
in script_elements:
272 if script_element.parentNode == project_element:
273 self.
script = str(script_element.firstChild.data)
280 jobinfo = subprocess.Popen([
'which', self.
script],
281 stdout=subprocess.PIPE,
282 stderr=subprocess.PIPE)
283 jobout, joberr = jobinfo.communicate()
287 script_path = jobout.splitlines()[0].
strip()
290 if script_path ==
'' or not larbatch_posix.access(script_path, os.X_OK):
291 raise IOError(
'Script %s not found.' % self.
script)
296 worker_validations = project_element.getElementsByTagName(
'check')
297 for worker_validation
in worker_validations:
298 if worker_validation.parentNode == project_element:
303 worker_copys = project_element.getElementsByTagName(
'copy')
304 for worker_copy
in worker_copys:
305 if worker_copy.parentNode == project_element:
306 self.
copy_to_fts = int(worker_copy.firstChild.data)
310 cvmfs_elements = project_element.getElementsByTagName(
'cvmfs')
311 for cvmfs_element
in cvmfs_elements:
312 if cvmfs_element.parentNode == project_element:
313 self.
cvmfs = int(cvmfs_element.firstChild.data)
317 stash_elements = project_element.getElementsByTagName(
'stash')
318 for stash_element
in stash_elements:
319 if stash_element.parentNode == project_element:
320 self.
stash = int(stash_element.firstChild.data)
324 singularity_elements = project_element.getElementsByTagName(
'singularity')
325 for singularity_element
in singularity_elements:
326 if singularity_element.parentNode == project_element:
327 self.
singularity = int(singularity_element.firstChild.data)
331 start_script_elements = project_element.getElementsByTagName(
'startscript')
332 for start_script_element
in start_script_elements:
333 if start_script_element.parentNode == project_element:
334 self.
start_script = str(start_script_element.firstChild.data)
342 jobinfo = subprocess.Popen([
'which', self.
start_script],
343 stdout=subprocess.PIPE,
344 stderr=subprocess.PIPE)
345 jobout, joberr = jobinfo.communicate()
349 script_path = jobout.splitlines()[0].
strip()
356 stop_script_elements = project_element.getElementsByTagName(
'stopscript')
357 for stop_script_element
in stop_script_elements:
358 if stop_script_element.parentNode == project_element:
359 self.
stop_script = str(stop_script_element.firstChild.data)
367 jobinfo = subprocess.Popen([
'which', self.
stop_script],
368 stdout=subprocess.PIPE,
369 stderr=subprocess.PIPE)
370 jobout, joberr = jobinfo.communicate()
374 script_path = jobout.splitlines()[0].
strip()
381 fclpath_elements = project_element.getElementsByTagName(
'fcldir')
382 for fclpath_element
in fclpath_elements:
383 self.fclpath.append(str(fclpath_element.firstChild.data))
387 if check
and 'FHICL_FILE_PATH' in os.environ:
388 for fcldir
in os.environ[
'FHICL_FILE_PATH'].split(
':'):
389 if larbatch_posix.exists(fcldir):
390 self.fclpath.append(fcldir)
396 if not larbatch_posix.exists(fcldir):
397 raise IOError(
"Fcl search directory %s does not exist." % fcldir)
401 stage_elements = project_element.getElementsByTagName(
'stage')
402 default_previous_stage =
''
403 default_input_lists[default_previous_stage] = default_first_input_list
404 for stage_element
in stage_elements:
409 if 'base' in dict(stage_element.attributes):
410 base_name = str(stage_element.attributes[
'base'].firstChild.data)
413 if stage.name == base_name:
417 if base_stage ==
None:
418 raise LookupError(
'Base stage %s not found.' % base_name)
420 self.stages.append(StageDef(stage_element,
423 default_previous_stage,
443 default_previous_stage = self.
stages[-1].name
444 default_input_lists[default_previous_stage] = os.path.join(self.
stages[-1].bookdir,
449 param_elements = project_element.getElementsByTagName(
'parameter')
450 for param_element
in param_elements:
451 name = str(param_element.attributes[
'name'].firstChild.data)
452 value = str(param_element.firstChild.data)
462 result =
'Project name = %s\n' % self.
name
463 result +=
'Total events = %d\n' % self.
num_events
464 result +=
'Number of jobs = %d\n' % self.
num_jobs
466 result +=
'Top level ups products:\n'
467 for prod
in self.
ups:
468 result +=
' %s\n' % prod
469 result +=
'OS = %s\n' % self.
os
470 result +=
'Resource = %s\n' % self.
resource
471 result +=
'Role = %s\n' % self.
role
472 result +=
'Lines = %s\n' % self.
lines
473 result +=
'Jobsub server = %s\n' % self.
server
474 result +=
'Site = %s\n' % self.
site
475 result +=
'Blacklist = %s\n' % self.
blacklist
476 result +=
'Cpu = %d\n' % self.
cpu
477 result +=
'Disk = %s\n' % self.
disk
478 result +=
'Memory = %d MB\n' % self.
memory
479 result +=
'Histogram merging program = %s\n' % self.
merge
480 result +=
'Analysis merge flag = %s\n' % self.
anamerge
481 result +=
'Larsoft release tag = %s\n' % self.
release_tag
482 result +=
'Larsoft release qualifier = %s\n' % self.
release_qual
483 result +=
'Version = %s\n' % self.
version
486 result +=
'File type = %s\n' % self.
file_type
487 result +=
'Run type = %s\n' % self.
run_type
488 result +=
'Run number = %d\n' % self.
run_number
489 result +=
'Batch script = %s\n' % self.
script
490 result +=
'Start sam project script = %s\n' % self.
start_script
491 result +=
'Stop sam project script = %s\n' % self.
stop_script
492 result +=
'Force dag = %d\n' % self.
force_dag
493 result +=
'Fcl search path:\n'
495 result +=
' %s\n' % fcldir
496 result +=
'Metadata parameters:\n'
498 result +=
'%s: %s\n' % (key,self.
parameters[key])
499 result +=
'\nStages:'
501 result +=
'\n\n' + str(stage)
509 raise LookupError(
"Project does not have any stages.")
511 elif stagename ==
'' and len(self.
stages) == 1:
516 if stagename == stage.name:
521 raise RuntimeError(
'No stage %s.' % stagename)
531 fcl = os.path.join(fcldir, name)
533 if larbatch_posix.exists(fcl):
536 if fcl ==
'' or not larbatch_posix.exists(fcl):
537 raise IOError(
'Could not find fcl file %s.' % name)
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.