#!/usr/bin/env python3 """Bring modules across from the reference, and record where each came from. scripts/bring.py [ ...] A source path is relative to the reference, under `app/` or `tests/`. Each file is copied to the matching place here (`app/x.py` becomes `execution_engine/x.py`) with its imports of `app` renamed, and nothing else changed. A module that is about the reference's own subject is not part of the engine: it goes to `tests/reference_workflow/`, the example Workflow the engine's tests run (ADR-0001). REFERENCE.lock.json records, for every file brought, the reference's commit and the hash of the file as it stood there, so a later look at the reference can tell which of them have moved since. The reference is only read. It must have no uncommitted change to a file brought, or the commit recorded would not describe what was copied. """ from __future__ import annotations import hashlib import json import re import subprocess import sys from pathlib import Path HERE = Path(__file__).resolve().parent.parent LOCK = HERE / "REFERENCE.lock.json" PACKAGE = "execution_engine" # The reference's modules that are about its subject, and where each goes. EXAMPLE = "tests/reference_workflow" EXAMPLE_MODULES = { "app/workflows/chain.py": "chain.py", "app/workflows/target_architecture.py": "target_architecture.py", "app/workflows/changes.py": "changes.py", "app/workflows/work_breakdown.py": "work_breakdown.py", "app/tools/pillars.py": "pillars.py", "app/tools/meaning.py": "meaning.py", "app/tools/local.py": "local.py", "app/tools/modules.py": "modules.py", "app/tools/coverage.py": "coverage.py", "app/tools/design_review.py": "design_review.py", "app/tools/requirements_review.py": "requirements_review.py", "app/tools/schema_review.py": "schema_review.py", "app/tools/assessment.py": "assessment.py", "app/presentation.py": "presentation.py", } _IMPORT = re.compile(r"^(\s*)(from|import) app(\.|\s)", re.MULTILINE) _DOTTED = re.compile(r"(? str: return subprocess.run( ["git", "-C", str(reference), *args], check=True, capture_output=True, text=True, ).stdout.strip() def _dotted(path: str) -> str: return path.removesuffix(".py").replace("/", ".") def destination(source: str) -> Path: if source in EXAMPLE_MODULES: return HERE / EXAMPLE / EXAMPLE_MODULES[source] if source.startswith("app/"): return HERE / PACKAGE / source.removeprefix("app/") if source.startswith("tests/"): return HERE / source raise SystemExit(f"{source}: only files under app/ or tests/ are brought") def renamed(text: str, is_test: bool = False) -> str: # The example's modules first, longest name first, so the general rule below # does not send them into the engine. example = { _dotted(source): _dotted(f"{EXAMPLE}/{name}") for source, name in EXAMPLE_MODULES.items() } if is_test: # A test that named the reference's Deliverables now takes them from the # example Workflow, where the reference's graph is declared. example["app.workflows.deliverables"] = _dotted(f"{EXAMPLE}/workflow") for source in sorted(example, key=len, reverse=True): text = re.sub(rf"(? int: if len(argv) < 2: print(__doc__) return 2 reference = Path(argv[0]).resolve() sources = argv[1:] dirty = git(reference, "status", "--porcelain", "--", *sources) if dirty: raise SystemExit(f"the reference has uncommitted changes to:\n{dirty}") commit = git(reference, "rev-parse", "HEAD") lock = json.loads(LOCK.read_text(encoding="utf-8")) if LOCK.is_file() else {} lock.setdefault("reference", git(reference, "remote", "get-url", "origin")) files = lock.setdefault("files", {}) for source in sources: original = (reference / source).read_bytes() target = destination(source) target.parent.mkdir(parents=True, exist_ok=True) text = renamed(original.decode("utf-8"), source.startswith("tests/")) target.write_text(text, encoding="utf-8") files[str(target.relative_to(HERE))] = { "source": source, "commit": commit, "sha256": hashlib.sha256(original).hexdigest(), } print(f"{source} -> {target.relative_to(HERE)}") lock["files"] = dict(sorted(files.items())) LOCK.write_text(json.dumps(lock, indent=2) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))