-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcli.py
More file actions
126 lines (108 loc) · 3.59 KB
/
Copy pathcli.py
File metadata and controls
126 lines (108 loc) · 3.59 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import argparse
import glob
import json
import os
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Dict
def list_slowest_tests() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--durations-path",
help=(
"Path to the file in which durations are stored, "
"default is .test_durations in the current working directory"
),
default=".test_durations",
type=argparse.FileType(),
)
parser.add_argument(
"-c",
"--count",
help="How many slowest to list",
default=10,
type=int,
)
args = parser.parse_args()
return _list_slowest_tests(json.load(args.durations_path), args.count)
def _list_slowest_tests(durations: "Dict[str, float]", count: int) -> None:
slowest_tests = tuple(
sorted(durations.items(), key=lambda item: item[1], reverse=True)
)[:count]
for test, duration in slowest_tests:
print(f"{duration:.2f} {test}") # noqa: T201
def run_combine_tests() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--durations-path",
help=(
"Path to the file in which durations are stored, "
"default is .test_durations in the current working directory"
),
default=".test_durations",
type=str,
)
parser.add_argument(
"--durations-pattern",
help=(
"Pattern to match the files in which durations are stored, "
"default is */.test_durations in the current working directory"
),
default="*/.test_durations",
type=str,
)
parser.add_argument(
"--root-folder",
help=(
"Path to the folder where to run the command"
"default is . to run in the current working directory"
),
default=".",
type=str,
)
parser.add_argument("--keep_original", action="store_true", default=False)
args = parser.parse_args()
return _run_combine_tests(
args.durations_path,
args.durations_pattern,
args.root_folder,
keep_original=args.keep_original,
)
def _run_combine_tests(
durations_path: str,
durations_pattern: str,
root_folder: str,
*,
keep_original: bool,
) -> None:
"""
Combines JSON files matching a pattern into a single object and writes it to an output file.
Args:
durations_pattern (str): A file pattern (e.g., "data_*.json") to match JSON files.
durations_path (str): The path to the output file where the combined data will be written.
"""
combined_data = {}
filenames = glob.glob(durations_pattern, root_dir=root_folder)
if not filenames:
print( # noqa: T201
f"No file found with pattern {durations_pattern} in {root_folder}"
)
return
try:
for filename in filenames:
fullpath_filename = os.path.join(root_folder, filename)
with open(fullpath_filename) as f:
data = json.load(f)
combined_data.update(data) # Efficiently merge dictionaries
except (OSError, json.JSONDecodeError) as e:
print(f"Error processing file '{filename}': {e}") # noqa: T201
return
if keep_original:
with open(durations_path) as f:
data = json.load(f)
combined_data.update(data)
print( # noqa: T201
f"{len(filenames)} files combined, with a total of {len(combined_data)} entries"
)
with open(durations_path, "w") as f:
json.dump(combined_data, f, indent=4) # Write with indentation