397 def main(argv = None):
400 optParser = optparse.OptionParser()
401 optParser.description =
"""All-in-one Alignment Validation.
402 This will run various validation procedures either on batch queues or interactviely.
403 If no name is given (-N parameter) a name containing time and date is created automatically.
404 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
406 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
407 help=
"create all scripts and cfg File but do not start jobs (default=False)")
408 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
False,
409 help=
"get all Images created during the process (default= False)")
410 defaultConfig =
"TkAlConfig.ini"
411 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
412 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
413 optParser.add_option(
"-N",
"--Name", dest=
"Name",
414 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
415 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
416 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
417 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
418 help=
"get the status of the crab jobs", metavar=
"STATUS")
419 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
421 help=
"run the tool to get full traceback of errors",
423 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
424 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
425 " Works only for batch jobs")
427 (options, args) = optParser.parse_args(argv)
429 if not options.restrictTo ==
None:
430 options.restrictTo = options.restrictTo.split(
",")
432 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
433 options.config.split(
"," ) ]
434 config = BetterConfigParser()
435 outputIniFileSet = set( config.read( options.config ) )
436 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
439 if options.config == [ os.path.abspath( defaultConfig ) ]:
440 if (
not options.crabStatus )
and \
441 (
not os.path.exists( defaultConfig ) ):
442 raise AllInOneError, (
"Default 'ini' file '%s' not found!\n"
443 "You can specify another name with the "
444 "command line option '-c'/'--config'."
447 for iniFile
in failedIniFiles:
448 if not os.path.exists( iniFile ):
449 raise AllInOneError, (
"'%s' does not exist. Please check for "
450 "typos in the filename passed to the "
451 "'-c'/'--config' option!"
454 raise AllInOneError, (
"'%s' does exist, but parsing of the "
455 "content failed!" ) % iniFile
458 if options.Name ==
None:
459 if not options.crabStatus:
460 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
462 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
463 "alignmentValidation_*" )
464 if len( existingValDirs ) > 0:
465 options.Name = existingValDirs[-1]
467 print "Cannot guess last working directory!"
468 print (
"Please use the parameter '-N' or '--Name' to specify "
469 "the task for which you want a status report." )
473 outPath = os.path.abspath( options.Name )
476 if options.crabStatus:
478 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
479 if len( crabLogDirs ) == 0:
480 print "Found no crab tasks for job name '%s'"%( options.Name )
483 for crabLogDir
in crabLogDirs:
485 print "*" +
"=" * 78 +
"*"
486 print (
"| Status report and output retrieval for:"
487 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
489 taskName = crabLogDir.replace(
"crab.",
"" )
490 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
491 print "*" +
"=" * 78 +
"*"
493 crabOptions = {
"-getoutput":
"",
496 theCrab.run( crabOptions )
497 except AllInOneError, e:
498 print "crab: No output retrieved for this task."
499 crabOptions = {
"-status":
"",
501 theCrab.run( crabOptions )
504 general = config.getGeneral()
505 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
506 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
507 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
508 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
512 if os.path.isdir( outPath ):
513 shutil.rmtree( outPath )
515 if not os.path.exists( outPath ):
516 os.makedirs( outPath )
517 elif not os.path.isdir( outPath ):
518 raise AllInOneError,
"the file %s is in the way rename the Job or move it away"%outPath
524 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
525 config.write( backupConfigFile )
528 for validation
in config.items(
"validation"):
529 alignmentList = [validation[1]]
530 validationsToAdd = [(validation[0],alignment) \
531 for alignment
in alignmentList]
532 validations.extend(validationsToAdd)
534 for validation
in validations ]
535 map(
lambda job: job.createJob(), jobs )
536 validations = [ job.getValidation()
for job
in jobs ]
541 map(
lambda job: job.runJob(), jobs )
543 if options.autoMerge:
546 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
547 print "> Automatically merging jobs when they have ended"
549 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
550 "jobName":
"TkAlMerge",
551 "logDir": config.getGeneral()[
"logdir"],
552 "script":
"TkAlMerge.sh",
553 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
554 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
557 "-o %(logDir)s/%(jobName)s.stdout "
558 "-e %(logDir)s/%(jobName)s.stderr "
560 "%(logDir)s/%(script)s"%repMap)