awlsim-covreport 2.95 KB
Newer Older
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# AWL simulator - Code coverage trace report generator
#
# Copyright 2018 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import division, absolute_import, print_function, unicode_literals

import sys
import os
import coverage as coverage_mod


30 31 32 33 34 35 36 37 38
def usage(f=sys.stdout):
	print("Awlsim code coverage report generator", file=f)
	print("", file=f)
	print("Usage: awlsim-covreport REPORTDIR DATASOURCE [DATASOURCE ...]", file=f)
	print("", file=f)
	print("REPORTDIR is the target directory where "
	      "the HTML report will be written to.", file=f)
	print("DATASOURCE is a Python coverage data file or a directory "
	      "containing only coverage data files.", file=f)
39 40 41 42 43 44

try:
	covReport = sys.argv[1]
	covData = sys.argv[2:]
	if not covData:
		raise IndexError
45 46 47 48 49 50 51 52

	dataPaths = []
	for item in covData:
		if os.path.isdir(item):
			for subItem in os.listdir(item):
				dataPaths.append(os.path.join(item, subItem))
		else:
			dataPaths.append(item)
53
except IndexError as e:
54
	usage(f=sys.stderr)
55 56 57 58
	sys.exit(1)

try:
	cov = coverage_mod.Coverage(config_file=False)
59 60 61 62 63 64 65 66 67 68 69

	# Define some exclude patterns
	cov.exclude(r"#@nocov")
	cov.exclude(r"assert\(0\)")
	cov.exclude(r"if 0:")
	cov.exclude(r"if False:")
	cov.exclude(r"raise AwlSimBug")
	cov.exclude(r"raise NotImplementedError")
	cov.exclude(r"raise RuntimeError")
	cov.exclude(r"pass\s*#\s*TODO")
	cov.exclude(r'raise AwlSimError\("Assertion failed"\)')
70
	cov.exclude(r'except UnicodeError')
71 72 73 74 75 76
	cov.exclude(r'if isPyPy')
	cov.exclude(r'if isJython')
	cov.exclude(r'if isIronPython')
	cov.exclude(r'if isCython')
	cov.exclude(r'if isMicroPython')
	cov.exclude(r'if isWinStandalone')
77 78 79
	cov.exclude(r'XmlFactory\.parser_beginTag\(self, tag\)')
	cov.exclude(r'XmlFactory\.parser_endTag\(self, tag\)')
	cov.exclude(r'XmlFactory\.parser_data\(self, data\)')
80 81

	# Combine all data files
82
	cov.combine(data_paths=dataPaths, strict=True)
83 84

	# Generate the HTML report
85
	cov.html_report(directory=covReport)
86 87 88 89 90 91 92 93

	# Fixup the report CSS
	with open(os.path.join(covReport, "style.css"), "rb+") as f:
		data = f.read()
		data = data.replace(b"#ffdddd", b"#ff0000")
		f.write(data)

except (coverage_mod.misc.CoverageException, OSError) as e:
94 95
	print("Could not generate coverage report: " + str(e),
	      file=sys.stderr)
96 97
	sys.exit(1)
sys.exit(0)