forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeferred-work-marker-count.py
More file actions
321 lines (277 loc) · 10.4 KB
/
Copy pathdeferred-work-marker-count.py
File metadata and controls
321 lines (277 loc) · 10.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python3
"""Count explicit deferred-work markers in repository text files."""
from __future__ import annotations
import argparse
import os
import re
import subprocess
import sys
from collections import Counter
from pathlib import Path
MARKERS = ("TO" "DO", "FIX" "ME", "HA" "CK")
# `(?<!\.)` keeps member accesses like Swift's `.todo` enum case (or `tags.todo`)
# from counting as deferred-work comment markers.
MARKER_RE = re.compile(r"(?<!\.)\b(" + "|".join(MARKERS) + r")\b", re.IGNORECASE)
TRACKING_ISSUE_RE = re.compile(r"(?:https://github\.com/[^/\s]+/[^/\s]+/(?:issues|pull)/\d+|(?<!\w)#\d+\b)")
EXCLUDED_DIR_NAMES = {
".build",
".dart_tool",
".git",
".next",
".pub-cache",
"build",
"DerivedData",
"node_modules",
"Pods",
"target",
}
NORMALIZED_EXCLUDED_DIR_NAMES = {
".pio",
"Generated",
"generated",
"vendor",
}
NORMALIZED_EXCLUDED_PREFIXES = (
".github/workflows/",
"app/lib/l10n/",
"omi/firmware/devkit/src/lib/opus-1.2.1/",
"omi/firmware/omi/src/lib/core/lib/opus-1.2.1/",
)
NORMALIZED_EXCLUDED_SUFFIXES = (
".g.dart",
".gen.dart",
"Package.resolved",
"package-lock.json",
"pubspec.lock",
)
NORMALIZED_EXCLUDED_FILES = {
".github/scripts/deferred-work-marker-count.py",
"AGENTS.md",
"CLAUDE.md",
}
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", default=".", help="Repository root to scan.")
parser.add_argument("--raw", action="store_true", help="Include generated, vendored, policy, and workflow files.")
parser.add_argument("--changed-files", type=Path, help="File listing changed paths for the new-marker guard.")
parser.add_argument("--base", help="Git base ref for the new-marker guard.")
parser.add_argument("--check-new", action="store_true", help="Fail when an added marker lacks a tracking issue.")
parser.add_argument(
"--format",
choices=("plain", "github-summary"),
default="plain",
help="Output format.",
)
return parser.parse_args()
def added_lines(base: str, path: str) -> list[tuple[int, str]]:
exists_at_base = (
subprocess.run(
["git", "cat-file", "-e", f"{base}:{path}"],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode
== 0
)
tracked = (
subprocess.run(
["git", "ls-files", "--error-unmatch", path],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode
== 0
)
if not exists_at_base and not tracked:
try:
return list(enumerate(Path(path).read_text(encoding="utf-8").splitlines(), start=1))
except (OSError, UnicodeDecodeError):
return []
result = subprocess.run(
["git", "diff", "--unified=0", "--no-color", base, "--", path],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
if result.returncode:
raise RuntimeError(result.stderr.strip() or f"git diff failed for {path}")
additions: list[tuple[int, str]] = []
head_line = 0
for raw_line in result.stdout.splitlines():
if raw_line.startswith("@@"):
match = re.search(r"\+(\d+)(?:,(\d+))?", raw_line)
head_line = int(match.group(1)) - 1 if match else 0
continue
if raw_line.startswith("+++"):
continue
if raw_line.startswith("+"):
head_line += 1
additions.append((head_line, raw_line[1:]))
elif not raw_line.startswith("-") and head_line:
head_line += 1
return additions
def comment_fragment(line: str) -> str | None:
"""Return the comment portion of a line without treating strings as comments."""
quote: str | None = None
escaped = False
index = 0
while index < len(line):
character = line[index]
if quote:
if escaped:
escaped = False
elif character == "\\":
escaped = True
elif character == quote:
quote = None
index += 1
continue
if character in {"'", '"'}:
quote = character
index += 1
continue
for marker in ("<!--", "//", "#", "/*", "--"):
if line.startswith(marker, index):
return line[index + len(marker) :]
index += 1
stripped = line.lstrip()
if MARKER_RE.match(stripped) or stripped.startswith("*"):
return stripped
return None
def marker_signature(line: str) -> str | None:
"""Return a whitespace-insensitive signature for an explicit deferred-work comment."""
fragment = comment_fragment(line)
if fragment is None or not MARKER_RE.search(fragment):
return None
return re.sub(r"\s+", " ", fragment.strip()).casefold()
def marker_counts_at_base(base: str, path: str) -> Counter[str]:
result = subprocess.run(
["git", "show", f"{base}:{path}"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8",
)
if result.returncode:
return Counter()
return Counter(signature for line in result.stdout.splitlines() if (signature := marker_signature(line)))
def new_marker_violations(additions: list[tuple[int, str]], existing_markers: Counter[str]) -> list[tuple[int, str]]:
"""Return added unowned markers after accounting for whitespace-only rewrites."""
existing = Counter(existing_markers)
violations: list[tuple[int, str]] = []
for lineno, line in additions:
signature = marker_signature(line)
if signature is None or TRACKING_ISSUE_RE.search(line):
continue
if existing[signature]:
existing[signature] -= 1
continue
violations.append((lineno, line))
return violations
def check_new_markers(base: str, changed_files_path: Path) -> int:
violations: list[str] = []
for path in changed_files_path.read_text(encoding="utf-8").splitlines():
if not path or excluded_by_normalized_policy(path) or not Path(path).is_file():
continue
try:
additions = added_lines(base, path)
except RuntimeError as exc:
print(f"FAIL: {exc}", file=sys.stderr)
return 1
if not any(marker_signature(line) is not None for _, line in additions):
continue
existing_markers = marker_counts_at_base(base, path)
for lineno, line in new_marker_violations(additions, existing_markers):
violations.append(f"{path}:{lineno}: {line.strip()}")
if violations:
print("FAIL: new deferred-work markers must reference a tracking issue (#123 or GitHub URL).")
for violation in violations:
print(f" - {violation}")
return 1
print("OK: new deferred-work markers reference tracking issues.")
return 0
def is_binary(path: Path) -> bool:
try:
with path.open("rb") as handle:
return b"\0" in handle.read(4096)
except OSError:
return True
def excluded_by_normalized_policy(relative_path: str) -> bool:
if any(part in NORMALIZED_EXCLUDED_DIR_NAMES for part in Path(relative_path).parts):
return True
if relative_path in NORMALIZED_EXCLUDED_FILES:
return True
if relative_path.endswith(NORMALIZED_EXCLUDED_SUFFIXES):
return True
return relative_path.startswith(NORMALIZED_EXCLUDED_PREFIXES)
def iter_files(root: Path, raw: bool):
for dirpath, dirnames, filenames in os.walk(root):
current = Path(dirpath)
dirnames[:] = [name for name in dirnames if name not in EXCLUDED_DIR_NAMES]
for filename in filenames:
path = current / filename
relative_path = path.relative_to(root).as_posix()
if not raw and excluded_by_normalized_policy(relative_path):
continue
if is_binary(path):
continue
yield path, relative_path
def count_markers(root: Path, raw: bool) -> tuple[dict[str, int], dict[str, int]]:
marker_counts = {marker: 0 for marker in MARKERS}
file_counts: dict[str, int] = {}
for path, relative_path in iter_files(root, raw):
try:
text = path.read_text(encoding="utf-8")
except UnicodeDecodeError:
try:
text = path.read_text(encoding="latin-1")
except OSError:
continue
except OSError:
continue
matches = MARKER_RE.findall(text)
if not matches:
continue
file_counts[relative_path] = len(matches)
for marker in matches:
marker_counts[marker.upper()] += 1
return marker_counts, file_counts
def print_plain(marker_counts: dict[str, int], file_counts: dict[str, int], raw: bool) -> None:
label = "raw" if raw else "normalized"
total = sum(marker_counts.values())
print(f"{label} total: {total}")
for marker in MARKERS:
print(f"{marker}: {marker_counts[marker]}")
print(f"files: {len(file_counts)}")
def print_github_summary(marker_counts: dict[str, int], file_counts: dict[str, int], raw: bool) -> None:
label = "Raw" if raw else "Normalized"
total = sum(marker_counts.values())
print(f"### {label} deferred-work marker count")
print()
print("| Marker | Count |")
print("| --- | ---: |")
for marker in MARKERS:
print(f"| `{marker}` | {marker_counts[marker]} |")
print(f"| **Total** | **{total}** |")
print()
print(f"Files with markers: {len(file_counts)}")
if not raw:
print()
print("Normalized count excludes generated, vendored, build, lock, policy, and workflow files.")
def main() -> int:
args = parse_args()
if args.check_new:
if not args.changed_files or not args.base:
print("FAIL: --check-new requires --changed-files and --base", file=sys.stderr)
return 2
return check_new_markers(args.base, args.changed_files)
root = Path(args.root).resolve()
marker_counts, file_counts = count_markers(root, args.raw)
if args.format == "github-summary":
print_github_summary(marker_counts, file_counts, args.raw)
else:
print_plain(marker_counts, file_counts, args.raw)
return 0
if __name__ == "__main__":
raise SystemExit(main())