| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (c) MediaTek USA Inc., 2020-2024 |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
# your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, see |
| 17 |
# <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
# |
| 20 |
# This script traverses Python coverage data in one or more coverage |
| 21 |
# data file (generated by the Coverage.py module) and translates it into |
| 22 |
# LCOV .info format. |
| 23 |
# |
| 24 |
# py2lcov [--output mydata.info] [--test-name name] [options] coverage.dat+ |
| 25 |
# |
| 26 |
# See 'py2lcov --help' for more usage information |
| 27 |
# |
| 28 |
# See https://coverage.readthedocs.io for directions on how to use Coverage.py |
| 29 |
# to generate Python coverage data. |
| 30 |
# |
| 31 |
# |
| 32 |
# arguably, should do this in Perl so we could use the lcovutil module utilities |
| 33 |
# In the meantime: suggested use model is to translate the python XML using this |
| 34 |
# utility, then read it back into lcov for additional processing. |
| 35 |
# |
| 36 |
# @todo figure out/enhance Coverage.py to characterize branch expressions |
| 37 |
# @todo perhaps this should be integrated into the Coverage.py module itself. |
| 38 |
# This might no longer be a good idea as XML translation is no longer limited |
| 39 |
# to the Coverage.py module. |
| 40 |
|
| 41 |
import os |
| 42 |
import os.path |
| 43 |
import sys |
| 44 |
import re |
| 45 |
import argparse |
| 46 |
import xml.etree.ElementTree as ET |
| 47 |
import fnmatch |
| 48 |
import subprocess |
| 49 |
import copy |
| 50 |
import base64 |
| 51 |
import hashlib |
| 52 |
import pdb |
| 53 |
from xml2lcovutil import ProcessFile |
| 54 |
|
| 55 |
def main(): |
| 56 |
usageString="""py2lcov: Translate Python coverage data to LCOV .info format. |
| 57 |
Please also see https://coverage.readthedocs.io |
| 58 |
|
| 59 |
Note that the '--no-functions' argument may result in subtly inconsistent coverage |
| 60 |
data if a 'no-functions' coverage DB is merged with one which contains derived |
| 61 |
function data because the 'def myFunc(...)' line will acquire a 'hit' count |
| 62 |
of 1 because the python interpreter considers the 'def' to have been executed |
| 63 |
when the line is interpreted (i.e., when the function is defined). |
| 64 |
This will generate an 'inconsistent' error if the function is not executed in |
| 65 |
your tests because the (derived) function will have a zero hit count but the |
| 66 |
first line of the function has a non-zero count. |
| 67 |
Best practice is to either always specify '--no-functions' or never specify |
| 68 |
'--no-functions'. |
| 69 |
|
| 70 |
py2lcov uses Coverage.py to extract coverage data. |
| 71 |
Note that the name of the Coverage.py executable my differ on your platform. |
| 72 |
By default, py2lcov uses 'coverage' (which it expects to be in your path). |
| 73 |
You can use a different executable, either: |
| 74 |
- through your COVERAGE_COMMAND environment variable, or |
| 75 |
- via the 'py2lcov --cmd exename ..' command line option. |
| 76 |
|
| 77 |
py2lcov does not implement the full suite of LCOV features (e.g., filtering, |
| 78 |
substitutions, etc.). |
| 79 |
Please generate the translated LCOV format file and then read the data |
| 80 |
back in to lcov to use any of those features. |
| 81 |
%(usage)s |
| 82 |
|
| 83 |
Example: |
| 84 |
$ export PYCOV_DATA=path/to/pydata |
| 85 |
|
| 86 |
For 'coverage' versions 6.6.1 and higher (which support "--data-file"): |
| 87 |
$ coverage run --data-file=${PYCOV_DATA} --append --branch \\ |
| 88 |
`which myPthonScript.py` args_to_my_python_script |
| 89 |
|
| 90 |
For older versions which don't support "--data-files": |
| 91 |
use COVERAGE_FILE environment variable to specify data file |
| 92 |
$ COVERAGE_FILE=${PYCOV_DATA} coverage run --append --branch \\ |
| 93 |
`which myPthonScript.py` args_to_my_python_script |
| 94 |
|
| 95 |
# now use py2lcov to translate the XML to INFO file format - |
| 96 |
# also include version information in the generated coverage data. |
| 97 |
$ py2lcov -o pydata.info ${PYCOV_DATA} |
| 98 |
|
| 99 |
# apply some filtering |
| 100 |
$ lcov -a pydata.info -o filtered.info --filter branch,blank |
| 101 |
|
| 102 |
# and use genhtml to produce an HTML coverage report: |
| 103 |
$ genhtml -o html_report pydata.info .... |
| 104 |
# the filtered result. |
| 105 |
$ genhtml -o html_filtered filtered.info .... |
| 106 |
# use differential coverage to see exactly what filtering did |
| 107 |
$ genhtml -o html_differential --baseline-file mydata.info filtered.info ... |
| 108 |
|
| 109 |
Deprecated feature: |
| 110 |
For backward compatibility, py2lcov also supports translation to LCOV |
| 111 |
format from intermediate XML: |
| 112 |
|
| 113 |
# first translate from Python coerage data to XML: |
| 114 |
$ coverage xml --data-file=${PYCOV_DATA} -o pydata.xml |& tee pydata.log |
| 115 |
# or - if your Coverage.py module is too old to support '--data-file': |
| 116 |
$ COVERAGE_FILE=${PYCOV_DATA} coverage xml -o pydata.xml |& tee pydata.log |
| 117 |
|
| 118 |
# then translate XML to LCOV format: |
| 119 |
$ py2lcov -i pydata.xml -o pydata.info --version-script myCovScript |
| 120 |
|
| 121 |
""" % { |
| 122 |
'usage' : ProcessFile.usageNote, |
| 123 |
} |
| 124 |
|
| 125 |
from_env = '' |
| 126 |
cover_cmd = 'coverage' |
| 127 |
if 'COVERAGE_COMMAND' in os.environ: |
| 128 |
cover_cmd = os.environ['COVERAGE_COMMAND'] |
| 129 |
from_env = ' (from your COVERAGE_COMMAND environment variable)' |
| 130 |
|
| 131 |
parser = argparse.ArgumentParser( |
| 132 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 133 |
epilog=usageString) |
| 134 |
|
| 135 |
parser.add_argument('-i', '--input', dest='input', default=None, |
| 136 |
help="DEPRECATED: specify the input xml file from coverage.py") |
| 137 |
parser.add_argument('-o', '--output', dest='output', default='py2lcov.info', |
| 138 |
help="specify the out LCOV .info file, default: py2lcov.info") |
| 139 |
parser.add_argument('-t', '--test-name', '--testname', dest='testName', default='', |
| 140 |
help="specify the test name for the TN: entry in LCOV .info file") |
| 141 |
parser.add_argument('-e', '--exclude', dest='excludePatterns', default='', |
| 142 |
help="specify the exclude file patterns separated by ','") |
| 143 |
parser.add_argument('-v', '--verbose', dest='verbose', default=0, action='count', |
| 144 |
help="print debug messages") |
| 145 |
parser.add_argument('--version-script', dest='version', |
| 146 |
help="version extract callback") |
| 147 |
parser.add_argument('--checksum', dest='checksum', action='store_true', |
| 148 |
default=False, |
| 149 |
help="compute line checksum - see 'man lcov'") |
| 150 |
parser.add_argument("--no-functions", dest='deriveFunctions', |
| 151 |
default=True, action='store_false', |
| 152 |
help="do not derive function coverpoints") |
| 153 |
parser.add_argument("--tabwidth", dest='tabwidth', default=8, type=int, |
| 154 |
help='tabsize when computing indent') |
| 155 |
parser.add_argument('-k', "--keep-going", dest='keepGoing', default=False, action='store_true', |
| 156 |
help="ignore errors") |
| 157 |
parser.add_argument('--cmd', dest='cover_cmd', default=cover_cmd, |
| 158 |
help='executable used to extract python data - e.g., "python3-coverage". Default is "%s"%s.' % (cover_cmd, from_env)) |
| 159 |
parser.add_argument('inputs', nargs='*', |
| 160 |
help="list of python coverage data input files - expected to be XML or Python .dat format") |
| 161 |
|
| 162 |
args = parser.parse_args() |
| 163 |
|
| 164 |
if args.input: |
| 165 |
if not args.keepGoing: |
| 166 |
print("--input is deprecated - please use 'py2lcov ... %s" % (args.input)) |
| 167 |
args.inputs.append(args.input); |
| 168 |
|
| 169 |
|
| 170 |
if not args.inputs: |
| 171 |
# no input file - see if COVERAGE_FILE environment variable is set |
| 172 |
try: |
| 173 |
args.inputs.append(os.environ['COVERAGE_FILE']) |
| 174 |
print("reading input from default COVERAGE_FILE '%s'" % args.inputs[0]) |
| 175 |
except: |
| 176 |
print("Error: no input files") |
| 177 |
sys.exit(1) |
| 178 |
|
| 179 |
args.isPython = True |
| 180 |
p = ProcessFile(args) |
| 181 |
|
| 182 |
for f in args.inputs: |
| 183 |
base, ext = os.path.splitext(f) |
| 184 |
if ext == '.xml': |
| 185 |
p.process_xml_file(f) |
| 186 |
continue |
| 187 |
|
| 188 |
# assume that anything not ending in .xml is a Coverage.py data file |
| 189 |
xml = base + '.xml' |
| 190 |
suffix = 1 |
| 191 |
while os.path.exists(xml): |
| 192 |
xml = base + '.xml%d' % suffix |
| 193 |
suffix += 1 |
| 194 |
env = os.environ.copy() |
| 195 |
env["COVERAGE_FILE"] = f |
| 196 |
cmd = [args.cover_cmd, "xml", "-o", xml] |
| 197 |
try: |
| 198 |
x = subprocess.run(cmd, shell=False, check=True, stdout=True, stderr=True, env=env) |
| 199 |
except subprocess.CalledProcessError as err: |
| 200 |
print("Error: error during XML conversion of %s: %s" % ( |
| 201 |
f, str(err))); |
| 202 |
if not args.keepGoing: |
| 203 |
sys.exit(1) |
| 204 |
continue |
| 205 |
p.process_xml_file(xml) |
| 206 |
os.unlink(xml) |
| 207 |
|
| 208 |
p.close() |
| 209 |
|
| 210 |
|
| 211 |
if __name__ == '__main__': |
| 212 |
main() |