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