460 def main(argv = None):
463 optParser = optparse.OptionParser()
464 optParser.description =
"""All-in-one Alignment Validation.
465 This will run various validation procedures either on batch queues or interactively.
466 If no name is given (-N parameter) a name containing time and date is created automatically.
467 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
469 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
470 help=
"create all scripts and cfg File but do not start jobs (default=False)")
471 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
True,
472 help=
"get all Images created during the process (default= True)")
473 defaultConfig =
"TkAlConfig.ini"
474 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
475 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
476 optParser.add_option(
"-N",
"--Name", dest=
"Name",
477 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
478 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
479 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
480 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
481 help=
"get the status of the crab jobs", metavar=
"STATUS")
482 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
484 help=
"run the tool to get full traceback of errors",
486 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
487 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
488 " Works only for batch jobs")
490 (options, args) = optParser.parse_args(argv)
492 if not options.restrictTo ==
None:
493 options.restrictTo = options.restrictTo.split(
",")
495 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
496 options.config.split(
"," ) ]
497 config = BetterConfigParser()
498 outputIniFileSet = set( config.read( options.config ) )
499 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
502 if options.config == [ os.path.abspath( defaultConfig ) ]:
503 if (
not options.crabStatus )
and \
504 (
not os.path.exists( defaultConfig ) ):
505 raise AllInOneError(
"Default 'ini' file '%s' not found!\n"
506 "You can specify another name with the "
507 "command line option '-c'/'--config'."
510 for iniFile
in failedIniFiles:
511 if not os.path.exists( iniFile ):
512 raise AllInOneError(
"'%s' does not exist. Please check for "
513 "typos in the filename passed to the "
514 "'-c'/'--config' option!"
517 raise AllInOneError((
"'%s' does exist, but parsing of the "
518 "content failed!" ) % iniFile)
521 if options.Name ==
None:
522 if not options.crabStatus:
523 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
525 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
526 "alignmentValidation_*" )
527 if len( existingValDirs ) > 0:
528 options.Name = existingValDirs[-1]
530 print "Cannot guess last working directory!"
531 print (
"Please use the parameter '-N' or '--Name' to specify "
532 "the task for which you want a status report." )
536 outPath = os.path.abspath( options.Name )
539 if options.crabStatus:
541 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
542 if len( crabLogDirs ) == 0:
543 print "Found no crab tasks for job name '%s'"%( options.Name )
546 for crabLogDir
in crabLogDirs:
548 print "*" +
"=" * 78 +
"*"
549 print (
"| Status report and output retrieval for:"
550 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
552 taskName = crabLogDir.replace(
"crab.",
"" )
553 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
554 print "*" +
"=" * 78 +
"*"
556 crabOptions = {
"-getoutput":
"",
559 theCrab.run( crabOptions )
560 except AllInOneError
as e:
561 print "crab: No output retrieved for this task."
562 crabOptions = {
"-status":
"",
564 theCrab.run( crabOptions )
567 general = config.getGeneral()
568 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
569 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
570 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
571 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
573 if not os.path.exists( outPath ):
574 os.makedirs( outPath )
575 elif not os.path.isdir( outPath ):
576 raise AllInOneError(
"the file %s is in the way rename the Job or move it away"%outPath)
582 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
583 config.write( backupConfigFile )
586 for validation
in config.items(
"validation"):
587 alignmentList = [validation[1]]
588 validationsToAdd = [(validation[0],alignment) \
589 for alignment
in alignmentList]
590 validations.extend(validationsToAdd)
592 for validation
in validations ]
593 map(
lambda job: job.createJob(), jobs )
594 validations = [ job.getValidation()
for job
in jobs ]
599 map(
lambda job: job.runJob(), jobs )
601 if options.autoMerge:
604 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
605 print "> Automatically merging jobs when they have ended"
607 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
608 "jobName":
"TkAlMerge",
609 "logDir": config.getGeneral()[
"logdir"],
610 "script":
"TkAlMerge.sh",
611 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
612 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
614 for ext
in (
"stdout",
"stderr",
"stdout.gz",
"stderr.gz"):
615 oldlog =
"%(logDir)s/%(jobName)s."%repMap + ext
616 if os.path.exists(oldlog):
620 "-o %(logDir)s/%(jobName)s.stdout "
621 "-e %(logDir)s/%(jobName)s.stderr "
623 "%(logDir)s/%(script)s"%repMap)