forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_code_size_budget.py
More file actions
175 lines (148 loc) · 5.55 KB
/
check_code_size_budget.py
File metadata and controls
175 lines (148 loc) · 5.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
"""Enforce a ratcheting Rust file-size budget.
This script keeps the current oversized-file debt from getting worse while the
larger refactor program is underway.
Policy:
- Production Rust files above the configured LOC threshold are tracked in a
baseline file.
- Existing tracked oversized files may not grow.
- New oversized production files may not be introduced.
- If oversized files shrink or disappear, the script reports the improvement.
- `--update` refreshes the baseline after intentional cleanup.
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import Any
REPO_ROOT = Path(__file__).resolve().parent.parent
BASELINE_FILE = REPO_ROOT / "scripts" / "code_size_budget.json"
DEFAULT_THRESHOLD = 1200
SCAN_ROOTS = (REPO_ROOT / "src", REPO_ROOT / "crates")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--update",
action="store_true",
help="refresh the baseline to the current oversized-file set",
)
return parser.parse_args()
def is_production_rust_file(path: Path) -> bool:
rel = path.relative_to(REPO_ROOT).as_posix()
if path.suffix != ".rs":
return False
parts = rel.split("/")
if parts[0] == "tests" or any(
part == "tests" or part.endswith("_tests") or part.endswith("_test") or part.startswith("tests_")
for part in parts
):
return False
name = path.name
if (
name == "tests.rs"
or name.endswith("_tests.rs")
or name.endswith("_test.rs")
or name.startswith("tests_")
):
return False
return True
def rust_file_line_count(path: Path) -> int:
with path.open("r", encoding="utf-8") as handle:
return sum(1 for _ in handle)
def current_oversized_files(threshold: int) -> dict[str, int]:
files: dict[str, int] = {}
for root in SCAN_ROOTS:
if not root.exists():
continue
for path in sorted(root.rglob("*.rs")):
if not is_production_rust_file(path):
continue
line_count = rust_file_line_count(path)
if line_count > threshold:
files[path.relative_to(REPO_ROOT).as_posix()] = line_count
return files
def load_baseline() -> dict[str, Any]:
if not BASELINE_FILE.exists():
raise SystemExit(f"error: missing baseline file: {BASELINE_FILE}")
data = json.loads(BASELINE_FILE.read_text(encoding="utf-8"))
if not isinstance(data, dict):
raise SystemExit(f"error: invalid baseline file format: {BASELINE_FILE}")
threshold = data.get("threshold_loc")
tracked = data.get("tracked_files")
if not isinstance(threshold, int) or threshold <= 0:
raise SystemExit(f"error: invalid threshold_loc in {BASELINE_FILE}")
if not isinstance(tracked, dict) or any(
not isinstance(k, str) or not isinstance(v, int) or v <= 0
for k, v in tracked.items()
):
raise SystemExit(f"error: invalid tracked_files in {BASELINE_FILE}")
return data
def write_baseline(threshold: int, tracked_files: dict[str, int]) -> None:
payload = {
"version": 1,
"threshold_loc": threshold,
"tracked_files": tracked_files,
}
BASELINE_FILE.write_text(
json.dumps(payload, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
def main() -> int:
args = parse_args()
baseline = load_baseline()
threshold = baseline["threshold_loc"]
current = current_oversized_files(threshold)
if args.update:
write_baseline(threshold, current)
print(
"Updated code-size baseline: "
f"tracked={len(baseline['tracked_files'])} -> {len(current)} oversized files"
)
return 0
tracked: dict[str, int] = baseline["tracked_files"]
regressions: list[str] = []
improvements: list[str] = []
for path, lines in sorted(current.items()):
old_lines = tracked.get(path)
if old_lines is None:
regressions.append(
f"new oversized file exceeds {threshold} LOC: {path} ({lines} LOC)"
)
elif lines > old_lines:
regressions.append(
f"oversized file grew: {path} ({old_lines} -> {lines} LOC)"
)
elif lines < old_lines:
improvements.append(f"oversized file shrank: {path} ({old_lines} -> {lines} LOC)")
for path, old_lines in sorted(tracked.items()):
if path not in current:
improvements.append(
f"oversized file no longer exceeds {threshold} LOC: {path} ({old_lines} -> OK)"
)
if regressions:
print(
"Code-size budget exceeded. Existing oversized Rust files must shrink or stay flat, "
"and new oversized production files are not allowed:",
file=sys.stderr,
)
for entry in regressions:
print(f" - {entry}", file=sys.stderr)
print(
"Run scripts/check_code_size_budget.py --update only after intentional cleanup.",
file=sys.stderr,
)
return 1
if improvements:
print("Code-size budget improved:")
for entry in improvements:
print(f" - {entry}")
print("Consider running: scripts/check_code_size_budget.py --update")
else:
print(
"Code-size budget OK: "
f"tracked={len(tracked)} threshold={threshold}LOC no oversized-file regressions"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())