ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/jsondiff.py
Revision: 1.3
Committed: Mon Nov 12 16:59:58 2012 UTC (12 years, 5 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +83 -23 lines
Log Message:
*** empty log message ***

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("-a", "--overlap", action="store_true", dest="overlap", help="show overlaps")
10 parser.add_option("-o", "--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 beginEnds = []
44 if run in dest:
45 beginEnds = dest[run]
46
47 for lumiBlock in lumiBlocks:
48 beginEnds.append((int(lumiBlock[0]), int(lumiBlock[1])))
49
50 dest[run] = beginEnds
51
52 jsonTxt = '{'
53
54 runs = newList.keys()
55 runs.sort()
56
57 for run in runs:
58 lumis = newList[run]
59 lumisToWrite = []
60
61 if options.overlap:
62 if run not in oldList or len(oldList[run]) == 0:
63 continue
64 else:
65 allOldLumis = []
66 for beginEndPair in oldList[run]:
67 allOldLumis += range(beginEndPair[0], beginEndPair[1] + 1)
68
69 lumisInOld = []
70 for beginEndPair in lumis:
71 for l in range(beginEndPair[0], beginEndPair[1] + 1):
72 if l in allOldLumis:
73 lumisInOld.append(l)
74
75 lumisInOld.sort()
76 beginOfBlock = lumisInOld[0]
77 currentLumi = lumisInOld[0]
78 for i in range(1, len(lumisInOld)):
79 if lumisInOld[i] != currentLumi + 1:
80 lumisToWrite.append((beginOfBlock, currentLumi))
81 beginOfBlock = lumisInOld[i]
82
83 currentLumi = lumisInOld[i]
84
85 lumisToWrite.append((beginOfBlock, currentLumi))
86
87 elif options.union:
88 allLumis = []
89 for beginEndPair in lumis:
90 allLumis += range(beginEndPair[0], beginEndPair[1] + 1)
91
92 if run in oldList:
93 for beginEndPair in oldList[run]:
94 for l in range(beginEndPair[0], beginEndPair[1] + 1):
95 if l not in allLumis:
96 allLumis.append(l)
97
98 allLumis.sort()
99 beginOfBlock = allLumis[0]
100 currentLumi = allLumis[0]
101 for i in range(1, len(allLumis)):
102 if allLumis[i] != currentLumi + 1:
103 lumisToWrite.append((beginOfBlock, currentLumi))
104 beginOfBlock = allLumis[i]
105
106 currentLumi = allLumis[i]
107
108 lumisToWrite.append((beginOfBlock, currentLumi))
109
110 else:
111 if run not in oldList or len(oldList[run]) == 0:
112 lumisToWrite = lumis
113 else:
114 allOldLumis = []
115 for beginEndPair in oldList[run]:
116 allOldLumis += range(beginEndPair[0], beginEndPair[1] + 1)
117
118 lumisNotInOld = []
119 for beginEndPair in lumis:
120 for l in range(beginEndPair[0], beginEndPair[1] + 1):
121 if l not in allOldLumis:
122 lumisNotInOld.append(l)
123
124 if len(lumisNotInOld) == 0:
125 continue
126
127 lumisNotInOld.sort()
128 beginOfBlock = lumisNotInOld[0]
129 currentLumi = lumisNotInOld[0]
130 for i in range(1, len(lumisNotInOld)):
131 if lumisNotInOld[i] != currentLumi + 1:
132 lumisToWrite.append((beginOfBlock, currentLumi))
133 beginOfBlock = lumisNotInOld[i]
134
135 currentLumi = lumisNotInOld[i]
136
137 lumisToWrite.append((beginOfBlock, currentLumi))
138
139 if len(lumisToWrite) > 0:
140 jsonTxt += '"' + str(run) + '": ['
141 for lumiPair in lumisToWrite:
142 jsonTxt += '[' + str(lumiPair[0]) + ', ' + str(lumiPair[1]) + '], '
143 jsonTxt = jsonTxt.rstrip(', ')
144 jsonTxt += '], '
145
146 jsonTxt = jsonTxt.rstrip(', ')
147 jsonTxt += '}'
148
149 print jsonTxt