389 def main(argv = None):
392 optParser = optparse.OptionParser()
393 optParser.description =
"""All-in-one Alignment Validation.
394 This will run various validation procedures either on batch queues or interactively.
395 If no name is given (-N parameter) a name containing time and date is created automatically.
396 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
398 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
399 help=
"create all scripts and cfg File but do not start jobs (default=False)")
400 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
True,
401 help=
"get all Images created during the process (default= True)")
402 defaultConfig =
"TkAlConfig.ini"
403 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
404 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
405 optParser.add_option(
"-N",
"--Name", dest=
"Name",
406 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
407 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
408 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
409 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
410 help=
"get the status of the crab jobs", metavar=
"STATUS")
411 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
413 help=
"run the tool to get full traceback of errors",
415 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
416 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
417 " Works only for batch jobs")
419 (options, args) = optParser.parse_args(argv)
421 if not options.restrictTo ==
None:
422 options.restrictTo = options.restrictTo.split(
",")
424 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
425 options.config.split(
"," ) ]
426 config = BetterConfigParser()
427 outputIniFileSet = set( config.read( options.config ) )
428 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
431 if options.config == [ os.path.abspath( defaultConfig ) ]:
432 if (
not options.crabStatus )
and \
433 (
not os.path.exists( defaultConfig ) ):
434 raise AllInOneError, (
"Default 'ini' file '%s' not found!\n"
435 "You can specify another name with the "
436 "command line option '-c'/'--config'."
439 for iniFile
in failedIniFiles:
440 if not os.path.exists( iniFile ):
441 raise AllInOneError, (
"'%s' does not exist. Please check for "
442 "typos in the filename passed to the "
443 "'-c'/'--config' option!"
446 raise AllInOneError, (
"'%s' does exist, but parsing of the "
447 "content failed!" ) % iniFile
450 if options.Name ==
None:
451 if not options.crabStatus:
452 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
454 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
455 "alignmentValidation_*" )
456 if len( existingValDirs ) > 0:
457 options.Name = existingValDirs[-1]
459 print "Cannot guess last working directory!"
460 print (
"Please use the parameter '-N' or '--Name' to specify "
461 "the task for which you want a status report." )
465 outPath = os.path.abspath( options.Name )
468 if options.crabStatus:
470 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
471 if len( crabLogDirs ) == 0:
472 print "Found no crab tasks for job name '%s'"%( options.Name )
475 for crabLogDir
in crabLogDirs:
477 print "*" +
"=" * 78 +
"*"
478 print (
"| Status report and output retrieval for:"
479 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
481 taskName = crabLogDir.replace(
"crab.",
"" )
482 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
483 print "*" +
"=" * 78 +
"*"
485 crabOptions = {
"-getoutput":
"",
488 theCrab.run( crabOptions )
489 except AllInOneError, e:
490 print "crab: No output retrieved for this task."
491 crabOptions = {
"-status":
"",
493 theCrab.run( crabOptions )
496 general = config.getGeneral()
497 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
498 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
499 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
500 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
502 if not os.path.exists( outPath ):
503 os.makedirs( outPath )
504 elif not os.path.isdir( outPath ):
505 raise AllInOneError,
"the file %s is in the way rename the Job or move it away"%outPath
511 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
512 config.write( backupConfigFile )
515 for validation
in config.items(
"validation"):
516 alignmentList = [validation[1]]
517 validationsToAdd = [(validation[0],alignment) \
518 for alignment
in alignmentList]
519 validations.extend(validationsToAdd)
521 for validation
in validations ]
522 map(
lambda job: job.createJob(), jobs )
523 validations = [ job.getValidation()
for job
in jobs ]
528 map(
lambda job: job.runJob(), jobs )
530 if options.autoMerge:
533 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
534 print "> Automatically merging jobs when they have ended"
536 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
537 "jobName":
"TkAlMerge",
538 "logDir": config.getGeneral()[
"logdir"],
539 "script":
"TkAlMerge.sh",
540 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
541 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
544 "-o %(logDir)s/%(jobName)s.stdout "
545 "-e %(logDir)s/%(jobName)s.stderr "
547 "%(logDir)s/%(script)s"%repMap)