blob: d4070312071225f573dbc81043a7ec31af706f90 (
plain)
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
|
#!/usr/bin/env python3
#
# Use BeautifulSoup to deduplicate functions in a grov XML (cobertura)
# output file.
import sys
try:
from bs4 import BeautifulSoup
_ = __import__("lxml")
except ImportError:
print("Sorry, BeautifulSoup 4 or lxml is not installed.", file=sys.stderr)
sys.exit(1)
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <cobertura_file>")
print(" Post-process a grcov cobertura.xml file")
sys.exit(1)
# Parse the coverage file
with open(sys.argv[1]) as f:
document = BeautifulSoup(f, "lxml")
def get_or_fail(obj, field):
"""
Like obj.field, but raise a KeyError if obj.field is None.
Insisting on an exception in this case helps mypy typecheck this code.
"""
val = getattr(obj, field)
if val is None:
raise KeyError(field)
return val
# Iterate over source files
coverage = get_or_fail(document, "coverage")
packages = get_or_fail(coverage, "packages")
for file in packages.findAll("package"):
already_seen = set()
# Iterate over function
for func in file.classes.findChild("class").methods.findAll("method"):
name = func["name"]
if name in already_seen:
# Remove duplicate function
func.extract()
else:
already_seen.add(name)
with open(sys.argv[1], "w") as out:
out.write(document.prettify())
|