416 def main(argv = None):
419 optParser = optparse.OptionParser()
420 optParser.description =
"""All-in-one Alignment Validation.
421 This will run various validation procedures either on batch queues or interactviely.
422 If no name is given (-N parameter) a name containing time and date is created automatically.
423 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
425 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
426 help=
"create all scripts and cfg File but do not start jobs (default=False)")
427 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
False,
428 help=
"get all Images created during the process (default= False)")
429 defaultConfig =
"TkAlConfig.ini"
430 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
431 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
432 optParser.add_option(
"-N",
"--Name", dest=
"Name",
433 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
434 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
435 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
436 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
437 help=
"get the status of the crab jobs", metavar=
"STATUS")
438 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
440 help=
"run the tool to get full traceback of errors",
442 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
443 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
444 " Works only for batch jobs")
446 (options, args) = optParser.parse_args(argv)
448 if not options.restrictTo ==
None:
449 options.restrictTo = options.restrictTo.split(
",")
451 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
452 options.config.split(
"," ) ]
453 config = BetterConfigParser()
454 outputIniFileSet = set( config.read( options.config ) )
455 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
458 if options.config == [ os.path.abspath( defaultConfig ) ]:
459 if (
not options.crabStatus )
and \
460 (
not os.path.exists( defaultConfig ) ):
461 raise AllInOneError, (
"Default 'ini' file '%s' not found!\n"
462 "You can specify another name with the "
463 "command line option '-c'/'--config'."
466 for iniFile
in failedIniFiles:
467 if not os.path.exists( iniFile ):
468 raise AllInOneError, (
"'%s' does not exist. Please check for "
469 "typos in the filename passed to the "
470 "'-c'/'--config' option!"
473 raise AllInOneError, (
"'%s' does exist, but parsing of the "
477 if options.Name ==
None:
478 if not options.crabStatus:
479 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
481 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
482 "alignmentValidation_*" )
483 if len( existingValDirs ) > 0:
484 options.Name = existingValDirs[-1]
486 print "Cannot guess last working directory!"
487 print (
"Please use the parameter '-N' or '--Name' to specify "
488 "the task for which you want a status report." )
492 outPath = os.path.abspath( options.Name )
495 if options.crabStatus:
497 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
498 if len( crabLogDirs ) == 0:
499 print "Found no crab tasks for job name '%s'"%( options.Name )
502 for crabLogDir
in crabLogDirs:
504 print "*" +
"=" * 78 +
"*"
505 print (
"| Status report and output retrieval for:"
506 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
508 taskName = crabLogDir.replace(
"crab.",
"" )
509 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
510 print "*" +
"=" * 78 +
"*"
512 crabOptions = {
"-getoutput":
"",
515 theCrab.run( crabOptions )
516 except AllInOneError, e:
517 print "crab: No output retrieved for this task."
518 crabOptions = {
"-status":
"",
520 theCrab.run( crabOptions )
523 general = config.getGeneral()
524 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
525 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
526 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
527 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
531 if os.path.isdir( outPath ):
532 shutil.rmtree( outPath )
534 if not os.path.exists( outPath ):
535 os.makedirs( outPath )
536 elif not os.path.isdir( outPath ):
537 raise AllInOneError,
"the file %s is in the way rename the Job or move it away"%outPath
543 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
544 config.write( backupConfigFile )
547 for validation
in config.items(
"validation"):
548 alignmentList = validation[1].
split(config.getSep())
549 validationsToAdd = [(validation[0],alignment) \
550 for alignment
in alignmentList]
551 validations.extend(validationsToAdd)
553 for validation
in validations ]
554 map(
lambda job: job.createJob(), jobs )
555 validations = [ job.getValidation()
for job
in jobs ]
557 if "OfflineValidationParallel" not in [val.__class__.__name__
for val
in validations]:
563 map(
lambda job: job.runJob(), jobs )
565 if options.autoMerge:
568 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
569 print "> Automatically merging jobs when they have ended"
571 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
572 "jobName":
"TkAlMerge",
573 "logDir": config.getGeneral()[
"logdir"],
574 "script":
"TkAlMerge.sh",
575 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
576 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
579 "-o %(logDir)s/%(jobName)s.stdout "
580 "-e %(logDir)s/%(jobName)s.stderr "
582 "%(logDir)s/%(script)s"%repMap)