1 """HTML reporting for Coverage.""" |
1 """HTML reporting for Coverage.""" |
2 |
2 |
3 import os, re, shutil |
3 import os, re, shutil, sys |
4 |
4 |
5 from . import __url__, __version__ # pylint: disable-msg=W0611 |
5 from . import coverage |
6 from .phystokens import source_token_lines |
6 from .backward import pickle |
|
7 from .misc import CoverageException, Hasher |
|
8 from .phystokens import source_token_lines, source_encoding |
7 from .report import Reporter |
9 from .report import Reporter |
|
10 from .results import Numbers |
8 from .templite import Templite |
11 from .templite import Templite |
9 |
12 |
10 # Disable pylint msg W0612, because a bunch of variables look unused, but |
13 |
11 # they're accessed in a templite context via locals(). |
14 # Static files are looked for in a list of places. |
12 # pylint: disable-msg=W0612 |
15 STATIC_PATH = [ |
13 |
16 # The place Debian puts system Javascript libraries. |
14 def data_filename(fname): |
17 "/usr/share/javascript", |
15 """Return the path to a data file of ours.""" |
18 |
16 return os.path.join(os.path.split(__file__)[0], fname) |
19 # Our htmlfiles directory. |
|
20 os.path.join(os.path.dirname(__file__), "htmlfiles"), |
|
21 ] |
|
22 |
|
23 def data_filename(fname, pkgdir=""): |
|
24 """Return the path to a data file of ours. |
|
25 |
|
26 The file is searched for on `STATIC_PATH`, and the first place it's found, |
|
27 is returned. |
|
28 |
|
29 Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir` |
|
30 is provided, at that subdirectory. |
|
31 |
|
32 """ |
|
33 for static_dir in STATIC_PATH: |
|
34 static_filename = os.path.join(static_dir, fname) |
|
35 if os.path.exists(static_filename): |
|
36 return static_filename |
|
37 if pkgdir: |
|
38 static_filename = os.path.join(static_dir, pkgdir, fname) |
|
39 if os.path.exists(static_filename): |
|
40 return static_filename |
|
41 raise CoverageException("Couldn't find static file %r" % fname) |
|
42 |
17 |
43 |
18 def data(fname): |
44 def data(fname): |
19 """Return the contents of a data file of ours.""" |
45 """Return the contents of a data file of ours.""" |
20 return open(data_filename(fname)).read() |
46 data_file = open(data_filename(fname)) |
|
47 try: |
|
48 return data_file.read() |
|
49 finally: |
|
50 data_file.close() |
21 |
51 |
22 |
52 |
23 class HtmlReporter(Reporter): |
53 class HtmlReporter(Reporter): |
24 """HTML reporting.""" |
54 """HTML reporting.""" |
25 |
55 |
26 def __init__(self, coverage, ignore_errors=False): |
56 # These files will be copied from the htmlfiles dir to the output dir. |
27 super(HtmlReporter, self).__init__(coverage, ignore_errors) |
57 STATIC_FILES = [ |
|
58 ("style.css", ""), |
|
59 ("jquery.min.js", "jquery"), |
|
60 ("jquery.hotkeys.js", "jquery-hotkeys"), |
|
61 ("jquery.isonscreen.js", "jquery-isonscreen"), |
|
62 ("jquery.tablesorter.min.js", "jquery-tablesorter"), |
|
63 ("coverage_html.js", ""), |
|
64 ("keybd_closed.png", ""), |
|
65 ("keybd_open.png", ""), |
|
66 ] |
|
67 |
|
68 def __init__(self, cov, config): |
|
69 super(HtmlReporter, self).__init__(cov, config) |
28 self.directory = None |
70 self.directory = None |
29 self.source_tmpl = Templite(data("htmlfiles/pyfile.html"), globals()) |
71 self.template_globals = { |
|
72 'escape': escape, |
|
73 'title': self.config.html_title, |
|
74 '__url__': coverage.__url__, |
|
75 '__version__': coverage.__version__, |
|
76 } |
|
77 self.source_tmpl = Templite( |
|
78 data("pyfile.html"), self.template_globals |
|
79 ) |
|
80 |
|
81 self.coverage = cov |
30 |
82 |
31 self.files = [] |
83 self.files = [] |
32 self.arcs = coverage.data.has_arcs() |
84 self.arcs = self.coverage.data.has_arcs() |
33 |
85 self.status = HtmlStatus() |
34 def report(self, morfs, directory, omit_prefixes=None): |
86 self.extra_css = None |
|
87 self.totals = Numbers() |
|
88 |
|
89 def report(self, morfs): |
35 """Generate an HTML report for `morfs`. |
90 """Generate an HTML report for `morfs`. |
36 |
91 |
37 `morfs` is a list of modules or filenames. `directory` is where to put |
92 `morfs` is a list of modules or filenames. |
38 the HTML files. `omit_prefixes` is a list of strings, prefixes of |
|
39 modules to omit from the report. |
|
40 |
93 |
41 """ |
94 """ |
42 assert directory, "must provide a directory for html reporting" |
95 assert self.config.html_dir, "must give a directory for html reporting" |
|
96 |
|
97 # Read the status data. |
|
98 self.status.read(self.config.html_dir) |
|
99 |
|
100 # Check that this run used the same settings as the last run. |
|
101 m = Hasher() |
|
102 m.update(self.config) |
|
103 these_settings = m.digest() |
|
104 if self.status.settings_hash() != these_settings: |
|
105 self.status.reset() |
|
106 self.status.set_settings_hash(these_settings) |
|
107 |
|
108 # The user may have extra CSS they want copied. |
|
109 if self.config.extra_css: |
|
110 self.extra_css = os.path.basename(self.config.extra_css) |
43 |
111 |
44 # Process all the files. |
112 # Process all the files. |
45 self.report_files(self.html_file, morfs, directory, omit_prefixes) |
113 self.report_files(self.html_file, morfs, self.config.html_dir) |
|
114 |
|
115 if not self.files: |
|
116 raise CoverageException("No data to report.") |
46 |
117 |
47 # Write the index file. |
118 # Write the index file. |
48 self.index_file() |
119 self.index_file() |
49 |
120 |
50 # Create the once-per-directory files. |
121 self.make_local_static_report_files() |
51 for static in [ |
122 |
52 "style.css", "coverage_html.js", |
123 return self.totals.pc_covered |
53 "jquery-1.3.2.min.js", "jquery.tablesorter.min.js" |
124 |
54 ]: |
125 def make_local_static_report_files(self): |
|
126 """Make local instances of static files for HTML report.""" |
|
127 # The files we provide must always be copied. |
|
128 for static, pkgdir in self.STATIC_FILES: |
55 shutil.copyfile( |
129 shutil.copyfile( |
56 data_filename("htmlfiles/" + static), |
130 data_filename(static, pkgdir), |
57 os.path.join(directory, static) |
131 os.path.join(self.directory, static) |
58 ) |
132 ) |
|
133 |
|
134 # The user may have extra CSS they want copied. |
|
135 if self.extra_css: |
|
136 shutil.copyfile( |
|
137 self.config.extra_css, |
|
138 os.path.join(self.directory, self.extra_css) |
|
139 ) |
|
140 |
|
141 def write_html(self, fname, html): |
|
142 """Write `html` to `fname`, properly encoded.""" |
|
143 fout = open(fname, "wb") |
|
144 try: |
|
145 fout.write(html.encode('ascii', 'xmlcharrefreplace')) |
|
146 finally: |
|
147 fout.close() |
|
148 |
|
149 def file_hash(self, source, cu): |
|
150 """Compute a hash that changes if the file needs to be re-reported.""" |
|
151 m = Hasher() |
|
152 m.update(source) |
|
153 self.coverage.data.add_to_hash(cu.filename, m) |
|
154 return m.digest() |
59 |
155 |
60 def html_file(self, cu, analysis): |
156 def html_file(self, cu, analysis): |
61 """Generate an HTML file for one source file.""" |
157 """Generate an HTML file for one source file.""" |
62 |
158 source_file = cu.source_file() |
63 source = cu.source_file().read() |
159 try: |
64 |
160 source = source_file.read() |
|
161 finally: |
|
162 source_file.close() |
|
163 |
|
164 # Find out if the file on disk is already correct. |
|
165 flat_rootname = cu.flat_rootname() |
|
166 this_hash = self.file_hash(source, cu) |
|
167 that_hash = self.status.file_hash(flat_rootname) |
|
168 if this_hash == that_hash: |
|
169 # Nothing has changed to require the file to be reported again. |
|
170 self.files.append(self.status.index_info(flat_rootname)) |
|
171 return |
|
172 |
|
173 self.status.set_file_hash(flat_rootname, this_hash) |
|
174 |
|
175 # If need be, determine the encoding of the source file. We use it |
|
176 # later to properly write the HTML. |
|
177 if sys.version_info < (3, 0): |
|
178 encoding = source_encoding(source) |
|
179 # Some UTF8 files have the dreaded UTF8 BOM. If so, junk it. |
|
180 if encoding.startswith("utf-8") and source[:3] == "\xef\xbb\xbf": |
|
181 source = source[3:] |
|
182 encoding = "utf-8" |
|
183 |
|
184 # Get the numbers for this file. |
65 nums = analysis.numbers |
185 nums = analysis.numbers |
66 |
186 |
67 missing_branch_arcs = analysis.missing_branch_arcs() |
187 if self.arcs: |
68 n_par = 0 # accumulated below. |
188 missing_branch_arcs = analysis.missing_branch_arcs() |
69 arcs = self.arcs |
|
70 |
189 |
71 # These classes determine which lines are highlighted by default. |
190 # These classes determine which lines are highlighted by default. |
72 c_run = " run hide_run" |
191 c_run = "run hide_run" |
73 c_exc = " exc" |
192 c_exc = "exc" |
74 c_mis = " mis" |
193 c_mis = "mis" |
75 c_par = " par" + c_run |
194 c_par = "par " + c_run |
76 |
195 |
77 lines = [] |
196 lines = [] |
78 |
197 |
79 for lineno, line in enumerate(source_token_lines(source)): |
198 for lineno, line in enumerate(source_token_lines(source)): |
80 lineno += 1 # 1-based line numbers. |
199 lineno += 1 # 1-based line numbers. |
81 # Figure out how to mark this line. |
200 # Figure out how to mark this line. |
82 line_class = "" |
201 line_class = [] |
83 annotate_html = "" |
202 annotate_html = "" |
84 annotate_title = "" |
203 annotate_title = "" |
85 if lineno in analysis.statements: |
204 if lineno in analysis.statements: |
86 line_class += " stm" |
205 line_class.append("stm") |
87 if lineno in analysis.excluded: |
206 if lineno in analysis.excluded: |
88 line_class += c_exc |
207 line_class.append(c_exc) |
89 elif lineno in analysis.missing: |
208 elif lineno in analysis.missing: |
90 line_class += c_mis |
209 line_class.append(c_mis) |
91 elif self.arcs and lineno in missing_branch_arcs: |
210 elif self.arcs and lineno in missing_branch_arcs: |
92 line_class += c_par |
211 line_class.append(c_par) |
93 n_par += 1 |
|
94 annlines = [] |
212 annlines = [] |
95 for b in missing_branch_arcs[lineno]: |
213 for b in missing_branch_arcs[lineno]: |
96 if b == -1: |
214 if b < 0: |
97 annlines.append("exit") |
215 annlines.append("exit") |
98 else: |
216 else: |
99 annlines.append(str(b)) |
217 annlines.append(str(b)) |
100 annotate_html = " ".join(annlines) |
218 annotate_html = " ".join(annlines) |
101 if len(annlines) > 1: |
219 if len(annlines) > 1: |
102 annotate_title = "no jumps to these line numbers" |
220 annotate_title = "no jumps to these line numbers" |
103 elif len(annlines) == 1: |
221 elif len(annlines) == 1: |
104 annotate_title = "no jump to this line number" |
222 annotate_title = "no jump to this line number" |
105 elif lineno in analysis.statements: |
223 elif lineno in analysis.statements: |
106 line_class += c_run |
224 line_class.append(c_run) |
107 |
225 |
108 # Build the HTML for the line |
226 # Build the HTML for the line |
109 html = "" |
227 html = [] |
110 for tok_type, tok_text in line: |
228 for tok_type, tok_text in line: |
111 if tok_type == "ws": |
229 if tok_type == "ws": |
112 html += escape(tok_text) |
230 html.append(escape(tok_text)) |
113 else: |
231 else: |
114 tok_html = escape(tok_text) or ' ' |
232 tok_html = escape(tok_text) or ' ' |
115 html += "<span class='%s'>%s</span>" % (tok_type, tok_html) |
233 html.append( |
|
234 "<span class='%s'>%s</span>" % (tok_type, tok_html) |
|
235 ) |
116 |
236 |
117 lines.append({ |
237 lines.append({ |
118 'html': html, |
238 'html': ''.join(html), |
119 'number': lineno, |
239 'number': lineno, |
120 'class': line_class.strip() or "pln", |
240 'class': ' '.join(line_class) or "pln", |
121 'annotate': annotate_html, |
241 'annotate': annotate_html, |
122 'annotate_title': annotate_title, |
242 'annotate_title': annotate_title, |
123 }) |
243 }) |
124 |
244 |
125 # Write the HTML page for this file. |
245 # Write the HTML page for this file. |
126 html_filename = cu.flat_rootname() + ".html" |
246 html = spaceless(self.source_tmpl.render({ |
|
247 'c_exc': c_exc, 'c_mis': c_mis, 'c_par': c_par, 'c_run': c_run, |
|
248 'arcs': self.arcs, 'extra_css': self.extra_css, |
|
249 'cu': cu, 'nums': nums, 'lines': lines, |
|
250 })) |
|
251 |
|
252 if sys.version_info < (3, 0): |
|
253 html = html.decode(encoding) |
|
254 |
|
255 html_filename = flat_rootname + ".html" |
127 html_path = os.path.join(self.directory, html_filename) |
256 html_path = os.path.join(self.directory, html_filename) |
128 html = spaceless(self.source_tmpl.render(locals())) |
257 self.write_html(html_path, html) |
129 fhtml = open(html_path, 'w') |
|
130 fhtml.write(html) |
|
131 fhtml.close() |
|
132 |
258 |
133 # Save this file's information for the index file. |
259 # Save this file's information for the index file. |
134 self.files.append({ |
260 index_info = { |
135 'nums': nums, |
261 'nums': nums, |
136 'par': n_par, |
|
137 'html_filename': html_filename, |
262 'html_filename': html_filename, |
138 'cu': cu, |
263 'name': cu.name, |
139 }) |
264 } |
|
265 self.files.append(index_info) |
|
266 self.status.set_index_info(flat_rootname, index_info) |
140 |
267 |
141 def index_file(self): |
268 def index_file(self): |
142 """Write the index.html file for this report.""" |
269 """Write the index.html file for this report.""" |
143 index_tmpl = Templite(data("htmlfiles/index.html"), globals()) |
270 index_tmpl = Templite( |
144 |
271 data("index.html"), self.template_globals |
145 files = self.files |
272 ) |
146 arcs = self.arcs |
273 |
147 |
274 self.totals = sum([f['nums'] for f in self.files]) |
148 totals = sum([f['nums'] for f in files]) |
275 |
149 |
276 html = index_tmpl.render({ |
150 fhtml = open(os.path.join(self.directory, "index.html"), "w") |
277 'arcs': self.arcs, |
151 fhtml.write(index_tmpl.render(locals())) |
278 'extra_css': self.extra_css, |
152 fhtml.close() |
279 'files': self.files, |
|
280 'totals': self.totals, |
|
281 }) |
|
282 |
|
283 if sys.version_info < (3, 0): |
|
284 html = html.decode("utf-8") |
|
285 self.write_html( |
|
286 os.path.join(self.directory, "index.html"), |
|
287 html |
|
288 ) |
|
289 |
|
290 # Write the latest hashes for next time. |
|
291 self.status.write(self.directory) |
|
292 |
|
293 |
|
294 class HtmlStatus(object): |
|
295 """The status information we keep to support incremental reporting.""" |
|
296 |
|
297 STATUS_FILE = "status.dat" |
|
298 STATUS_FORMAT = 1 |
|
299 |
|
300 def __init__(self): |
|
301 self.reset() |
|
302 |
|
303 def reset(self): |
|
304 """Initialize to empty.""" |
|
305 self.settings = '' |
|
306 self.files = {} |
|
307 |
|
308 def read(self, directory): |
|
309 """Read the last status in `directory`.""" |
|
310 usable = False |
|
311 try: |
|
312 status_file = os.path.join(directory, self.STATUS_FILE) |
|
313 fstatus = open(status_file, "rb") |
|
314 try: |
|
315 status = pickle.load(fstatus) |
|
316 finally: |
|
317 fstatus.close() |
|
318 except (IOError, ValueError): |
|
319 usable = False |
|
320 else: |
|
321 usable = True |
|
322 if status['format'] != self.STATUS_FORMAT: |
|
323 usable = False |
|
324 elif status['version'] != coverage.__version__: |
|
325 usable = False |
|
326 |
|
327 if usable: |
|
328 self.files = status['files'] |
|
329 self.settings = status['settings'] |
|
330 else: |
|
331 self.reset() |
|
332 |
|
333 def write(self, directory): |
|
334 """Write the current status to `directory`.""" |
|
335 status_file = os.path.join(directory, self.STATUS_FILE) |
|
336 status = { |
|
337 'format': self.STATUS_FORMAT, |
|
338 'version': coverage.__version__, |
|
339 'settings': self.settings, |
|
340 'files': self.files, |
|
341 } |
|
342 fout = open(status_file, "wb") |
|
343 try: |
|
344 pickle.dump(status, fout) |
|
345 finally: |
|
346 fout.close() |
|
347 |
|
348 def settings_hash(self): |
|
349 """Get the hash of the coverage.py settings.""" |
|
350 return self.settings |
|
351 |
|
352 def set_settings_hash(self, settings): |
|
353 """Set the hash of the coverage.py settings.""" |
|
354 self.settings = settings |
|
355 |
|
356 def file_hash(self, fname): |
|
357 """Get the hash of `fname`'s contents.""" |
|
358 return self.files.get(fname, {}).get('hash', '') |
|
359 |
|
360 def set_file_hash(self, fname, val): |
|
361 """Set the hash of `fname`'s contents.""" |
|
362 self.files.setdefault(fname, {})['hash'] = val |
|
363 |
|
364 def index_info(self, fname): |
|
365 """Get the information for index.html for `fname`.""" |
|
366 return self.files.get(fname, {}).get('index', {}) |
|
367 |
|
368 def set_index_info(self, fname, info): |
|
369 """Set the information for index.html for `fname`.""" |
|
370 self.files.setdefault(fname, {})['index'] = info |
153 |
371 |
154 |
372 |
155 # Helpers for templates and generating HTML |
373 # Helpers for templates and generating HTML |
156 |
374 |
157 def escape(t): |
375 def escape(t): |
158 """HTML-escape the text in t.""" |
376 """HTML-escape the text in `t`.""" |
159 return (t |
377 return (t |
160 # Convert HTML special chars into HTML entities. |
378 # Convert HTML special chars into HTML entities. |
161 .replace("&", "&").replace("<", "<").replace(">", ">") |
379 .replace("&", "&").replace("<", "<").replace(">", ">") |
162 .replace("'", "'").replace('"', """) |
380 .replace("'", "'").replace('"', """) |
163 # Convert runs of spaces: "......" -> " . . ." |
381 # Convert runs of spaces: "......" -> " . . ." |