create_jcoverage_report.py 3.3 KB

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