Skip to content

Commit be0c6a2

Browse files
committed
Wip
1 parent 8b8cedf commit be0c6a2

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

split-minimal-tests.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python3
2+
3+
"""split-minimal-tests
4+
This script splits HTML minimal-tests, produced by a software called
5+
`json-minimal-tests`, into distinct directories depending on metric differences.
6+
7+
Usage:
8+
9+
./split-minimal-tests.py -i INPUT_DIR -o OUTPUT_DIR_NAME [-t MT_THRESHOLD]
10+
11+
NOTE: OUTPUT_DIR_NAME is the name of the output directory.
12+
This directory could contain either a series of directories, called as
13+
the metrics that presents differences, or be empty if no metric differences
14+
are found.
15+
MT_THRESHOLD determines the maximum number of considered minimal tests
16+
for a metric.
17+
"""
18+
19+
import argparse
20+
import itertools
21+
import pathlib
22+
import re
23+
import shutil
24+
import typing as T
25+
26+
# List of metrics
27+
# TODO: Implement a command into rust-code-analysis-cli that returns all
28+
# computed metrics https://github.com/mozilla/rust-code-analysis/issues/478
29+
METRICS = [
30+
"cognitive",
31+
"sloc",
32+
"ploc",
33+
"lloc",
34+
"cloc",
35+
"blank",
36+
"cyclomatic",
37+
"halstead",
38+
"nom",
39+
"nexits",
40+
"nargs",
41+
]
42+
43+
44+
def main() -> None:
45+
parser = argparse.ArgumentParser(
46+
prog="split-minimal-tests",
47+
description="This tool splits HTML minimal-tests, produced by "
48+
"a software called `json-minimal-tests`, into distinct directories "
49+
"depending on metric differences.",
50+
epilog="The source code of this program can be found on "
51+
"GitHub at https://github.com/mozilla/rust-code-analysis",
52+
)
53+
54+
# Arguments
55+
parser.add_argument(
56+
"--input",
57+
"-i",
58+
type=lambda value: pathlib.Path(value),
59+
required=True,
60+
help="Input directory containing HTML minimal tests.",
61+
)
62+
63+
parser.add_argument(
64+
"--output-name",
65+
"-o",
66+
type=str,
67+
required=True,
68+
help="Name of the output directory.",
69+
)
70+
71+
# Optional arguments
72+
parser.add_argument(
73+
"--threshold",
74+
"-t",
75+
type=int,
76+
help="Maximum number of considered minimal tests for a metric.",
77+
)
78+
79+
# Parse arguments
80+
args = parser.parse_args()
81+
82+
# Create output directory
83+
output_dir = pathlib.Path(args.output_name)
84+
output_dir.mkdir(parents=True, exist_ok=True)
85+
86+
# Save files associated to each metric
87+
metrics_saver = {metric_name: [] for metric_name in METRICS}
88+
89+
# Iterate over the files contained in the input directory
90+
for path in args.input.glob("*.html"):
91+
# Open a file
92+
with open(path) as f:
93+
# Read a file
94+
file_str = f.read()
95+
96+
# Iterate over metrics
97+
for metric_name, metric_files in metrics_saver.items():
98+
# Regular expressions that match only when a metric is
99+
# outside the <pre></pre> tags
100+
re_expr = f"(\.{metric_name})|<pre>(?:(?!<pre\s?>).)*"
101+
102+
# Remove newlines to match tags
103+
if re.search(re_expr, file_str.replace("\n", "")).group(1):
104+
metric_files.append(path)
105+
106+
# Iterate over metrics to print them
107+
for metric_name, metric_files in metrics_saver.items():
108+
# Create path for metric directory
109+
metric_path = output_dir / metric_name
110+
111+
if metric_files:
112+
# Create metric directory
113+
metric_path.mkdir(parents=True, exist_ok=True)
114+
115+
# Save the number of files specified in the threshold
116+
output_paths = (
117+
metric_files[: args.threshold] if args.threshold else metric_files
118+
)
119+
120+
for path in output_paths:
121+
# Copy files in the directory
122+
shutil.copy(path, metric_path)
123+
124+
125+
if __name__ == "__main__":
126+
main()

0 commit comments

Comments
 (0)