ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/jsondiff.py
Revision: 1.4
Committed: Fri Feb 8 20:21:58 2013 UTC (12 years, 2 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +45 -92 lines
Log Message:
simplified jsondiff

File Contents

# Content
1 #!/usr/bin/env python
2
3 import re
4 import sys
5 from optparse import OptionParser
6
7 parser = OptionParser()
8 parser.add_option("-r", "--range", dest="range", help="run range", metavar="BEGIN-END")
9 parser.add_option("-i", "--intersection", action="store_true", dest="intersection", help="show intersections")
10 parser.add_option("-u", "--union", action="store_true", dest="union", help="show union")
11
12 (options, args) = parser.parse_args()
13
14 begin = 0
15 end = 1000000
16 if options.range:
17 m = re.match('([0-9]+)-([0-9]+)', options.range)
18 begin = int(m.group(1))
19 end = int(m.group(2))
20 if begin >= end:
21 raise
22
23 new = file(args[0]).read()
24 old = file(args[1]).read()
25
26 runPat = re.compile(r'"([0-9]+)": \[\s*((?:\[[0-9]+,\s*[0-9]+\],?\s*)+)\]')
27 lumiPat = re.compile(r'\[([0-9]+),\s*([0-9]+)\]')
28
29 oldList = dict()
30 newList = dict()
31
32 for sourceDest in ((old, oldList), (new, newList)):
33 source = sourceDest[0]
34 dest = sourceDest[1]
35
36 runBlocks = runPat.findall(source)
37 for runBlock in runBlocks:
38 run = int(runBlock[0])
39 if run < begin or run > end:
40 continue
41
42 lumiBlocks = lumiPat.findall(runBlock[1])
43 boundPairs = []
44 if run in dest:
45 boundPairs = dest[run]
46
47 for lumiBlock in lumiBlocks:
48 boundPairs.append((int(lumiBlock[0]), int(lumiBlock[1])))
49
50 dest[run] = boundPairs
51
52 jsonTxt = '{'
53
54 runs = set(newList.keys())
55 if options.union:
56 runs |= set(oldList.keys())
57
58 runsList = list(runs)
59 runsList.sort()
60
61 for run in runsList:
62 allOldLumis = set()
63 allNewLumis = set()
64
65 if run in oldList:
66 for boundPair in oldList[run]:
67 allOldLumis |= set(range(boundPair[0], boundPair[1] + 1))
68
69 if run in newList:
70 for boundPair in newList[run]:
71 allNewLumis |= set(range(boundPair[0], boundPair[1] + 1))
72
73 if options.union:
74 allLumis = allNewLumis.union(allOldLumis)
75 elif options.intersection:
76 allLumis = allNewLumis.intersection(allOldLumis)
77 else:
78 allLumis = allNewLumis.difference(allOldLumis)
79
80 if len(allLumis) == 0:
81 continue
82
83 lumiList = list(allLumis)
84 lumiList.sort()
85
86 jsonTxt += '"' + str(run) + '": ['
87
88 beginOfBlock = lumiList[0]
89 currentLumi = lumiList[0]
90 for i in range(1, len(lumiList)):
91 if lumiList[i] != currentLumi + 1:
92 jsonTxt += '[' + str(beginOfBlock) + ', ' + str(currentLumi) + '], '
93 beginOfBlock = lumiList[i]
94
95 currentLumi = lumiList[i]
96
97 jsonTxt += '[' + str(beginOfBlock) + ', ' + str(currentLumi) + ']], '
98
99 jsonTxt = jsonTxt.rstrip(', ')
100 jsonTxt += '}'
101
102 print jsonTxt