CMS 3D CMS Logo

compareJSON.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import optparse
5 from FWCore.PythonUtilities.LumiList import LumiList
6 
7 
8 if __name__ == '__main__':
9 
10  parser = optparse.OptionParser ("Usage: %prog --command [--options] alpha.json beta.json [output.json]")
11  # required parameters
12  cmdGroup = optparse.OptionGroup (parser, "Command Options ")
13  cmdGroup.add_option ('--and', dest='command', action='store_const',
14  const='and',
15  help = '"and" (i.e., take intersection) of two files')
16  cmdGroup.add_option ('--or', dest='command', action='store_const',
17  const='or',
18  help = '"or" (i.e., take union) of two files')
19  cmdGroup.add_option ('--sub', dest='command', action='store_const',
20  const='sub',
21  help = '"subtraction" (i.e., lumi sections in alpha not in beta) of two files')
22  cmdGroup.add_option ('--diff', dest='command', action='store_const',
23  const='diff',
24  help = '"All differences (i.e., alpha - beta AND beta - alpha) of two files. Output will only be to screen (not proper JSON format).')
25  parser.add_option_group (cmdGroup)
26  (options, args) = parser.parse_args()
27  if len (args) < 2 or len (args) > 3:
28  raise RuntimeError("Two input filenames with one optional output filename must be provided.")
29  if not options.command:
30  raise RuntimeError("Exactly one command option must be specified")
31 
32  alphaList = LumiList (filename = args[0]) # Read in first JSON file
33  betaList = LumiList (filename = args[1]) # Read in second JSON file
34 
35  ##################
36  ## Diff Command ##
37  ##################
38  if options.command == 'diff':
39  if len (args) >= 3:
40  raise RuntimeError("Can not output to file with '--diff' option. The output is not standard JSON.")
41  firstOnly = alphaList - betaList
42  secondOnly = betaList - alphaList
43  if not firstOnly and not secondOnly:
44  print "Files '%s' and '%s' are the same." % (args[0], args[1])
45  sys.exit()
46  print "'%s'-only lumis:" % args[0]
47  if firstOnly:
48  print firstOnly
49  else:
50  print "None"
51  print "\n'%s'-only lumis:" % args[1]
52  if secondOnly:
53  print secondOnly
54  else:
55  print "None"
56  sys.exit()
57 
58  ########################
59  ## All other commands ##
60  ########################
61  if options.command == 'and':
62  outputList = alphaList & betaList
63 
64  if options.command == 'or':
65  outputList = alphaList | betaList
66 
67  if options.command == 'sub':
68  outputList = alphaList - betaList
69 
70  if len (args) >= 3:
71  outputList.writeJSON (args[2])
72  else:
73  # print to screen
74  print outputList
75