File: combine_coverage.py

package info (click to toggle)
python-pex 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 2,840 kB
  • sloc: python: 9,757; sh: 1,394; makefile: 165
file content (68 lines) | stat: -rw-r--r-- 2,003 bytes parent folder | download | duplicates (3)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import pprint
import sys

from coverage.data import CoverageData
from pex.pex_builder import PEXBuilder


def _iter_filter(root_dir, data_dict):
  root_fragment = os.path.join(root_dir, 'pex/')
  pex_fragment = '/%s/_pex/' % PEXBuilder.BOOTSTRAP_DIR

  for filename, records in data_dict.items():
    # already acceptable coverage
    if filename.startswith(root_fragment):
      yield (filename, dict((record, None) for record in records))
      continue

    # possible it's coverage from within a pex environment
    try:
      bi = filename.index(pex_fragment)
    except ValueError:
      continue

    # rewrite to make up for fact that each pex environment is ephemeral.
    yield (
        os.path.join(root_dir, 'pex', filename[bi + len(pex_fragment):]),
        dict((record, None) for record in records)
    )


def combine_pex_coverage(root_dir, coverage_file_iter, unlink=True):
  combined = CoverageData(basename='.coverage')

  for filename in coverage_file_iter:
    cov = CoverageData(basename=filename)
    cov.read()
    combined.add_line_data(dict(_iter_filter(root_dir, cov.line_data())))
    combined.add_arc_data(dict(_iter_filter(root_dir, cov.arc_data())))
    os.unlink(filename)

  # filter out non-pex files
  prefix = os.path.join(root_dir, 'pex/')
  non_pex = [filename for filename in combined.lines if not filename.startswith(prefix)]
  for filename in non_pex:
    combined.lines.pop(filename)

  non_pex = [filename for filename in combined.arcs if not filename.startswith(prefix)]
  for filename in non_pex:
    combined.arcs.pop(filename)

  combined.write()
  return combined.filename


def main(args):
  script = os.path.realpath(args[0])
  root = os.path.realpath(os.path.join(os.path.dirname(script), '..'))

  coverage_iterator = [
      os.path.join(root, filename) for filename in os.listdir(root)
      if filename.startswith('.coverage')]

  combine_pex_coverage(root, coverage_iterator, unlink=False)


if __name__ == '__main__':
  main(sys.argv)