375 def main(argv = None):
378 optParser = optparse.OptionParser()
379 optParser.description =
"""All-in-one Alignment Validation.
380 This will run various validation procedures either on batch queues or interactviely.
381 If no name is given (-N parameter) a name containing time and date is created automatically.
382 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
384 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
385 help=
"create all scripts and cfg File but do not start jobs (default=False)")
386 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
False,
387 help=
"get all Images created during the process (default= False)")
388 defaultConfig =
"TkAlConfig.ini"
389 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
390 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
391 optParser.add_option(
"-N",
"--Name", dest=
"Name",
392 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
393 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
394 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
395 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
396 help=
"get the status of the crab jobs", metavar=
"STATUS")
397 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
399 help=
"run the tool to get full traceback of errors",
401 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
402 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
403 " Works only for batch jobs")
405 (options, args) = optParser.parse_args(argv)
407 if not options.restrictTo ==
None:
408 options.restrictTo = options.restrictTo.split(
",")
410 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
411 options.config.split(
"," ) ]
412 config = BetterConfigParser()
413 outputIniFileSet = set( config.read( options.config ) )
414 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
417 if options.config == [ os.path.abspath( defaultConfig ) ]:
418 if (
not options.crabStatus )
and \
419 (
not os.path.exists( defaultConfig ) ):
420 raise AllInOneError, (
"Default 'ini' file '%s' not found!\n"
421 "You can specify another name with the "
422 "command line option '-c'/'--config'."
425 for iniFile
in failedIniFiles:
426 if not os.path.exists( iniFile ):
427 raise AllInOneError, (
"'%s' does not exist. Please check for "
428 "typos in the filename passed to the "
429 "'-c'/'--config' option!"
432 raise AllInOneError, (
"'%s' does exist, but parsing of the "
436 if options.Name ==
None:
437 if not options.crabStatus:
438 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
440 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
441 "alignmentValidation_*" )
442 if len( existingValDirs ) > 0:
443 options.Name = existingValDirs[-1]
445 print "Cannot guess last working directory!"
446 print (
"Please use the parameter '-N' or '--Name' to specify "
447 "the task for which you want a status report." )
451 outPath = os.path.abspath( options.Name )
454 if options.crabStatus:
456 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
457 if len( crabLogDirs ) == 0:
458 print "Found no crab tasks for job name '%s'"%( options.Name )
461 for crabLogDir
in crabLogDirs:
463 print "*" +
"=" * 78 +
"*"
464 print (
"| Status report and output retrieval for:"
465 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
467 taskName = crabLogDir.replace(
"crab.",
"" )
468 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
469 print "*" +
"=" * 78 +
"*"
471 crabOptions = {
"-getoutput":
"",
474 theCrab.run( crabOptions )
475 except AllInOneError, e:
476 print "crab: No output retrieved for this task."
477 crabOptions = {
"-status":
"",
479 theCrab.run( crabOptions )
482 general = config.getGeneral()
483 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
484 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
485 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
486 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
490 if os.path.isdir( outPath ):
491 shutil.rmtree( outPath )
493 if not os.path.exists( outPath ):
494 os.makedirs( outPath )
495 elif not os.path.isdir( outPath ):
496 raise AllInOneError,
"the file %s is in the way rename the Job or move it away"%outPath
502 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
503 config.write( backupConfigFile )
506 for validation
in config.items(
"validation"):
507 alignmentList = validation[1].
split(config.getSep())
508 validationsToAdd = [(validation[0],alignment) \
509 for alignment
in alignmentList]
510 validations.extend(validationsToAdd)
512 for validation
in validations ]
513 map(
lambda job: job.createJob(), jobs )
514 validations = [ job.getValidation()
for job
in jobs ]
519 map(
lambda job: job.runJob(), jobs )
521 if options.autoMerge:
524 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
525 print "> Automatically merging jobs when they have ended"
527 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
528 "jobName":
"TkAlMerge",
529 "logDir": config.getGeneral()[
"logdir"],
530 "script":
"TkAlMerge.sh",
531 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
532 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
535 "-o %(logDir)s/%(jobName)s.stdout "
536 "-e %(logDir)s/%(jobName)s.stderr "
538 "%(logDir)s/%(script)s"%repMap)