# SPDX-License-Identifier: MIT """Move withdrawn Rust code aside, unread, into scratch/withdrawn/ (the clean rewrite of 2026-11-19, docs/restart.md). Prints only names and line counts, never the text it moves. withdraw.py file PATH move a whole file withdraw.py items PATH NAME... cut fn/const items by name (with their doc comments and attributes) withdraw.py fields PATH NAME... cut every `NAME:` line (struct field or initialiser) with the comment lines above it withdraw.py moddoc PATH cut the leading //! module documentation withdraw.py faults SCRIPT SRC cut seeded faults whose search text no longer occurs in their file under SRC """ from __future__ import annotations import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "scratch" / "withdrawn" def _stash(rel: str, label: str, text: str) -> None: dest = OUT / (rel + ".cut") dest.parent.mkdir(parents=True, exist_ok=True) with dest.open("a") as f: f.write(f"// ---- {label}\n{text}\n") def _comment_start(lines: list[str], i: int) -> int: while i > 0 and re.match(r"\s*(//|#\[)", lines[i - 1]) and not lines[i - 1].lstrip().startswith("//!"): i -= 1 return i def _brace_end(lines: list[str], i: int) -> int: """Index of the line holding the brace that closes the first `{` at or after line i (or the `;` that ends an item with no body).""" depth = 0 seen = False for j in range(i, len(lines)): line = re.sub(r"//.*", "", lines[j]) line = re.sub(r'"(\\.|[^"\\])*"', '""', line) line = re.sub(r"'(\\.|[^'\\])'", "''", line) for ch in line: if ch == "{": depth += 1 seen = True elif ch == "}": depth -= 1 if seen and depth == 0: return j elif ch == ";" and not seen and depth == 0: return j raise SystemExit("unbalanced item") def whole_file(path: str) -> None: src = ROOT / path dest = OUT / path dest.parent.mkdir(parents=True, exist_ok=True) src.rename(dest) print(f"moved {path} -> scratch/withdrawn/{path}") def items(path: str, names: list[str]) -> None: p = ROOT / path lines = p.read_text().split("\n") for name in names: pat = re.compile(rf"^\s*(pub(\([a-z]+\))?\s+)?(const\s+fn|fn|const|static)\s+{re.escape(name)}\b") hits = [i for i, line in enumerate(lines) if pat.match(line)] if len(hits) != 1: print(f"{name}: {len(hits)} matches, skipped") continue i = hits[0] start = _comment_start(lines, i) end = _brace_end(lines, i) _stash(path, name, "\n".join(lines[start : end + 1])) print(f"{name}: cut {end + 1 - start} lines") del lines[start : end + 1] if start < len(lines) and start > 0 and not lines[start].strip() and not lines[start - 1].strip(): del lines[start] p.write_text("\n".join(lines)) def fields(path: str, names: list[str]) -> None: p = ROOT / path lines = p.read_text().split("\n") for name in names: pat = re.compile(rf"^\s*(pub(\([a-z]+\))?\s+)?{re.escape(name)}\s*:") count = 0 while True: hits = [i for i, line in enumerate(lines) if pat.match(line)] if not hits: break i = hits[0] start = _comment_start(lines, i) end = i depth = 0 for j in range(i, len(lines)): code = re.sub(r"//.*", "", lines[j]) depth += code.count("(") + code.count("[") + code.count("{") depth -= code.count(")") + code.count("]") + code.count("}") end = j if depth <= 0 and code.rstrip().endswith(","): break if depth < 0: break _stash(path, f"field {name}", "\n".join(lines[start : end + 1])) del lines[start : end + 1] count += 1 print(f"{name}: cut {count} occurrence(s)") p.write_text("\n".join(lines)) def moddoc(path: str) -> None: p = ROOT / path lines = p.read_text().split("\n") idx = [i for i, line in enumerate(lines) if line.startswith("//!")] if not idx: print(f"{path}: no module doc") return start, end = idx[0], idx[-1] _stash(path, "module doc", "\n".join(lines[start : end + 1])) del lines[start : end + 1] p.write_text("\n".join(lines)) print(f"{path}: module doc cut, {end + 1 - start} lines") def faults(script: str, src: str) -> None: p = ROOT / script text = p.read_text() lines = text.split("\n") sys.path.insert(0, str(p.parent)) ns: dict = {"__file__": str(p)} body = text.split("\nLOCKSTEP = [")[0] exec(compile(body, script, "exec"), ns) # noqa: S102 (our own script: the FAULTS table) for name, (file, old, _new, _why) in ns["FAULTS"].items(): target = ROOT / src / file if target.exists() and old in target.read_text(): continue i = next(k for k, line in enumerate(lines) if line.startswith(f' "{name}": (')) end = next(k for k in range(i, len(lines)) if lines[k] == " ),") _stash(script, f"fault {name}", "\n".join(lines[i : end + 1])) del lines[i : end + 1] print(f"fault {name}: cut ({file})") p.write_text("\n".join(lines)) def main() -> None: cmd, path, *rest = sys.argv[1:] if cmd == "file": whole_file(path) elif cmd == "items": items(path, rest) elif cmd == "fields": fields(path, rest) elif cmd == "moddoc": moddoc(path) elif cmd == "faults": faults(path, rest[0]) else: raise SystemExit(__doc__) if __name__ == "__main__": main()