CMS 3D CMS Logo

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