CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys
00004 import optparse
00005 from FWCore.PythonUtilities.LumiList import LumiList
00006 
00007 
00008 if __name__ == '__main__':
00009     
00010     parser = optparse.OptionParser ("Usage: %prog --command [--options] alpha.json beta.json [output.json]")
00011     # required parameters
00012     cmdGroup = optparse.OptionGroup (parser, "Command Options ")
00013     cmdGroup.add_option ('--and', dest='command', action='store_const',
00014                          const='and', 
00015                          help = '"and" (i.e., take intersection) of two files')
00016     cmdGroup.add_option ('--or', dest='command', action='store_const',
00017                          const='or',
00018                          help = '"or" (i.e., take union) of two files')
00019     cmdGroup.add_option ('--sub', dest='command', action='store_const',
00020                          const='sub',
00021                          help = '"subtraction" (i.e., lumi sections in alpha not in beta) of two files')
00022     cmdGroup.add_option ('--diff', dest='command', action='store_const',
00023                          const='diff',
00024                          help = '"All differences (i.e., alpha - beta AND beta - alpha) of two files. Output will only be to screen (not proper JSON format).')
00025     parser.add_option_group (cmdGroup)
00026     (options, args) = parser.parse_args()
00027     if len (args) < 2 or len (args) > 3:
00028         raise RuntimeError, "Two input filenames with one optional output filename must be provided."
00029     if not options.command:
00030         raise RuntimeError, "Exactly one command option must be specified"
00031 
00032     alphaList = LumiList (filename = args[0])  # Read in first  JSON file
00033     betaList  = LumiList (filename = args[1])  # Read in second JSON file
00034 
00035     ##################
00036     ## Diff Command ##
00037     ##################
00038     if options.command == 'diff':
00039         if len (args) >= 3:
00040             raise RuntimeError, "Can not output to file with '--diff' option.  The output is not standard JSON."
00041         firstOnly  = alphaList - betaList
00042         secondOnly = betaList  - alphaList
00043         if not firstOnly and not secondOnly:
00044             print "Files '%s' and '%s' are the same." % (args[0], args[1])
00045             sys.exit()
00046         print "'%s'-only lumis:" % args[0]
00047         if firstOnly:
00048             print firstOnly
00049         else:
00050             print "None"
00051         print "\n'%s'-only lumis:" % args[1]
00052         if secondOnly:
00053             print secondOnly
00054         else:
00055             print "None"
00056         sys.exit()
00057     
00058     ########################
00059     ## All other commands ##
00060     ########################
00061     if options.command == 'and':
00062         outputList = alphaList & betaList
00063 
00064     if options.command == 'or':
00065         outputList = alphaList | betaList
00066 
00067     if options.command == 'sub':
00068         outputList = alphaList - betaList
00069 
00070     if len (args) >= 3:
00071         outputList.writeJSON (args[2])
00072     else:
00073         # print to screen
00074         print outputList
00075