#!/usr/bin/env python3 """Insert the derived power-on flow analysis into a project's README.md. `PROMPTS.md` asks every project README to carry a power-on flow analysis: the RESET/NMI/main-loop walk. The walk is derived from the disassembly by `_tools/derive_poweron.py`, so it cannot drift from the source. The section is placed just before the "## Pass 3 data-table inventory" if there is one, and otherwise after the "## Status" section. A README that already has the section is left alone. Usage: python3 _tools/add_poweron.py [ ...] """ import subprocess import sys from pathlib import Path HERE = Path(__file__).resolve().parent def main(): for proj in sys.argv[1:]: d = Path(proj) rd = d / "README.md" if not rd.is_file(): print(f"{proj}: no README.md") continue text = rd.read_text() if "## Power-on flow analysis" in text: print(f"{proj}: already has one") continue res = subprocess.run( [sys.executable, str(HERE / "derive_poweron.py"), str(d)], capture_output=True, text=True) if res.returncode != 0: print(f"{proj}: derive_poweron failed: {res.stderr.strip()}") continue section = res.stdout.rstrip() + "\n\n" marker = "## Pass 3 data-table inventory" if marker in text: text = text.replace(marker, section + marker, 1) else: marker = "## ROM" if marker in text: text = text.replace(marker, section + marker, 1) else: text = text.rstrip() + "\n\n" + section rd.write_text(text) print(f"{proj}: power-on flow analysis added") if __name__ == "__main__": main()