"""One-off, auditable source relocation; NOT a build/runtime dependency. The committed single-file plugin becomes the source of truth after migration. Read the immutable pre-migration Git revision, preserve source text, and change only imports, module qualification and the three colliding private names. Run with --write once; without it, verify the initial migration is reproducible. Never run against a plugin with subsequent business edits expecting to keep them. """ from __future__ import annotations import argparse import ast import hashlib import json from pathlib import Path import re import subprocess ROOT = Path(__file__).resolve().parents[1] SOURCE_REVISION = "62c2388" MODULES = ( "ita_protocol", "ita_calendar_query", "ita_calendar_book", "ita_booking_final", "ita_reservation", "ita_final_read_port", "ita_calendar_session", "ita_otp", "ita_turnstile", "ita_workflow", "ita_plugin", ) RENAMES = { "ita_protocol": {"time": "datetime_time"}, "ita_booking_final": {"_text": "_final_text"}, "ita_reservation": {"_text": "_reservation_text"}, "ita_otp": {"_EMAIL_PATTERN": "_OTP_EMAIL_PATTERN"}, } def original(module: str) -> str: return subprocess.check_output( ["git", "show", f"{SOURCE_REVISION}:plugins/{module}.py"], cwd=ROOT, text=True, ) def relocate(module: str, source: str) -> str: """Splice AST-located tokens; never reformat/rewrite a function or JS body.""" lines = source.splitlines(keepends=True) offsets = [0] for line in lines: offsets.append(offsets[-1] + len(line)) def pos(line, byte_column): return offsets[line - 1] + len(lines[line - 1].encode()[:byte_column].decode()) tree = ast.parse(source) edits = [] removed = [] renames = RENAMES.get(module, {}) def replace(node, value): edits.append((pos(node.lineno, node.col_offset), pos(node.end_lineno, node.end_col_offset), value)) for node in ast.walk(tree): if isinstance(node, ast.ImportFrom): if node.module == "__future__": replace(node, "") removed.append(node) elif node.module and node.module.startswith("plugins.ita_"): # Retain existing private aliases, not a module object/cache. aliases = [f"{a.asname} = {a.name}" for a in node.names if a.asname] replace(node, "; ".join(aliases)) removed.append(node) elif node.module == "plugins": assert module == "ita_workflow" and len(node.names) == 1 assert node.names[0].asname == "preparation" replace(node, "") removed.append(node) elif module == "ita_protocol" and node.module == "datetime": replace(node, "from datetime import date, datetime, time as datetime_time") removed.append(node) elif isinstance(node, ast.Assign) and any( isinstance(t, ast.Name) and t.id == "__all__" for t in node.targets): replace(node, "") removed.append(node) for node in ast.walk(tree): if any(parent.lineno <= getattr(node, "lineno", 0) <= parent.end_lineno for parent in removed): continue if isinstance(node, ast.Name) and node.id in renames: replace(node, renames[node.id]) elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in renames: start = pos(node.lineno, node.col_offset) + len("def ") edits.append((start, start + len(node.name), renames[node.name])) elif (module == "ita_workflow" and isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name) and node.value.id == "preparation"): replace(node, node.attr) last = len(source) + 1 for start, end, value in sorted(edits, reverse=True): assert end <= last, "overlapping relocation edits" source = source[:start] + value + source[end:] last = start ast.parse(source) return source.strip() + "\n" def assemble() -> tuple[str, dict]: parts = ['"""Prenot@Mi plugin: one uploadable runtime file.\n\n' 'Sections below were mechanically relocated from commit 62c2388.\n' 'Maintain this file directly; tools/consolidate_ita_plugin.py is an audit\n' 'of the initial move, not a build step. Shared host APIs remain shared.\n' '"""\nfrom __future__ import annotations\n'] evidence = {"sourceRevision": SOURCE_REVISION, "target": "plugins/ita_plugin.py", "integrationChanges": ["ItaPlugin.version: 1.0.0 -> 1.0.1", "trim Python-only trailing whitespace", "retain process-wide OTP lock on the existing shared vs_plg host module"], "sections": []} for module in MODULES: source = original(module) moved = relocate(module, source) if module == "ita_plugin": assert moved.count(' version = "1.0.0"') == 1 moved = moved.replace(' version = "1.0.0"', ' version = "1.0.1"') if module == "ita_workflow": assert moved.count("_OTP_INPUT_LOCK = Lock()") == 1 moved = moved.replace("_OTP_INPUT_LOCK = Lock()", '''# The existing factory executes this file separately for each instance. # Keep only the primitive process-wide lock on the shared host module; never # cache plugin functions/classes there. setdefault preserves concurrent loads # and hot reloads without requiring another Italy file or a host code change. import vs_plg as _ita_host _OTP_INPUT_LOCK = vars(_ita_host).setdefault("_coordinate_ita_otp_input_lock", Lock())''') # Keep literal values (including JavaScript) byte-for-byte intact. literal_lines = {line for node in ast.walk(ast.parse(moved)) if isinstance(node, ast.Constant) and isinstance(node.value, str) for line in range(node.lineno, node.end_lineno + 1)} moved = "".join(line if number in literal_lines else line.rstrip() + "\n" for number, line in enumerate(moved.splitlines(keepends=True), 1)) parts.append(f"\n\n# {'=' * 76}\n# {module.removeprefix('ita_').upper()} (from plugins/{module}.py)\n# {'=' * 76}\n\n{moved}") evidence["sections"].append({ "source": f"plugins/{module}.py", "sourceSha256": hashlib.sha256(source.encode()).hexdigest(), "relocatedSha256": hashlib.sha256(moved.encode()).hexdigest(), "renames": RENAMES.get(module, {}), }) result = "".join(parts) ast.parse(result) evidence["targetSha256"] = hashlib.sha256(result.encode()).hexdigest() return result, evidence def retarget_consumers(source: str) -> str: helpers = set(MODULES) - {"ita_plugin"} # Preserve local aliases used by tests/tools, but they now refer to the # single public module. No compatibility shims ship beside the plugin. def imports(match): module, alias = match.group(1), match.group(2) if module not in helpers: return match.group(0) return "from plugins import ita_plugin as " + (alias or module) source = re.sub(r"from plugins import (ita_\w+)(?: as (\w+))?", imports, source) for module in helpers: source = source.replace("plugins." + module, "plugins.ita_plugin") source = source.replace(f'"{module}.py"', '"ita_plugin.py"') source = source.replace(f'plugins/{module}.py', 'plugins/ita_plugin.py') source = source.replace("ita_workflow.preparation", "ita_workflow") source = source.replace("workflow.preparation", "workflow") return source def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--write", action="store_true", help="apply the initial relocation (overwrites target)") args = parser.parse_args() source, manifest = assemble() target = ROOT / "plugins/ita_plugin.py" manifest_path = ROOT / "docs/ita-single-file-migration.json" if not args.write: if target.read_text() != source: raise SystemExit("Target differs from initial relocation (may contain later integration edits).") print("Italy initial relocation is reproducible") return # Refuse to overwrite an existing consolidated file or local source edits. for module in MODULES: path = ROOT / f"plugins/{module}.py" if not path.exists() or path.read_text() != original(module): raise SystemExit(f"Refusing to replace modified/missing source: {path.relative_to(ROOT)}") target.write_text(source) manifest_path.write_text(json.dumps(manifest, indent=2) + "\n") changed = [] for folder in ("test", "tools"): for path in sorted((ROOT / folder).glob("*.py")): if path.resolve() == Path(__file__).resolve(): continue old = path.read_text() new = retarget_consumers(old) if new != old: ast.parse(new) path.write_text(new) changed.append(str(path.relative_to(ROOT))) for module in MODULES[:-1]: (ROOT / f"plugins/{module}.py").unlink() print(json.dumps({"relocatedModules": len(MODULES), "updatedConsumers": changed}, indent=2)) if __name__ == "__main__": main()