CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
fjr2json.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from FWCore.PythonUtilities.XML2Python import xml2obj
4 from FWCore.PythonUtilities.LumiList import LumiList
5 from pprint import pprint
6 
7 import ast
8 import optparse
9 import sys
10 
11 
12 if __name__ == '__main__':
13 
14  parser = optparse.OptionParser ("Usage: %prog [--options] job1.fjr [job2.fjr...]")
15  parser.add_option ('--output', dest='output', type='string',
16  help='Save output to file OUTPUT')
17  (options, args) = parser.parse_args()
18  if not args:
19  raise RuntimeError, "Must provide at least one input file"
20 
21  runsLumisDict = {}
22  for fjr in args:
23  try:
24  obj = xml2obj (filename=fjr)
25  except:
26  print "'%s' is not an framework job report. Skipping." % fjr
27  continue
28  for inputFile in obj.InputFile:
29  try: # Regular XML version, assume only one of these
30  runObjects = inputFile.Runs.Run
31  for run in runObjects:
32  runNumber = int (run.ID)
33  runList = runsLumisDict.setdefault (runNumber, [])
34  for lumiPiece in run.LumiSection:
35  lumi = int (lumiPiece.ID)
36  runList.append (lumi)
37  except:
38  try: # JSON-like version in CRAB XML files, runObjects is usually a list
39  if isinstance(inputFile.Runs, basestring):
40  runObjects = [inputFile.Runs]
41  else:
42  runObjects = inputFile.Runs
43 
44  for runObject in runObjects:
45  try:
46  runs = ast.literal_eval(runObject)
47  for (run, lumis) in runs.iteritems():
48  runList = runsLumisDict.setdefault (int(run), [])
49  runList.extend(lumis)
50  except ValueError: # Old style handled above
51  pass
52  except:
53  print "Run missing in '%s'. Skipping." % fjr
54  continue
55 
56  jsonList = LumiList (runsAndLumis = runsLumisDict)
57  if options.output:
58  jsonList.writeJSON (options.output)
59  else:
60  print jsonList
61