coverage_helper.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - Code coverage tracing support
  4. #
  5. # Copyright 2018 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. from __future__ import division, absolute_import, print_function, unicode_literals
  22. try:
  23. import coverage as coverage_mod
  24. except ImportError as e:
  25. coverage_mod = None
  26. import os
  27. import sys
  28. import atexit
  29. __all__ = [ ]
  30. coverageInstance = None
  31. def coverageError(msg):
  32. if sys.stderr:
  33. sys.stderr.write(msg + "\n")
  34. sys.stderr.flush()
  35. def coverageTryStart():
  36. global coverageInstance
  37. covPath = os.getenv("AWLSIM_COVERAGE", "")
  38. if covPath:
  39. if not coverage_mod:
  40. coverageError("Code coverage tracing is requested "
  41. "(AWLSIM_COVERAGE), but the Python 'coverage' "
  42. "module was not found. "
  43. "Please install Python 'coverage'.")
  44. return
  45. if not coverageInstance:
  46. atexit.register(coverageStop)
  47. try:
  48. omit = [
  49. "/usr/*",
  50. "awlsim-test",
  51. "awlsim/gui/*",
  52. "awlsim_loader/*",
  53. "libs/*",
  54. "submodules/*",
  55. "tests/*",
  56. ]
  57. kwargs = {
  58. "data_file" : covPath,
  59. "auto_data" : True,
  60. "branch" : False,
  61. "check_preimported" : True,
  62. "config_file" : False,
  63. "omit" : omit,
  64. }
  65. for remove in ("", "check_preimported"):
  66. if remove:
  67. kwargs.pop(remove)
  68. try:
  69. coverageInstance = coverage_mod.Coverage(**kwargs)
  70. break
  71. except TypeError:
  72. pass
  73. else:
  74. coverageError("Failed to initialize code "
  75. "coverage tracing. It is unknown how to "
  76. "instantiate Coverage correctly.")
  77. sys.exit(1)
  78. coverageInstance.start()
  79. except coverage_mod.misc.CoverageException as e:
  80. coverageError("Coverage tracing exception: " + str(e))
  81. sys.exit(1)
  82. def coverageStop():
  83. global coverageInstance
  84. if coverageInstance:
  85. try:
  86. coverageInstance.stop()
  87. except coverage_mod.misc.CoverageException as e:
  88. coverageError("Coverage tracing exception: " + str(e))
  89. sys.exit(1)
  90. finally:
  91. coverageInstance = None
  92. coverageTryStart()