Skip to content

Commit 6a1c275

Browse files
committedJan 27, 2023
exp: an unsupport select_contexts.py #668
1 parent 2d628d4 commit 6a1c275

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
 

‎lab/select_contexts.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2+
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
3+
4+
"""\
5+
Select certain contexts from a coverage.py data file.
6+
"""
7+
8+
import argparse
9+
import re
10+
import sys
11+
12+
import coverage
13+
14+
15+
def main(argv):
16+
parser = argparse.ArgumentParser(description=__doc__)
17+
parser.add_argument("--include", type=str, help="Regex for contexts to keep")
18+
parser.add_argument("--exclude", type=str, help="Regex for contexts to discard")
19+
args = parser.parse_args(argv)
20+
21+
print("** Note: this is a proof-of-concept. Support is not promised. **")
22+
print("Feedback is appreciated: https://github.com/nedbat/coveragepy/issues/668")
23+
24+
cov_in = coverage.Coverage()
25+
cov_in.load()
26+
data_in = cov_in.get_data()
27+
print(f"Contexts in {data_in.data_filename()}:")
28+
for ctx in sorted(data_in.measured_contexts()):
29+
print(f" {ctx}")
30+
31+
if args.include is None and args.exclude is None:
32+
print("Nothing to do, no output written.")
33+
return
34+
35+
out_file = "output.data"
36+
file_names = data_in.measured_files()
37+
print(f"{len(file_names)} measured files")
38+
print(f"Writing to {out_file}")
39+
cov_out = coverage.Coverage(data_file=out_file)
40+
data_out = cov_out.get_data()
41+
42+
for ctx in sorted(data_in.measured_contexts()):
43+
if args.include is not None:
44+
if not re.search(args.include, ctx):
45+
print(f"Skipping context {ctx}, not included")
46+
continue
47+
if args.exclude is not None:
48+
if re.search(args.exclude, ctx):
49+
print(f"Skipping context {ctx}, excluded")
50+
continue
51+
print(f"Keeping context {ctx}")
52+
data_in.set_query_context(ctx)
53+
data_out.set_context(ctx)
54+
if data_in.has_arcs():
55+
data_out.add_arcs({f: data_in.arcs(f) for f in file_names})
56+
else:
57+
data_out.add_lines({f: data_in.lines(f) for f in file_names})
58+
59+
for fname in file_names:
60+
data_out.touch_file(fname, data_in.file_tracer(fname))
61+
62+
cov_out.save()
63+
64+
65+
if __name__ == "__main__":
66+
sys.exit(main(sys.argv[1:]))

0 commit comments

Comments
 (0)
Please sign in to comment.