ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/jsondiff.py
Revision: 1.1
Committed: Wed Oct 17 14:46:33 2012 UTC (12 years, 6 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 import re
2 import sys
3 from optparse import OptionParser
4
5 parser = OptionParser()
6 parser.add_option("-r", "--range", dest="range", help="run range", metavar="BEGIN-END")
7
8 (options, args) = parser.parse_args()
9
10 begin = 0
11 end = 1000000
12 if options.range:
13 m = re.match('([0-9]+)-([0-9]+)', options.range)
14 begin = int(m.group(1))
15 end = int(m.group(2))
16 if begin >= end:
17 raise
18
19 new = file(args[0]).read()
20 old = file(args[1]).read()
21
22 runPat = re.compile(r'"([0-9]+)": \[\s*((?:\[[0-9]+,\s*[0-9]+\],?\s*)+)\]')
23 lumiPat = re.compile(r'\[([0-9]+),\s*([0-9]+)\]')
24
25 oldList = dict()
26 newList = dict()
27
28 for sourceDest in ((old, oldList), (new, newList)):
29 runBlocks = runPat.findall(sourceDest[0])
30 for runBlock in runBlocks:
31 run = int(runBlock[0])
32 if run < begin or run > end:
33 continue
34
35 lumiBlocks = lumiPat.findall(runBlock[1])
36 beginEnds = []
37 for lumiBlock in lumiBlocks:
38 beginEnds.append((int(lumiBlock[0]), int(lumiBlock[1])))
39
40 sourceDest[1][int(runBlock[0])] = beginEnds
41
42 jsonTxt = '{'
43
44 runs = newList.keys()
45 runs.sort()
46
47 for run in runs:
48 lumis = newList[run]
49 lumisToWrite = []
50
51 if run not in oldList or len(oldList[run]) == 0:
52 lumisToWrite = lumis
53 else:
54 allOldLumis = []
55 for beginEndPair in oldList[run]:
56 allOldLumis += range(beginEndPair[0], beginEndPair[1] + 1)
57
58 lumisNotInOld = []
59 for beginEndPair in lumis:
60 for l in range(beginEndPair[0], beginEndPair[1] + 1):
61 if l not in allOldLumis:
62 lumisNotInOld.append(l)
63
64 if len(lumisNotInOld) == 0:
65 continue
66
67 lumisNotInOld.sort()
68 beginOfBlock = lumisNotInOld[0]
69 currentLumi = lumisNotInOld[0]
70 for i in range(1, len(lumisNotInOld)):
71 if lumisNotInOld[i] != currentLumi + 1:
72 lumisToWrite.append((beginOfBlock, currentLumi))
73 beginOfBlock = lumisNotInOld[i]
74
75 currentLumi = lumisNotInOld[i]
76
77 if len(lumisToWrite) > 0:
78 jsonTxt += '"' + str(run) + '": ['
79 for lumiPair in lumisToWrite:
80 jsonTxt += '[' + str(lumiPair[0]) + ', ' + str(lumiPair[1]) + '], '
81 jsonTxt = jsonTxt.rstrip(', ')
82 jsonTxt += '], '
83
84 jsonTxt = jsonTxt.rstrip(', ')
85 jsonTxt += '}'
86
87 print jsonTxt