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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Crocodile HTML output."""
import os
import shutil
import time
import xml.dom
class CrocHtmlError(Exception):
"""Coverage HTML error."""
class HtmlElement(object):
"""Node in a HTML file."""
def __init__(self, doc, element):
"""Constructor.
Args:
doc: XML document object.
element: XML element.
"""
self.doc = doc
self.element = element
def E(self, name, **kwargs):
"""Adds a child element.
Args:
name: Name of element.
kwargs: Attributes for element. To use an attribute which is a python
reserved word (i.e. 'class'), prefix the attribute name with 'e_'.
Returns:
The child element.
"""
he = HtmlElement(self.doc, self.doc.createElement(name))
element = he.element
self.element.appendChild(element)
for k, v in kwargs.iteritems():
if k.startswith('e_'):
# Remove prefix
element.setAttribute(k[2:], str(v))
else:
element.setAttribute(k, str(v))
return he
def Text(self, text):
"""Adds a text node.
Args:
text: Text to add.
Returns:
self.
"""
t = self.doc.createTextNode(str(text))
self.element.appendChild(t)
return self
class HtmlFile(object):
"""HTML file."""
def __init__(self, xml_impl, filename):
"""Constructor.
Args:
xml_impl: DOMImplementation to use to create document.
filename: Path to file.
"""
self.xml_impl = xml_impl
doctype = xml_impl.createDocumentType(
'HTML', '-//W3C//DTD HTML 4.01//EN',
'http://www.w3.org/TR/html4/strict.dtd')
self.doc = xml_impl.createDocument(None, 'html', doctype)
self.filename = filename
# Create head and body elements
root = HtmlElement(self.doc, self.doc.documentElement)
self.head = root.E('head')
self.body = root.E('body')
def Write(self, cleanup=True):
"""Writes the file.
Args:
cleanup: If True, calls unlink() on the internal xml document. This
frees up memory, but means that you can't use this file for anything
else.
"""
f = open(self.filename, 'wt')
self.doc.writexml(f, encoding='UTF-8')
f.close()
if cleanup:
self.doc.unlink()
# Prevent future uses of the doc now that we've unlinked it
self.doc = None
#------------------------------------------------------------------------------
COV_TYPE_STRING = {None: 'm', 0: 'i', 1: 'E', 2: ' '}
COV_TYPE_CLASS = {None: 'missing', 0: 'instr', 1: 'covered', 2: ''}
class CrocHtml(object):
"""Crocodile HTML output class."""
def __init__(self, cov, output_root, base_url=None):
"""Constructor."""
self.cov = cov
self.output_root = output_root
self.base_url = base_url
self.xml_impl = xml.dom.getDOMImplementation()
self.time_string = 'Coverage information generated %s.' % time.asctime()
def CreateHtmlDoc(self, filename, title):
"""Creates a new HTML document.
Args:
filename: Filename to write to, relative to self.output_root.
title: Title of page
Returns:
The document.
"""
f = HtmlFile(self.xml_impl, self.output_root + '/' + filename)
f.head.E('title').Text(title)
if self.base_url:
css_href = self.base_url + 'croc.css'
base_href = self.base_url + os.path.dirname(filename)
if not base_href.endswith('/'):
base_href += '/'
f.head.E('base', href=base_href)
else:
css_href = '../' * (len(filename.split('/')) - 1) + 'croc.css'
f.head.E('link', rel='stylesheet', type='text/css', href=css_href)
return f
def AddCaptionForFile(self, body, path):
"""Adds a caption for the file, with links to each parent dir.
Args:
body: Body elemement.
path: Path to file.
"""
# This is slightly different that for subdir, because it needs to have a
# link to the current directory's index.html.
hdr = body.E('h2')
hdr.Text('Coverage for ')
dirs = [''] + path.split('/')
num_dirs = len(dirs)
for i in range(num_dirs - 1):
hdr.E('a', href=(
'../' * (num_dirs - i - 2) + 'index.html')).Text(dirs[i] + '/')
hdr.Text(dirs[-1])
def AddCaptionForSubdir(self, body, path):
"""Adds a caption for the subdir, with links to each parent dir.
Args:
body: Body elemement.
path: Path to subdir.
"""
# Link to parent dirs
hdr = body.E('h2')
hdr.Text('Coverage for ')
dirs = [''] + path.split('/')
num_dirs = len(dirs)
for i in range(num_dirs - 1):
hdr.E('a', href=(
'../' * (num_dirs - i - 1) + 'index.html')).Text(dirs[i] + '/')
hdr.Text(dirs[-1] + '/')
def AddSectionHeader(self, table, caption, itemtype, is_file=False):
"""Adds a section header to the coverage table.
Args:
table: Table to add rows to.
caption: Caption for section, if not None.
itemtype: Type of items in this section, if not None.
is_file: Are items in this section files?
"""
if caption is not None:
table.E('tr').E('th', e_class='secdesc', colspan=8).Text(caption)
sec_hdr = table.E('tr')
if itemtype is not None:
sec_hdr.E('th', e_class='section').Text(itemtype)
sec_hdr.E('th', e_class='section').Text('Coverage')
sec_hdr.E('th', e_class='section', colspan=3).Text(
'Lines executed / instrumented / missing')
graph = sec_hdr.E('th', e_class='section')
graph.E('span', style='color:#00FF00').Text('exe')
graph.Text(' / ')
graph.E('span', style='color:#FFFF00').Text('inst')
graph.Text(' / ')
graph.E('span', style='color:#FF0000').Text('miss')
if is_file:
sec_hdr.E('th', e_class='section').Text('Language')
sec_hdr.E('th', e_class='section').Text('Group')
else:
sec_hdr.E('th', e_class='section', colspan=2)
def AddItem(self, table, itemname, stats, attrs, link=None):
"""Adds a bar graph to the element. This is a series of <td> elements.
Args:
table: Table to add item to.
itemname: Name of item.
stats: Stats object.
attrs: Attributes dictionary; if None, no attributes will be printed.
link: Destination for itemname hyperlink, if not None.
"""
row = table.E('tr')
# Add item name
if itemname is not None:
item_elem = row.E('td')
if link is not None:
item_elem = item_elem.E('a', href=link)
item_elem.Text(itemname)
# Get stats
stat_exe = stats.get('lines_executable', 0)
stat_ins = stats.get('lines_instrumented', 0)
stat_cov = stats.get('lines_covered', 0)
percent = row.E('td')
# Add text
row.E('td', e_class='number').Text(stat_cov)
row.E('td', e_class='number').Text(stat_ins)
row.E('td', e_class='number').Text(stat_exe - stat_ins)
# Add percent and graph; only fill in if there's something in there
graph = row.E('td', e_class='graph', width=100)
if stat_exe:
percent_cov = 100.0 * stat_cov / stat_exe
percent_ins = 100.0 * stat_ins / stat_exe
# Color percent based on thresholds
percent.Text('%.1f%%' % percent_cov)
if percent_cov >= 80:
percent.element.setAttribute('class', 'high_pct')
elif percent_cov >= 60:
percent.element.setAttribute('class', 'mid_pct')
else:
percent.element.setAttribute('class', 'low_pct')
# Graphs use integer values
percent_cov = int(percent_cov)
percent_ins = int(percent_ins)
graph.Text('.')
graph.E('span', style='padding-left:%dpx' % percent_cov,
e_class='g_covered')
graph.E('span', style='padding-left:%dpx' % (percent_ins - percent_cov),
e_class='g_instr')
graph.E('span', style='padding-left:%dpx' % (100 - percent_ins),
e_class='g_missing')
if attrs:
row.E('td', e_class='stat').Text(attrs.get('language'))
row.E('td', e_class='stat').Text(attrs.get('group'))
else:
row.E('td', colspan=2)
def WriteFile(self, cov_file):
"""Writes the HTML for a file.
Args:
cov_file: croc.CoveredFile to write.
"""
print ' ' + cov_file.filename
title = 'Coverage for ' + cov_file.filename
f = self.CreateHtmlDoc(cov_file.filename + '.html', title)
body = f.body
# Write header section
self.AddCaptionForFile(body, cov_file.filename)
# Summary for this file
table = body.E('table')
self.AddSectionHeader(table, None, None, is_file=True)
self.AddItem(table, None, cov_file.stats, cov_file.attrs)
body.E('h2').Text('Line-by-line coverage:')
# Print line-by-line coverage
if cov_file.local_path:
code_table = body.E('table').E('tr').E('td').E('pre')
flines = open(cov_file.local_path, 'rt')
lineno = 0
for line in flines:
lineno += 1
line_cov = cov_file.lines.get(lineno, 2)
e_class = COV_TYPE_CLASS.get(line_cov)
code_table.E('span', e_class=e_class).Text('%4d %s : %s\n' % (
lineno,
COV_TYPE_STRING.get(line_cov),
line.rstrip()
))
else:
body.Text('Line-by-line coverage not available. Make sure the directory'
' containing this file has been scanned via ')
body.E('B').Text('add_files')
body.Text(' in a configuration file, or the ')
body.E('B').Text('--addfiles')
body.Text(' command line option.')
# TODO: if file doesn't have a local path, try to find it by
# reverse-mapping roots and searching for the file.
body.E('p', e_class='time').Text(self.time_string)
f.Write()
def WriteSubdir(self, cov_dir):
"""Writes the index.html for a subdirectory.
Args:
cov_dir: croc.CoveredDir to write.
"""
print ' ' + cov_dir.dirpath + '/'
# Create the subdir if it doesn't already exist
subdir = self.output_root + '/' + cov_dir.dirpath
if not os.path.exists(subdir):
os.mkdir(subdir)
if cov_dir.dirpath:
title = 'Coverage for ' + cov_dir.dirpath + '/'
f = self.CreateHtmlDoc(cov_dir.dirpath + '/index.html', title)
else:
title = 'Coverage summary'
f = self.CreateHtmlDoc('index.html', title)
body = f.body
dirs = [''] + cov_dir.dirpath.split('/')
num_dirs = len(dirs)
sort_jsfile = '../' * (num_dirs - 1) + 'sorttable.js'
script = body.E('script', src=sort_jsfile)
body.E('/script')
# Write header section
if cov_dir.dirpath:
self.AddCaptionForSubdir(body, cov_dir.dirpath)
else:
body.E('h2').Text(title)
table = body.E('table', e_class='sortable')
table.E('h3').Text('Coverage by Group')
# Coverage by group
self.AddSectionHeader(table, None, 'Group')
for group in sorted(cov_dir.stats_by_group):
self.AddItem(table, group, cov_dir.stats_by_group[group], None)
# List subdirs
if cov_dir.subdirs:
table = body.E('table', e_class='sortable')
table.E('h3').Text('Subdirectories')
self.AddSectionHeader(table, None, 'Subdirectory')
for d in sorted(cov_dir.subdirs):
self.AddItem(table, d + '/', cov_dir.subdirs[d].stats_by_group['all'],
None, link=d + '/index.html')
# List files
if cov_dir.files:
table = body.E('table', e_class='sortable')
table.E('h3').Text('Files in This Directory')
self.AddSectionHeader(table, None, 'Filename',
is_file=True)
for filename in sorted(cov_dir.files):
cov_file = cov_dir.files[filename]
self.AddItem(table, filename, cov_file.stats, cov_file.attrs,
link=filename + '.html')
body.E('p', e_class='time').Text(self.time_string)
f.Write()
def WriteRoot(self):
"""Writes the files in the output root."""
# Find ourselves
src_dir = os.path.split(self.WriteRoot.func_code.co_filename)[0]
# Files to copy into output root
copy_files = ['croc.css']
# Third_party files to copy into output root
third_party_files = ['sorttable.js']
# Copy files from our directory into the output directory
for copy_file in copy_files:
print ' Copying %s' % copy_file
shutil.copyfile(os.path.join(src_dir, copy_file),
os.path.join(self.output_root, copy_file))
# Copy third party files from third_party directory into
# the output directory
src_dir = os.path.join(src_dir, 'third_party')
for third_party_file in third_party_files:
print ' Copying %s' % third_party_file
shutil.copyfile(os.path.join(src_dir, third_party_file),
os.path.join(self.output_root, third_party_file))
def Write(self):
"""Writes HTML output."""
print 'Writing HTML to %s...' % self.output_root
# Loop through the tree and write subdirs, breadth-first
# TODO: switch to depth-first and sort values - makes nicer output?
todo = [self.cov.tree]
while todo:
cov_dir = todo.pop(0)
# Append subdirs to todo list
todo += cov_dir.subdirs.values()
# Write this subdir
self.WriteSubdir(cov_dir)
# Write files in this subdir
for cov_file in cov_dir.files.itervalues():
self.WriteFile(cov_file)
# Write files in root directory
self.WriteRoot()
|