-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmigrate_p3_asserts.py
More file actions
73 lines (59 loc) · 2.45 KB
/
Copy pathmigrate_p3_asserts.py
File metadata and controls
73 lines (59 loc) · 2.45 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
import csv
import os
import re
import sys
from pathlib import Path
def _warn_skip(filepath: Path, exc: BaseException) -> None:
print(f"Skipping {filepath}: {type(exc).__name__}: {exc}", file=sys.stderr)
def main():
ledger_root = Path(
os.environ.get(
"AURA_AUDIT_LEDGER_ROOT",
Path.home() / "Downloads" / "aura_exhaustive_forensic_ledger" / "by_category",
)
).expanduser()
ledger_file = ledger_root / "assert_in_production.csv"
if not ledger_file.exists():
print("CSV not found.")
return
file_mods = {}
with open(ledger_file) as f:
reader = csv.DictReader(f)
for row in reader:
filepath = Path(row["file"])
if not filepath.exists():
continue
line_no = int(row["line"]) - 1
if filepath not in file_mods:
try:
file_mods[filepath] = {"lines": filepath.read_text().splitlines(), "changed": False}
except (OSError, UnicodeDecodeError) as exc:
_warn_skip(filepath, exc)
continue
lines = file_mods[filepath]["lines"]
if line_no < 0 or line_no >= len(lines):
continue
original_line = lines[line_no]
# Simple assert with message: assert condition, "message"
match = re.search(r'^([ \t]*)assert (.*?), (.*)$', original_line)
if match:
indent = match.group(1)
condition = match.group(2)
msg = match.group(3)
lines[line_no] = f"{indent}if not ({condition}): raise RuntimeError({msg})"
file_mods[filepath]["changed"] = True
continue
# Simple assert without message: assert condition
match = re.search(r'^([ \t]*)assert (.*)$', original_line)
if match:
indent = match.group(1)
condition = match.group(2)
lines[line_no] = f"{indent}if not ({condition}): raise RuntimeError('Assertion failed')"
file_mods[filepath]["changed"] = True
for filepath, data in file_mods.items():
if data["changed"]:
content = "\n".join(data["lines"]) + "\n"
filepath.write_text(content)
print(f"Migrated P3 asserts in {filepath}")
if __name__ == "__main__":
main()