ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/jsondiff.py
Revision: 1.2
Committed: Mon Oct 22 12:27:19 2012 UTC (12 years, 6 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +2 -0 lines
Log Message:
making executable

File Contents

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