forked from jcranmer/mozilla-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_ui.py
executable file
·347 lines (318 loc) · 13.1 KB
/
make_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/python
import json
import os
import shutil
import sys
from ccov import CoverageData
def main(argv):
from optparse import OptionParser
usage = "Usage: %prog [options] inputfile(s)"
o = OptionParser(usage)
o.add_option('-o', '--output', dest="outdir",
help="Directory to store all HTML files", metavar="DIRECTORY")
o.add_option('-s', '--source-dir', dest="basedir",
help="Base directory for source code", metavar="DIRECTORY")
o.add_option('-l', '--limits', dest="limits",
help="Custom limits for medium,high coverage")
(opts, args) = o.parse_args(argv)
if opts.outdir is None:
print("Need to pass in -o!")
sys.exit(1)
if len(args) < 2:
print("Need to specify at least one input file!")
sys.exit(1)
# Add in all the data
cov = CoverageData()
for lcovFile in args[1:]:
print("Reading coverage data from", lcovFile)
cov.addFromLcovFile(open(lcovFile, 'r'))
# Make the output directory
if not os.path.exists(opts.outdir):
os.makedirs(opts.outdir)
print ('Building UI...')
builder = UiBuilder(cov, opts.outdir, opts.basedir, opts.limits)
builder.makeStaticOutput()
builder.makeDynamicOutput()
class UiBuilder(object):
def __init__(self, covdata, outdir, basedir, limits):
self.data = covdata
self.flatdata = self.data.getFlatData()
self.outdir = outdir
self.uidir = os.path.dirname(__file__)
self.basedir = basedir
self.limits = limits
self.relsrc = None
self.tests = []
def _loadGlobalData(self):
json_data = self.buildJSONData(self.flatdata)
# Make the root node be the lowest path where filenames diverge. This
# generally works, assuming that things like /usr/include/ are removed
# from the coverage files before hand.
self.relsrc = ''
while len(json_data["files"]) == 1 and len(json_data["files"][0]["files"]):
json_data = json_data["files"][0]
if 'name' in json_data:
self.relsrc += '/' + json_data['name']
self.relsrc = self.relsrc.replace('//', '/')
if self.basedir is None:
self.basedir = self.relsrc
return json_data
def buildJSONData(self, data):
# The output format is a tree structure, where each node looks like:
# { lines: <number of lines in the file/directory>,
# lines-hit: <number of lines that have a count > 0 in file/directory>,
# funcs: <number of functions in the file/directory>,
# funcs-hit: <number of functions that have a count > 0 in file/directory>,
# branches: <number of branches>,
# branches-hit: <number of branches with count > 0>,
# files: [ list of children of this node ],
# name: "local name of the file, not the full path"
# }
default_dict = {"lines": 0, "lines-hit": 0, "funcs": 0, "funcs-hit": 0,
"branches": 0, "branches-hit": 0, "files": []}
json_data = dict(default_dict)
for filename in data:
parts = filename.split('/')
linehit, linecount = 0, 0
fnhit, fncount = 0, 0
brhit, brcount = 0, 0
#linehit, linecount = reduce(lambda x, y: (x[0] + 1, x[1] + (y[1] != 0)),
# data[filename].lines(), (0, 0))
for lno, lc in data[filename].lines():
linehit += lc > 0
linecount += 1
for fname, _, fc in data[filename].functions():
fnhit += fc > 0
fncount += 1
for _, _, _, brinfo in data[filename].branches():
brcount += len(brinfo)
brhit += sum(k != 0 for k in brinfo)
blob = json_data
for component in parts:
blob["lines"] += linecount
blob["lines-hit"] += linehit
blob["funcs"] += fncount
blob["funcs-hit"] += fnhit
blob["branches"] += brcount
blob["branches-hit"] += brhit
for f in blob["files"]:
if f["name"] == component:
blob = f
break
else:
blob["files"].append(default_dict.copy())
blob = blob["files"][-1]
blob["name"] = component
blob["files"] = []
blob["lines"] += linecount
blob["lines-hit"] += linehit
blob["funcs"] += fncount
blob["funcs-hit"] += fnhit
blob["branches"] += brcount
blob["branches-hit"] += brhit
if self.relsrc:
for part in self.relsrc.split('/'):
json_data = json_data['files'][0]
return json_data
def makeStaticOutput(self):
staticdir = os.path.join(self.uidir, "webui")
for static in os.listdir(staticdir):
shutil.copy2(os.path.join(staticdir, static),
os.path.join(self.outdir, static))
def makeDynamicOutput(self):
# Dump out JSON files
json_data = self._loadGlobalData()
if 'all' in self.tests :
json.dump(json_data, open(os.path.join(self.outdir, 'all.json'), 'w'))
for test in self.data.getTests():
small_data = self.data.getTestData(test)
if len(small_data) == 0:
continue
self.tests.append(test)
test_data = self.buildJSONData(small_data)
json.dump(test_data,
open(os.path.join(self.outdir, test + '.json'), 'w'))
self.tests.sort()
covtemp = self._readTemplate("coverage.html")
with open(os.path.join(self.outdir, "coverage.html"), 'w') as fd:
fd.write(covtemp.substitute({'tests':
'\n'.join(('<option>%s</option>' % t) for t in self.tests)}))
self._makeDirectoryIndex('', json_data)
def _readTemplate(self, name):
from string import Template
templatefile = os.path.join(self.uidir, "uitemplates", name)
fd = open(templatefile, 'r')
try:
template = fd.read()
finally:
fd.close()
return Template(template)
def _makeDirectoryIndex(self, dirname, jsondata):
# Utility method for printing out rows of the table
mediumLimit = 75.0
highLimit = 90.0
if self.limits:
values = self.limits.split(",");
if len(values) == 2:
mediumLimit = float(values[0])
highLimit = float(values[1])
def summary_string(lhs, jsondata):
output = '<tr>'
output += '<td>%s</td>' % lhs
for piece in ['lines', 'funcs', 'branches']:
hit = jsondata[piece + '-hit']
count = jsondata[piece]
if count == 0:
output += '<td>0 / 0</td><td>-</td>'
else:
ratio = 100.0 * hit / count
if ratio < mediumLimit: clazz = "lowcov"
elif ratio < highLimit: clazz = "mediumcov"
else: clazz = "highcov"
output += '<td class="%s">%d / %d</td><td class="%s">%.1f%%</td>' % (
clazz, hit, count, clazz, ratio)
return output + '</tr>'
if dirname:
htmltmp = self._readTemplate('directory.html')
else:
htmltmp = self._readTemplate('root_directory.html')
jsondata['files'].sort(key=lambda x: x['name'])
# Parameters for output
parameters = {}
parameters['directory'] = dirname
if dirname:
parameters['depth'] = '/'.join('..' for x in dirname.split('/'))
else:
parameters['depth'] = '.'
parameters['testoptions'] = '\n'.join(
('<option>%s</option>' % test) for test in self.tests)
from datetime import date
parameters['date'] = date.today().isoformat()
if not dirname:
parameters['reponame'] = os.getcwd()[os.getcwd().rfind('/')+1:len(os.getcwd())]
def htmlname(json):
if len(json['files']) > 0:
return json['name'] + "/index.html"
else:
return json['name'] + '.html'
tablestr = '\n'.join(summary_string(
'<a href="%s">%s</a>' % (htmlname(child), child['name']), child)
for child in jsondata['files'])
parameters['tbody'] = tablestr
parameters['tfoot'] = summary_string('Total', jsondata)
outputdir = os.path.join(self.outdir, dirname)
if not os.path.exists(outputdir):
os.makedirs(outputdir)
fd = open(os.path.join(outputdir, 'index.html'), 'w')
try:
fd.write(htmltmp.substitute(parameters))
finally:
fd.close()
# Recursively build for all files in the directory
for child in jsondata['files']:
if len(child['files']) > 0:
self._makeDirectoryIndex(os.path.join(dirname, child['name']), child)
else:
self._makeFileData(dirname, child['name'], child)
def _makeFileData(self, dirname, filename, jsondata):
# Python 2 / 3 compatibility fix
try:
import html
except ImportError:
import cgi as html
if dirname:
if dirname == 'inc':
htmltmp = self._readTemplate('single_test_file.html')
else:
htmltmp = self._readTemplate('file.html')
else:
htmltmp = self._readTemplate('single_test_file.html')
parameters = {}
parameters['file'] = os.path.join(dirname, filename)
parameters['directory'] = dirname
if dirname:
parameters['depth'] = '/'.join('..' for x in dirname.split('/'))
else:
parameters['depth'] = '.'
parameters['testoptions'] = '\n'.join(
'<option>%s</option>' % s for s in self.tests)
from datetime import date
parameters['date'] = date.today().isoformat()
# Read the input file
srcfile = os.path.join(self.basedir, dirname, filename)
filekey = os.path.join(self.relsrc, dirname, filename)
if not os.path.exists(srcfile):
parameters['tbody'] = (
'<tr><td colspan="5">File could not be found</td></tr>')
parameters['data'] = ''
else:
if sys.version_info[0] == 2:
# Python 2 version of open
with open(srcfile, mode="r") as fd:
srclines = fd.readlines()
else:
with open(srcfile, mode="r", encoding="utf-8", errors="ignore") as fd:
srclines = fd.readlines()
flatdata = self.flatdata[filekey]
del self.flatdata[filekey] # Scavenge memory we don't need anymore.
alldata = self._buildFileJson(flatdata)
outdata = {'all': alldata}
for test in self.tests[1:]:
outdata[test] = self._buildFileJson(
self.data.getFileData(filekey, test))
parameters['data'] = '''var data=%s;''' % json.dumps(outdata)
# Precompute branch data for each line.
brlinedata = {}
for line in range(len(alldata['lines'])):
data = alldata['bcounts'][line]
entries = []
for branch, tdata in data.items():
tentries = ['<span class="%s" title="%d"> %s </span>' % (
"highcov" if count > 0 else "lowcov", count,
"+" if count > 0 else "-") for count in tdata]
tentries[0] = ('<span data-branchid="%d">[' % branch +
tentries[0])
tentries[-1] += ']</span>'
entries.extend(tentries)
# Insert breaks every 8 values to make particularly long strings
# not overflow the browser viewport
for i in range(7, len(entries) - 1, 8):
entries[i] += '<br>'
brlinedata[alldata['lines'][line]] = ''.join(entries)
lineno = 1
outlines = []
linehitdata = dict(flatdata.lines())
for line in srclines:
covstatus = ''
linecount = ''
if lineno in linehitdata:
linecount = str(linehitdata[lineno])
iscov = linecount != '0'
covstatus = ' class="highcov"' if iscov else ' class="lowcov"'
brcount = brlinedata.get(lineno, '')
outlines.append((' <tr%s><td>%d</td>' +
'<td>%s</td><td>%s</td><td>%s</td></tr>\n'
) % (covstatus, lineno, brcount, linecount,
html.escape(line.rstrip())))
lineno += 1
parameters['tbody'] = ''.join(outlines)
outputdir = os.path.join(self.outdir, dirname)
if not os.path.exists(outputdir):
os.makedirs(outputdir)
with open(os.path.join(outputdir, filename + '.html'), 'w') as fd:
fd.write(htmltmp.substitute(parameters))
def _buildFileJson(self, data):
lcs = list(data.lines())
if lcs:
lines, counts = zip(*lcs)
else:
lines, counts = [],[]
brdata = list(data.branches())
brdata.sort()
brlinedata = {}
for line, branchid, ids, brcounts in brdata:
brlinedata.setdefault(line, {})[branchid] = brcounts
flat = [brlinedata.get(l, {}) for l in lines]
return {'lines': lines, 'lcounts': counts, 'bcounts': flat}
if __name__ == '__main__':
main(sys.argv)