consolidate_ita_plugin.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """One-off, auditable source relocation; NOT a build/runtime dependency.
  2. The committed single-file plugin becomes the source of truth after migration.
  3. Read the immutable pre-migration Git revision, preserve source text, and change
  4. only imports, module qualification and the three colliding private names.
  5. Run with --write once; without it, verify the initial migration is reproducible.
  6. Never run against a plugin with subsequent business edits expecting to keep them.
  7. """
  8. from __future__ import annotations
  9. import argparse
  10. import ast
  11. import hashlib
  12. import json
  13. from pathlib import Path
  14. import re
  15. import subprocess
  16. ROOT = Path(__file__).resolve().parents[1]
  17. SOURCE_REVISION = "62c2388"
  18. MODULES = (
  19. "ita_protocol", "ita_calendar_query", "ita_calendar_book",
  20. "ita_booking_final", "ita_reservation", "ita_final_read_port",
  21. "ita_calendar_session", "ita_otp", "ita_turnstile", "ita_workflow",
  22. "ita_plugin",
  23. )
  24. RENAMES = {
  25. "ita_protocol": {"time": "datetime_time"},
  26. "ita_booking_final": {"_text": "_final_text"},
  27. "ita_reservation": {"_text": "_reservation_text"},
  28. "ita_otp": {"_EMAIL_PATTERN": "_OTP_EMAIL_PATTERN"},
  29. }
  30. def original(module: str) -> str:
  31. return subprocess.check_output(
  32. ["git", "show", f"{SOURCE_REVISION}:plugins/{module}.py"],
  33. cwd=ROOT, text=True,
  34. )
  35. def relocate(module: str, source: str) -> str:
  36. """Splice AST-located tokens; never reformat/rewrite a function or JS body."""
  37. lines = source.splitlines(keepends=True)
  38. offsets = [0]
  39. for line in lines:
  40. offsets.append(offsets[-1] + len(line))
  41. def pos(line, byte_column):
  42. return offsets[line - 1] + len(lines[line - 1].encode()[:byte_column].decode())
  43. tree = ast.parse(source)
  44. edits = []
  45. removed = []
  46. renames = RENAMES.get(module, {})
  47. def replace(node, value):
  48. edits.append((pos(node.lineno, node.col_offset),
  49. pos(node.end_lineno, node.end_col_offset), value))
  50. for node in ast.walk(tree):
  51. if isinstance(node, ast.ImportFrom):
  52. if node.module == "__future__":
  53. replace(node, "")
  54. removed.append(node)
  55. elif node.module and node.module.startswith("plugins.ita_"):
  56. # Retain existing private aliases, not a module object/cache.
  57. aliases = [f"{a.asname} = {a.name}" for a in node.names if a.asname]
  58. replace(node, "; ".join(aliases))
  59. removed.append(node)
  60. elif node.module == "plugins":
  61. assert module == "ita_workflow" and len(node.names) == 1
  62. assert node.names[0].asname == "preparation"
  63. replace(node, "")
  64. removed.append(node)
  65. elif module == "ita_protocol" and node.module == "datetime":
  66. replace(node, "from datetime import date, datetime, time as datetime_time")
  67. removed.append(node)
  68. elif isinstance(node, ast.Assign) and any(
  69. isinstance(t, ast.Name) and t.id == "__all__" for t in node.targets):
  70. replace(node, "")
  71. removed.append(node)
  72. for node in ast.walk(tree):
  73. if any(parent.lineno <= getattr(node, "lineno", 0) <= parent.end_lineno for parent in removed):
  74. continue
  75. if isinstance(node, ast.Name) and node.id in renames:
  76. replace(node, renames[node.id])
  77. elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in renames:
  78. start = pos(node.lineno, node.col_offset) + len("def ")
  79. edits.append((start, start + len(node.name), renames[node.name]))
  80. elif (module == "ita_workflow" and isinstance(node, ast.Attribute)
  81. and isinstance(node.value, ast.Name) and node.value.id == "preparation"):
  82. replace(node, node.attr)
  83. last = len(source) + 1
  84. for start, end, value in sorted(edits, reverse=True):
  85. assert end <= last, "overlapping relocation edits"
  86. source = source[:start] + value + source[end:]
  87. last = start
  88. ast.parse(source)
  89. return source.strip() + "\n"
  90. def assemble() -> tuple[str, dict]:
  91. parts = ['"""Prenot@Mi plugin: one uploadable runtime file.\n\n'
  92. 'Sections below were mechanically relocated from commit 62c2388.\n'
  93. 'Maintain this file directly; tools/consolidate_ita_plugin.py is an audit\n'
  94. 'of the initial move, not a build step. Shared host APIs remain shared.\n'
  95. '"""\nfrom __future__ import annotations\n']
  96. evidence = {"sourceRevision": SOURCE_REVISION, "target": "plugins/ita_plugin.py",
  97. "integrationChanges": ["ItaPlugin.version: 1.0.0 -> 1.0.1", "trim Python-only trailing whitespace",
  98. "retain process-wide OTP lock on the existing shared vs_plg host module"],
  99. "sections": []}
  100. for module in MODULES:
  101. source = original(module)
  102. moved = relocate(module, source)
  103. if module == "ita_plugin":
  104. assert moved.count(' version = "1.0.0"') == 1
  105. moved = moved.replace(' version = "1.0.0"', ' version = "1.0.1"')
  106. if module == "ita_workflow":
  107. assert moved.count("_OTP_INPUT_LOCK = Lock()") == 1
  108. moved = moved.replace("_OTP_INPUT_LOCK = Lock()", '''# The existing factory executes this file separately for each instance.
  109. # Keep only the primitive process-wide lock on the shared host module; never
  110. # cache plugin functions/classes there. setdefault preserves concurrent loads
  111. # and hot reloads without requiring another Italy file or a host code change.
  112. import vs_plg as _ita_host
  113. _OTP_INPUT_LOCK = vars(_ita_host).setdefault("_coordinate_ita_otp_input_lock", Lock())''')
  114. # Keep literal values (including JavaScript) byte-for-byte intact.
  115. literal_lines = {line for node in ast.walk(ast.parse(moved))
  116. if isinstance(node, ast.Constant) and isinstance(node.value, str)
  117. for line in range(node.lineno, node.end_lineno + 1)}
  118. moved = "".join(line if number in literal_lines else line.rstrip() + "\n"
  119. for number, line in enumerate(moved.splitlines(keepends=True), 1))
  120. parts.append(f"\n\n# {'=' * 76}\n# {module.removeprefix('ita_').upper()} (from plugins/{module}.py)\n# {'=' * 76}\n\n{moved}")
  121. evidence["sections"].append({
  122. "source": f"plugins/{module}.py",
  123. "sourceSha256": hashlib.sha256(source.encode()).hexdigest(),
  124. "relocatedSha256": hashlib.sha256(moved.encode()).hexdigest(),
  125. "renames": RENAMES.get(module, {}),
  126. })
  127. result = "".join(parts)
  128. ast.parse(result)
  129. evidence["targetSha256"] = hashlib.sha256(result.encode()).hexdigest()
  130. return result, evidence
  131. def retarget_consumers(source: str) -> str:
  132. helpers = set(MODULES) - {"ita_plugin"}
  133. # Preserve local aliases used by tests/tools, but they now refer to the
  134. # single public module. No compatibility shims ship beside the plugin.
  135. def imports(match):
  136. module, alias = match.group(1), match.group(2)
  137. if module not in helpers:
  138. return match.group(0)
  139. return "from plugins import ita_plugin as " + (alias or module)
  140. source = re.sub(r"from plugins import (ita_\w+)(?: as (\w+))?", imports, source)
  141. for module in helpers:
  142. source = source.replace("plugins." + module, "plugins.ita_plugin")
  143. source = source.replace(f'"{module}.py"', '"ita_plugin.py"')
  144. source = source.replace(f'plugins/{module}.py', 'plugins/ita_plugin.py')
  145. source = source.replace("ita_workflow.preparation", "ita_workflow")
  146. source = source.replace("workflow.preparation", "workflow")
  147. return source
  148. def main():
  149. parser = argparse.ArgumentParser(description=__doc__)
  150. parser.add_argument("--write", action="store_true", help="apply the initial relocation (overwrites target)")
  151. args = parser.parse_args()
  152. source, manifest = assemble()
  153. target = ROOT / "plugins/ita_plugin.py"
  154. manifest_path = ROOT / "docs/ita-single-file-migration.json"
  155. if not args.write:
  156. if target.read_text() != source:
  157. raise SystemExit("Target differs from initial relocation (may contain later integration edits).")
  158. print("Italy initial relocation is reproducible")
  159. return
  160. # Refuse to overwrite an existing consolidated file or local source edits.
  161. for module in MODULES:
  162. path = ROOT / f"plugins/{module}.py"
  163. if not path.exists() or path.read_text() != original(module):
  164. raise SystemExit(f"Refusing to replace modified/missing source: {path.relative_to(ROOT)}")
  165. target.write_text(source)
  166. manifest_path.write_text(json.dumps(manifest, indent=2) + "\n")
  167. changed = []
  168. for folder in ("test", "tools"):
  169. for path in sorted((ROOT / folder).glob("*.py")):
  170. if path.resolve() == Path(__file__).resolve():
  171. continue
  172. old = path.read_text()
  173. new = retarget_consumers(old)
  174. if new != old:
  175. ast.parse(new)
  176. path.write_text(new)
  177. changed.append(str(path.relative_to(ROOT)))
  178. for module in MODULES[:-1]:
  179. (ROOT / f"plugins/{module}.py").unlink()
  180. print(json.dumps({"relocatedModules": len(MODULES), "updatedConsumers": changed}, indent=2))
  181. if __name__ == "__main__":
  182. main()