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