forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_coverage_conflicts.py
More file actions
executable file
·84 lines (68 loc) · 2.55 KB
/
Copy pathfilter_coverage_conflicts.py
File metadata and controls
executable file
·84 lines (68 loc) · 2.55 KB
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
#!/usr/bin/env python3
# Copyright (c) 2026 Zededa, Inc.
# SPDX-License-Identifier: Apache-2.0
"""Filter a Go coverage text file to remove entries with conflicting NumStmt.
When coverage data from different binary builds is merged, the same source
block may appear with different NumStmt values (e.g. after a code change).
'go tool cover -func' rejects such files with "inconsistent NumStmt".
This script keeps the first occurrence of each (file, range) key and drops
subsequent lines where NumStmt differs, printing a warning to stderr.
Usage:
python3 filter_coverage_conflicts.py <input> [output]
python3 filter_coverage_conflicts.py < input.txt > output.txt
"""
import sys
def filter_conflicts(infile, outfile):
"""Read coverage lines from infile, write deduplicated lines to outfile."""
seen = {} # key -> numstmt of first occurrence
dropped = 0
for line in infile:
stripped = line.rstrip('\n')
if not stripped or stripped.startswith('mode:'):
outfile.write(line)
continue
parts = stripped.split()
if len(parts) != 3:
outfile.write(line)
continue
key, numstmt, _ = parts
prev = seen.get(key)
if prev is None:
seen[key] = numstmt
outfile.write(line)
elif prev != numstmt:
dropped += 1
if dropped <= 10:
print(
f"warning: dropping {key} numstmt={numstmt} "
f"(conflicts with first-seen numstmt={prev})",
file=sys.stderr,
)
else:
outfile.write(line)
if dropped > 10:
print(
f"warning: {dropped} conflicting entries dropped in total",
file=sys.stderr,
)
elif dropped:
print(f"warning: {dropped} conflicting entries dropped", file=sys.stderr)
def main():
"""Parse arguments and invoke filter_conflicts on the specified files."""
args = sys.argv[1:]
if args and args[0] in ('-h', '--help'):
print(__doc__)
sys.exit(0)
if not args:
filter_conflicts(sys.stdin, sys.stdout)
elif len(args) == 1:
with open(args[0], encoding='utf-8') as inf:
filter_conflicts(inf, sys.stdout)
elif len(args) == 2:
with open(args[0], encoding='utf-8') as inf, open(args[1], 'w', encoding='utf-8') as outf:
filter_conflicts(inf, outf)
else:
print(f"usage: {sys.argv[0]} [input [output]]", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()