awlsim-covreport 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Code coverage trace report generator
  5. #
  6. # Copyright 2018 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. import sys
  24. import os
  25. import coverage as coverage_mod
  26. def usage(f=sys.stdout):
  27. print("Awlsim code coverage report generator", file=f)
  28. print("", file=f)
  29. print("Usage: awlsim-covreport REPORTDIR DATASOURCE [DATASOURCE ...]", file=f)
  30. print("", file=f)
  31. print("REPORTDIR is the target directory where "
  32. "the HTML report will be written to.", file=f)
  33. print("DATASOURCE is a Python coverage data file or a directory "
  34. "containing only coverage data files.", file=f)
  35. try:
  36. covReport = sys.argv[1]
  37. covData = sys.argv[2:]
  38. if not covData:
  39. raise IndexError
  40. dataPaths = []
  41. for item in covData:
  42. if os.path.isdir(item):
  43. for subItem in os.listdir(item):
  44. dataPaths.append(os.path.join(item, subItem))
  45. else:
  46. dataPaths.append(item)
  47. except IndexError as e:
  48. usage(f=sys.stderr)
  49. sys.exit(1)
  50. try:
  51. cov = coverage_mod.Coverage(config_file=False)
  52. # Define some exclude patterns
  53. cov.exclude(r"#@nocov")
  54. cov.exclude(r"assert\(0\)")
  55. cov.exclude(r"if 0:")
  56. cov.exclude(r"if False:")
  57. cov.exclude(r"raise AwlSimBug")
  58. cov.exclude(r"raise NotImplementedError")
  59. cov.exclude(r"raise RuntimeError")
  60. cov.exclude(r"pass\s*#\s*TODO")
  61. cov.exclude(r'raise AwlSimError\("Assertion failed"\)')
  62. cov.exclude(r'except UnicodeError')
  63. cov.exclude(r'if isPyPy')
  64. cov.exclude(r'if isJython')
  65. cov.exclude(r'if isIronPython')
  66. cov.exclude(r'if isCython')
  67. cov.exclude(r'if isMicroPython')
  68. cov.exclude(r'if isWinStandalone')
  69. cov.exclude(r'XmlFactory\.parser_beginTag\(self, tag\)')
  70. cov.exclude(r'XmlFactory\.parser_endTag\(self, tag\)')
  71. cov.exclude(r'XmlFactory\.parser_data\(self, data\)')
  72. # Combine all data files
  73. cov.combine(data_paths=dataPaths, strict=True)
  74. # Generate the HTML report
  75. cov.html_report(directory=covReport)
  76. # Fixup the report CSS
  77. with open(os.path.join(covReport, "style.css"), "rb+") as f:
  78. data = f.read()
  79. data = data.replace(b"#ffdddd", b"#ff0000")
  80. f.write(data)
  81. except (coverage_mod.misc.CoverageException, OSError) as e:
  82. print("Could not generate coverage report: " + str(e),
  83. file=sys.stderr)
  84. sys.exit(1)
  85. sys.exit(0)