forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_fallback_instrumentation.py
More file actions
executable file
·95 lines (76 loc) · 2.67 KB
/
Copy pathcheck_fallback_instrumentation.py
File metadata and controls
executable file
·95 lines (76 loc) · 2.67 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
#!/usr/bin/env python3
"""Optional ratchet: warn when touched files add fallback branches without record_fallback.
Usage:
python scripts/check_fallback_instrumentation.py path/to/file.py [more files...]
git diff --name-only | xargs python scripts/check_fallback_instrumentation.py
Not wired into CI yet — advisory only.
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
_FALLBACK_BRANCH = re.compile(
r'(?:fallback|fail.?open|degraded)',
re.IGNORECASE,
)
_RECORD_FALLBACK = re.compile(
r'record_fallback|recordFallback',
re.IGNORECASE,
)
_DIFF_HUNK = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@')
def _added_lines(path: Path) -> list[tuple[int, str]]:
try:
import subprocess
result = subprocess.run(
['git', 'diff', '--unified=0', '--', str(path)],
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0 or not result.stdout.strip():
return [
(idx, line)
for idx, line in enumerate(path.read_text(encoding='utf-8', errors='replace').splitlines(), start=1)
]
except Exception:
return [
(idx, line)
for idx, line in enumerate(path.read_text(encoding='utf-8', errors='replace').splitlines(), start=1)
]
added: list[tuple[int, str]] = []
line_no = 1
for line in result.stdout.splitlines():
if line.startswith('+++') or line.startswith('---'):
continue
hunk_match = _DIFF_HUNK.match(line)
if hunk_match:
line_no = int(hunk_match.group(1))
continue
if line.startswith('+') and not line.startswith('+++'):
added.append((line_no, line[1:]))
line_no += 1
return added
def check_file(path: Path) -> list[str]:
text = path.read_text(encoding='utf-8', errors='replace')
if _RECORD_FALLBACK.search(text):
return []
warnings: list[str] = []
for line_no, line in _added_lines(path):
if _FALLBACK_BRANCH.search(line):
warnings.append(f'{path}:{line_no}: fallback-like branch without record_fallback/recordFallback')
return warnings
def main(argv: list[str]) -> int:
if len(argv) < 2:
print(__doc__.strip(), file=sys.stderr)
return 2
warnings: list[str] = []
for raw in argv[1:]:
path = Path(raw)
if not path.is_file():
continue
warnings.extend(check_file(path))
for warning in warnings:
print(f'warning: {warning}', file=sys.stderr)
return 1 if warnings else 0
if __name__ == '__main__':
raise SystemExit(main(sys.argv))