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("-f", "--file", dest="filename", help="list of jfr files", metavar="FILE")
|
9 |
|
10 |
(options, args) = parser.parse_args()
|
11 |
|
12 |
filenames = []
|
13 |
lines = []
|
14 |
|
15 |
if options.filename:
|
16 |
filelist = file(options.filename, "r")
|
17 |
for f in filelist:
|
18 |
filenames.append(f.strip())
|
19 |
|
20 |
filelist.close()
|
21 |
else:
|
22 |
filenames = args
|
23 |
|
24 |
for f in filenames:
|
25 |
try:
|
26 |
lines += file(f, "r").readlines()
|
27 |
except IOError:
|
28 |
sys.stderr.write("Cannot open " + f + "\n")
|
29 |
|
30 |
runPat = re.compile('\s*<Run[ ]ID[=]"([0-9]+)">')
|
31 |
lumiPat = re.compile('\s*<LumiSection[ ]ID[=]"([0-9]+)"/>')
|
32 |
|
33 |
inRunBlock = False
|
34 |
|
35 |
lumilist = dict()
|
36 |
|
37 |
run = 0
|
38 |
for line in lines:
|
39 |
runMatch = runPat.match(line)
|
40 |
if runMatch:
|
41 |
run = int(runMatch.group(1))
|
42 |
if run not in lumilist:
|
43 |
lumilist[run] = list()
|
44 |
|
45 |
continue
|
46 |
|
47 |
if run != 0:
|
48 |
if '</Run>' in line:
|
49 |
run = 0
|
50 |
continue
|
51 |
|
52 |
lumiMatch = lumiPat.match(line)
|
53 |
if lumiMatch:
|
54 |
lumilist[run].append(int(lumiMatch.group(1)))
|
55 |
|
56 |
|
57 |
jsonTxt = "{"
|
58 |
|
59 |
runs = lumilist.keys()
|
60 |
runs.sort()
|
61 |
|
62 |
for run in runs:
|
63 |
lumis = lumilist[run]
|
64 |
if len(lumis) == 0:
|
65 |
continue
|
66 |
|
67 |
lumis.sort()
|
68 |
|
69 |
jsonTxt += '"' + str(run) + '": [[' + str(lumis[0]) + ', '
|
70 |
|
71 |
l = lumis[0] - 1
|
72 |
for lumi in lumis:
|
73 |
if lumi != l + 1:
|
74 |
jsonTxt += str(l) + '], [' + str(lumi) + ', '
|
75 |
|
76 |
l = lumi
|
77 |
|
78 |
jsonTxt += str(lumis[len(lumis) - 1]) + ']], '
|
79 |
|
80 |
jsonTxt = jsonTxt.rstrip(', ')
|
81 |
jsonTxt += "}"
|
82 |
|
83 |
print jsonTxt
|