File size: 5,778 Bytes
275d22b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Helper to manage coverage.py related operations. Does two main
things: (1) pass commands into the coverage.py library and (2)
translate .coverage created from a pyinstaller executable into
paths that match local files. This is needed for html report creation.
"""
import os
import re
import json
import sys
from coverage.cmdline import main as coverage_main
from coverage.data import CoverageData


def should_exclude_file(filepath):
  """Returns whether the path should be excluded from the coverage report."""
  # Skip all atheris code
  if "atheris" in filepath:
    return True

  # Filter out all standard python libraries
  if ('/usr/local/lib/python' in filepath and
      'site-packages' not in filepath and 'dist-packages' not in filepath):
    return True

  # Avoid all PyInstaller modules.
  if 'PyInstaller' in filepath:
    return True

  return False


def translate_lines(cov_data, new_cov_data, all_file_paths):
  """
  Translate lines in a .coverage file created by coverage.py such that
  the file paths points to local files instead. This is needed when collecting
  coverage from executables created by pyinstaller.
  """
  for pyinstaller_file_path in cov_data.measured_files():
    stripped_py_file_path = pyinstaller_file_path
    if stripped_py_file_path.startswith('/tmp/_MEI'):
      stripped_py_file_path = '/'.join(stripped_py_file_path.split('/')[3:])
    if stripped_py_file_path.startswith('/out/'):
      stripped_py_file_path = stripped_py_file_path.replace('/out/', '')

    # Check if this file exists in our file paths:
    for local_file_path in all_file_paths:
      if should_exclude_file(local_file_path):
        continue
      if local_file_path.endswith(stripped_py_file_path):
        print('Found matching: %s' % (local_file_path))
        new_cov_data.add_lines(
            {local_file_path: cov_data.lines(pyinstaller_file_path)})


def translate_coverage(all_file_paths):
  """
  Translate pyinstaller-generated file paths in .coverage (produced by
  coverage.py) into local file paths. Place result in .new_coverage.
  """
  covdata_pre_translation = CoverageData('.coverage')
  covdata_post_translation = CoverageData('.new_coverage')

  covdata_pre_translation.read()
  translate_lines(covdata_pre_translation, covdata_post_translation,
                  all_file_paths)
  covdata_post_translation.write()


def convert_coveragepy_cov_to_summary_json(src, dst):
  """
  Converts a json file produced by coveragepy into a summary.json file
  similary to llvm-cov output. `src` is the source coveragepy json file,
  `dst` is the destination json file, which will be overwritten.
  """
  dst_dict = {'data': [{'files': {}}]}
  lines_covered = 0
  lines_count = 0
  with open(src, "r") as src_f:
    src_json = json.loads(src_f.read())
    if 'files' in src_json:
      for elem in src_json.get('files'):
        if 'summary' not in src_json['files'][elem]:
          continue
        src_dict = src_json['files'][elem]['summary']
        count = src_dict['covered_lines'] + src_dict['missing_lines']
        covered = src_dict['covered_lines']
        notcovered = src_dict['missing_lines']
        percent = src_dict['percent_covered']

        # Accumulate line coverage
        lines_covered += covered
        lines_count += count

        dst_dict['data'][0]['files'][elem] = {
            'summary': {
                'lines': {
                    'count': count,
                    'covered': covered,
                    'notcovered': notcovered,
                    'percent': percent
                }
            }
        }
  if lines_count > 0:
    lines_covered_percent = lines_covered / lines_count
  else:
    lines_covered_percent = 0.0
  dst_dict['data'][0]['totals'] = {
      'branches': {
          'count': 0,
          'covered': 0,
          'notcovered': 0,
          'percent': 0.0
      },
      'functions': {
          'count': 0,
          'covered': 0,
          'percent': 0.0
      },
      'instantiations': {
          'count': 0,
          'covered': 0,
          'percent': 0.0
      },
      'lines': {
          'count': lines_count,
          'covered': lines_covered,
          'percent': lines_covered_percent
      },
      'regions': {
          'count': 0,
          'covered': 0,
          'notcovered': 0,
          'percent': 0.0
      }
  }

  with open(dst, 'w') as dst_f:
    dst_f.write(json.dumps(dst_dict))


def main():
  """
  Main handler.
  """
  if sys.argv[1] == 'translate':
    print('Translating the coverage')
    files_path = sys.argv[2]
    all_file_paths = list()
    for root, _, files in os.walk(files_path):
      for relative_file_path in files:
        abs_file_path = os.path.abspath(os.path.join(root, relative_file_path))
        all_file_paths.append(abs_file_path)
    print('Done with path walk')
    translate_coverage(all_file_paths)
  elif sys.argv[1] == 'convert-to-summary-json':
    src = sys.argv[2]
    dst = sys.argv[3]
    convert_coveragepy_cov_to_summary_json(src, dst)
  else:
    # Pass commands into coverage package
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(coverage_main())


if __name__ == '__main__':
  main()