CMS 3D CMS Logo

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 import six
12 
13 if __name__ == '__main__':
14 
15  parser = optparse.OptionParser ("Usage: %prog [--options] job1.fjr [job2.fjr...]")
16  parser.add_option ('--output', dest='output', type='string',
17  help='Save output to file OUTPUT')
18  (options, args) = parser.parse_args()
19  if not args:
20  raise RuntimeError("Must provide at least one input file")
21 
22  runsLumisDict = {}
23  for fjr in args:
24  try:
25  obj = xml2obj (filename=fjr)
26  except:
27  print "'%s' is not an framework job report. Skipping." % fjr
28  continue
29  for inputFile in obj.InputFile:
30  try: # Regular XML version, assume only one of these
31  runObjects = inputFile.Runs.Run
32  for run in runObjects:
33  runNumber = int (run.ID)
34  runList = runsLumisDict.setdefault (runNumber, [])
35  for lumiPiece in run.LumiSection:
36  lumi = int (lumiPiece.ID)
37  runList.append (lumi)
38  except:
39  try: # JSON-like version in CRAB XML files, runObjects is usually a list
40  if isinstance(inputFile.Runs, str):
41  runObjects = [inputFile.Runs]
42  else:
43  runObjects = inputFile.Runs
44 
45  for runObject in runObjects:
46  try:
47  runs = ast.literal_eval(runObject)
48  for (run, lumis) in six.iteritems(runs):
49  runList = runsLumisDict.setdefault (int(run), [])
50  runList.extend(lumis)
51  except ValueError: # Old style handled above
52  pass
53  except:
54  print "Run missing in '%s'. Skipping." % fjr
55  continue
56 
57  jsonList = LumiList (runsAndLumis = runsLumisDict)
58  if options.output:
59  jsonList.writeJSON (options.output)
60  else:
61  print jsonList
62