create_jcoverage_report.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from __future__ import print_function
  2. import argparse
  3. import tarfile
  4. import zipfile
  5. import os
  6. import sys
  7. import time
  8. import subprocess
  9. def mkdir_p(path):
  10. try:
  11. os.makedirs(path)
  12. except OSError:
  13. pass
  14. class Timer(object):
  15. def __init__(self):
  16. self.start = time.time()
  17. def step(self, msg):
  18. sys.stderr.write("{} ({}s)\n".format(msg, int(time.time() - self.start)))
  19. self.start = time.time()
  20. def main(
  21. source,
  22. output,
  23. java,
  24. prefix_filter,
  25. exclude_filter,
  26. jars_list,
  27. output_format,
  28. tar_output,
  29. agent_disposition,
  30. runners_paths,
  31. ):
  32. timer = Timer()
  33. reports_dir = 'jacoco_reports_dir'
  34. mkdir_p(reports_dir)
  35. with tarfile.open(source) as tf:
  36. tf.extractall(reports_dir)
  37. timer.step("Coverage data extracted")
  38. reports = [os.path.join(reports_dir, fname) for fname in os.listdir(reports_dir)]
  39. with open(jars_list) as f:
  40. jars = f.read().strip().split()
  41. if jars and runners_paths:
  42. for r in runners_paths:
  43. try:
  44. jars.remove(r)
  45. except ValueError:
  46. pass
  47. src_dir = 'sources_dir'
  48. cls_dir = 'classes_dir'
  49. mkdir_p(src_dir)
  50. mkdir_p(cls_dir)
  51. for jar in jars:
  52. if jar.endswith('devtools-jacoco-agent.jar'):
  53. agent_disposition = jar
  54. # Skip java contrib - it's irrelevant coverage
  55. if jar.startswith('contrib/java'):
  56. continue
  57. with zipfile.ZipFile(jar) as jf:
  58. for entry in jf.infolist():
  59. if entry.filename.endswith('.java'):
  60. dest = src_dir
  61. elif entry.filename.endswith('.class'):
  62. dest = cls_dir
  63. else:
  64. continue
  65. entry.filename = entry.filename
  66. jf.extract(entry, dest)
  67. timer.step("Jar files extracted")
  68. if not agent_disposition:
  69. print('Can\'t find jacoco agent. Will not generate html report for java coverage.', file=sys.stderr)
  70. if tar_output:
  71. report_dir = 'java.report.temp'
  72. else:
  73. report_dir = output
  74. mkdir_p(report_dir)
  75. if agent_disposition:
  76. agent_cmd = [
  77. java,
  78. '-jar',
  79. agent_disposition,
  80. src_dir,
  81. cls_dir,
  82. prefix_filter or '.',
  83. exclude_filter or '__no_exclude__',
  84. report_dir,
  85. output_format,
  86. ]
  87. agent_cmd += reports
  88. subprocess.check_call(agent_cmd)
  89. timer.step("Jacoco finished")
  90. if tar_output:
  91. with tarfile.open(output, 'w') as outf:
  92. outf.add(report_dir, arcname='.')
  93. if __name__ == '__main__':
  94. if 'LC_ALL' in os.environ:
  95. if os.environ['LC_ALL'] == 'C':
  96. os.environ['LC_ALL'] = 'en_GB.UTF-8'
  97. parser = argparse.ArgumentParser()
  98. parser.add_argument('--source', action='store')
  99. parser.add_argument('--output', action='store')
  100. parser.add_argument('--java', action='store')
  101. parser.add_argument('--prefix-filter', action='store')
  102. parser.add_argument('--exclude-filter', action='store')
  103. parser.add_argument('--jars-list', action='store')
  104. parser.add_argument('--output-format', action='store', default="html")
  105. parser.add_argument('--raw-output', dest='tar_output', action='store_false', default=True)
  106. parser.add_argument('--agent-disposition', action='store')
  107. parser.add_argument('--runner-path', dest='runners_paths', action='append', default=[])
  108. args = parser.parse_args()
  109. main(**vars(args))