415 def main(argv = None):
418 optParser = optparse.OptionParser()
419 optParser.description =
"""All-in-one Alignment Validation.
420 This will run various validation procedures either on batch queues or interactively.
421 If no name is given (-N parameter) a name containing time and date is created automatically.
422 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
424 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
425 help=
"create all scripts and cfg File but do not start jobs (default=False)")
426 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
True,
427 help=
"get all Images created during the process (default= True)")
428 defaultConfig =
"TkAlConfig.ini"
429 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
430 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
431 optParser.add_option(
"-N",
"--Name", dest=
"Name",
432 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
433 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
434 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
435 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
436 help=
"get the status of the crab jobs", metavar=
"STATUS")
437 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
439 help=
"run the tool to get full traceback of errors",
441 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
442 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
443 " Works only for batch jobs")
445 (options, args) = optParser.parse_args(argv)
447 if not options.restrictTo ==
None:
448 options.restrictTo = options.restrictTo.split(
",")
450 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
451 options.config.split(
"," ) ]
452 config = BetterConfigParser()
453 outputIniFileSet = set( config.read( options.config ) )
454 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
457 if options.config == [ os.path.abspath( defaultConfig ) ]:
458 if (
not options.crabStatus )
and \
459 (
not os.path.exists( defaultConfig ) ):
460 raise AllInOneError(
"Default 'ini' file '%s' not found!\n"
461 "You can specify another name with the "
462 "command line option '-c'/'--config'."
465 for iniFile
in failedIniFiles:
466 if not os.path.exists( iniFile ):
467 raise AllInOneError(
"'%s' does not exist. Please check for "
468 "typos in the filename passed to the "
469 "'-c'/'--config' option!"
472 raise AllInOneError((
"'%s' does exist, but parsing of the "
473 "content failed!" ) % iniFile)
476 if options.Name ==
None:
477 if not options.crabStatus:
478 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
480 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
481 "alignmentValidation_*" )
482 if len( existingValDirs ) > 0:
483 options.Name = existingValDirs[-1]
485 print "Cannot guess last working directory!"
486 print (
"Please use the parameter '-N' or '--Name' to specify "
487 "the task for which you want a status report." )
491 outPath = os.path.abspath( options.Name )
494 if options.crabStatus:
496 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
497 if len( crabLogDirs ) == 0:
498 print "Found no crab tasks for job name '%s'"%( options.Name )
501 for crabLogDir
in crabLogDirs:
503 print "*" +
"=" * 78 +
"*"
504 print (
"| Status report and output retrieval for:"
505 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
507 taskName = crabLogDir.replace(
"crab.",
"" )
508 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
509 print "*" +
"=" * 78 +
"*"
511 crabOptions = {
"-getoutput":
"",
514 theCrab.run( crabOptions )
515 except AllInOneError
as e:
516 print "crab: No output retrieved for this task."
517 crabOptions = {
"-status":
"",
519 theCrab.run( crabOptions )
522 general = config.getGeneral()
523 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
524 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
525 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
526 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
528 if not os.path.exists( outPath ):
529 os.makedirs( outPath )
530 elif not os.path.isdir( outPath ):
531 raise AllInOneError(
"the file %s is in the way rename the Job or move it away"%outPath)
537 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
538 config.write( backupConfigFile )
541 for validation
in config.items(
"validation"):
542 alignmentList = [validation[1]]
543 validationsToAdd = [(validation[0],alignment) \
544 for alignment
in alignmentList]
545 validations.extend(validationsToAdd)
547 for validation
in validations ]
548 map(
lambda job: job.createJob(), jobs )
549 validations = [ job.getValidation()
for job
in jobs ]
554 map(
lambda job: job.runJob(), jobs )
556 if options.autoMerge:
559 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
560 print "> Automatically merging jobs when they have ended"
562 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
563 "jobName":
"TkAlMerge",
564 "logDir": config.getGeneral()[
"logdir"],
565 "script":
"TkAlMerge.sh",
566 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
567 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
569 for ext
in (
"stdout",
"stderr",
"stdout.gz",
"stderr.gz"):
570 oldlog =
"%(logDir)s/%(jobName)s."%repMap + ext
571 if os.path.exists(oldlog):
575 "-o %(logDir)s/%(jobName)s.stdout "
576 "-e %(logDir)s/%(jobName)s.stderr "
578 "%(logDir)s/%(script)s"%repMap)