CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/FWCore/PythonUtilities/scripts/fjr2json.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 from FWCore.PythonUtilities.XML2Python import xml2obj
00004 from FWCore.PythonUtilities.LumiList   import LumiList
00005 from pprint import pprint
00006 
00007 import ast
00008 import optparse
00009 import sys
00010 
00011 
00012 if __name__ == '__main__':
00013 
00014     parser = optparse.OptionParser ("Usage: %prog [--options] job1.fjr [job2.fjr...]")
00015     parser.add_option ('--output', dest='output', type='string',
00016                        help='Save output to file OUTPUT')
00017     (options, args) = parser.parse_args()
00018     if not args:
00019         raise RuntimeError, "Must provide at least one input file"
00020 
00021     runsLumisDict = {}
00022     for fjr in args:
00023         try:
00024             obj = xml2obj (filename=fjr)
00025         except:
00026             print "'%s' is not an framework job report.  Skipping." % fjr
00027             continue
00028         for inputFile in obj.InputFile:
00029             try: # Regular XML version, assume only one of these
00030                 runObjects = inputFile.Runs.Run
00031                 for run in runObjects:
00032                     runNumber = int (run.ID)
00033                     runList = runsLumisDict.setdefault (runNumber, [])
00034                     for lumiPiece in run.LumiSection:
00035                         lumi = int (lumiPiece.ID)
00036                         runList.append (lumi)
00037             except:
00038                 try: # JSON-like version in CRAB XML files, runObjects is usually a list
00039                     if isinstance(inputFile.Runs, basestring):
00040                         runObjects = [inputFile.Runs]
00041                     else:
00042                         runObjects = inputFile.Runs
00043 
00044                     for runObject in runObjects:
00045                         try:
00046                             runs = ast.literal_eval(runObject)
00047                             for (run, lumis) in runs.iteritems():
00048                                 runList = runsLumisDict.setdefault (int(run), [])
00049                                 runList.extend(lumis)
00050                         except ValueError: # Old style handled above
00051                             pass
00052                 except:
00053                     print "Run missing in '%s'.  Skipping." % fjr
00054                 continue
00055 
00056     jsonList = LumiList (runsAndLumis = runsLumisDict)
00057     if options.output:
00058         jsonList.writeJSON (options.output)
00059     else:
00060         print jsonList
00061