GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Automation and CI / globalgrid2050 / scripts

GlobalGrid2050 Make Publication and 2 more files

A Python command-line tool in the scripts folder that runs git (`run_git`), detects style (`detect_style`) and converts to declared path (`to_declared_path`), using SHA-256.

641 lines of Python from globalgrid2050, in 3 files. Topic: Automation and CI. Section 1771 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. GlobalGrid2050 Make Publication storage · globalgrid2050 / scripts/make_publication.py lines 1–453
  2. GlobalGrid2050 Measure Homepage Version other · globalgrid2050 / scripts/measure_homepage_version.py lines 1–47
  3. GlobalGrid2050 Modularize V3 storage · globalgrid2050 / scripts/modularize_v3.py lines 1–141

GlobalGrid2050 Make Publication

A Python command-line tool in the scripts folder that runs git (`run_git`), detects style (`detect_style`) and converts to declared path (`to_declared_path`), using SHA-256.

storageglobalgrid2050 / scripts/make_publication.py · lines 1–453 of 453 · Entry point · pattern tracked_files (shared by 3 files)

What it does
It loads data. Defines 25 functions or classes, including GitError, run_git, repo_root_of, committed_bytes, BatchReader. Uses libraries __future__, argparse, hashlib, json, os. Side effects: reads or writes files; starts processes.
Inputs & outputs
In: JSON file publication.json; command-line arguments.
Downstream impact
Used directly by 1 file: GlobalGrid2050 Test Make Publication (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 Project Signals Acid Workflow (globalgrid2050).
What it achieves
It loads existing (`load_existing`) and is built from the git error class (`GitError`) and the repo root of helper (`repo_root_of`). Generate and verify testcode publication manifests from committed bytes. It accepts `--check`, `--strict` and `--rev`.
Why it matters
Checked by GlobalGrid2050 Test Make Publication.
Application
globalgrid2050 · scripts · Python CLI
Uses
__future__, argparse, hashlib, json, os, subprocess, sys
Used by
imported by GlobalGrid2050 Test Make Publication (import)
Produces
GitError, run_git, repo_root_of, committed_bytes, BatchReader, tracked_files, entry_path_key, declared_entries, same_declaration, detect_style, to_declared_path, build
Defines here
GitError, run_git, repo_root_of, committed_bytes, BatchReader, __init__, read, close, __enter__, __exit__, tracked_files, entry_path_key, declared_entries, norm, same_declaration, detect_style, to_declared_path, build, extra_for, render, load_existing, crlf_expansion, classify, process, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 scripts/make_publication.py:1-453 · blob 2d40497cee9f60a71860af7b02cd792308b0d217 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem, process · domain stage: unknown · users: none · canonical: production_core
#!/usr/bin/env python3
"""Generate and verify testcode publication manifests from committed bytes.

WHY THIS EXISTS
---------------
Every digest in this repository is taken over git blob content, which is LF,
and GitHub Pages serves those same bytes. `.gitattributes` now pins the working
copy to LF as well, but that file is a recent fix and it did not retroactively
correct manifests whose numbers were recorded on a CRLF checkout. Three such
declarations exist:

    testcode/202609060432/publication.json  source-manifest.json
    testcode/202609150010/publication.json  merged/result.json
    testcode/202609150010/publication.json  runs/local-result.json

Each declares the size and sha256 of the CRLF expansion of a file. A CRLF
expansion is exactly `len(lf) + <number of line endings>` bytes long and has a
wholly unrelated digest, so those three manifests attest bytes that GitHub
Pages has never served.

The durable fix is not to normalise at the point of comparison -- that was
tried, four times, and each fix only covered one caller -- but to stop the
working copy being a source of digests at all. This generator reads bytes from
the object database (`git cat-file blob`), so the numbers it writes are the
numbers that ship, identically on Windows, on a runner, and in a container.
`--check` re-derives every declaration and exits non-zero on any drift.

WHAT IT PRESERVES
-----------------
The archive holds two path conventions (paths relative to the stamp folder, and
paths relative to the repository root) and two shapes for the `files` block (a
list of objects carrying `path`, and an object keyed by path). Rewriting an
archived manifest into one house style would be a large diff that changes files
nobody has touched, and would make `--check` fail on 49 folders that are in
fact correct. So both conventions are inferred from the manifest already
present and reproduced; only the numbers are recomputed.

USAGE
-----
    python scripts/make_publication.py testcode/202609150010
    python scripts/make_publication.py testcode/202609150010 --check
    python scripts/make_publication.py testcode/*/ --check --quiet

Exit status is 0 when every folder is in order, 1 otherwise.

Dependency-free: the standard library, plus the `git` executable that having a
checkout at all already requires.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import os
import subprocess
import sys

SCHEMA = "globalgrid2050.testcode-publication.v1"
MANIFEST_NAME = "publication.json"

# A manifest cannot declare its own digest: writing the digest changes the
# bytes the digest is of.
SELF_DESCRIBING = {"publication.json"}


class GitError(Exception):
    pass


# --------------------------------------------------------------------------
# git access -- the only source of bytes
# --------------------------------------------------------------------------

def run_git(args, repo_root, binary=False):
    proc = subprocess.run(["git"] + args, cwd=repo_root,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
    if proc.returncode != 0:
        raise GitError("git %s: %s" % (" ".join(args),
                                       proc.stderr.decode("utf-8", "replace").strip()))
    return proc.stdout if binary else proc.stdout.decode("utf-8")


def repo_root_of(path):
    start = path if os.path.isdir(path) else os.path.dirname(path)
    return os.path.abspath(run_git(["rev-parse", "--show-toplevel"], start).strip())


def committed_bytes(repo_root, rel_path, rev):
    """Bytes of rel_path exactly as git stores them.

    `rev` may be any commit-ish, or the empty string meaning the index -- the
    index form lets a new stamp folder be manifested in the same commit that
    adds it, which is the normal publishing case.
    """
    return run_git(["cat-file", "blob", "%s:%s" % (rev, rel_path)], repo_root, binary=True)


class BatchReader:
    """One `git cat-file --batch` process for a whole folder.

    Spawning a git process per file costs more than reading the files: the
    first version of this script took minutes to sweep the archive on Windows,
    which is long enough that nobody would put it in CI, and a check nobody
    runs prevents nothing.
    """

    def __init__(self, repo_root):
        self.proc = subprocess.Popen(
            ["git", "cat-file", "--batch"], cwd=repo_root,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    def read(self, spec):
        self.proc.stdin.write(spec.encode("utf-8") + b"\n")
        self.proc.stdin.flush()
        header = self.proc.stdout.readline().decode("utf-8", "replace").strip()
        parts = header.split()
        if len(parts) < 3 or parts[1] != "blob":
            raise GitError("cat-file --batch: %r -> %r" % (spec, header))
        size = int(parts[2])
        buf = b""
        while len(buf) < size:
            chunk = self.proc.stdout.read(size - len(buf))
            if not chunk:
                raise GitError("cat-file --batch: short read for %r" % spec)
            buf += chunk
        self.proc.stdout.read(1)  # trailing newline git appends
        return buf

    def close(self):
        try:
            self.proc.stdin.close()
            self.proc.wait(timeout=10)
        except Exception:  # noqa: BLE001
            self.proc.kill()
        finally:
            for s in (self.proc.stdout, self.proc.stderr):
                try:
                    s.close()
                except Exception:  # noqa: BLE001
                    pass

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        self.close()
        return False


def tracked_files(repo_root, stamp_rel, rev):
    if rev:
        out = run_git(["ls-tree", "-r", "--name-only", rev, "--", stamp_rel], repo_root)
    else:
        out = run_git(["ls-files", "--cached", "--", stamp_rel], repo_root)
    paths = [p.strip() for p in out.replace("\r\n", "\n").split("\n") if p.strip()]
    return sorted(p for p in paths if os.path.basename(p) not in SELF_DESCRIBING)


# --------------------------------------------------------------------------
# reading whatever shape the existing manifest happens to use
# --------------------------------------------------------------------------

# The archive names the path field two ways. Both are read; whichever a
# manifest already uses is the one written back.
PATH_KEYS = ("path", "file")


def entry_path_key(doc):
    f = (doc or {}).get("files")
    if isinstance(f, list):
        for e in f:
            if isinstance(e, dict):
                for k in PATH_KEYS:
                    if e.get(k):
                        return k
    return "path"


def declared_entries(doc):
    """{declared_path: {"bytes": int|None, "sha256": str|None, "_extra": {...}}}.

    `_extra` carries per-entry keys this generator does not derive (an
    `mtime_utc`, say) so that regenerating an archived manifest does not strip
    fields somebody put there on purpose.
    """
    out = {}
    f = (doc or {}).get("files")

    def norm(e, p):
        extra = {k: v for k, v in e.items()
                 if k not in ("bytes", "sha256") and k not in PATH_KEYS}
        return {"bytes": e.get("bytes"),
                "sha256": (e.get("sha256") or "").lower() or None,
                "_extra": extra}

    if isinstance(f, list):
        for e in f:
            if not isinstance(e, dict):
                continue
            p = next((e[k] for k in PATH_KEYS if e.get(k)), None)
            if p:
                out[p] = norm(e, p)
    elif isinstance(f, dict):
        for p, e in f.items():
            if isinstance(e, dict):
                out[p] = norm(e, p)
    return out


def same_declaration(a, b):
    """Digest comparison only -- `_extra` is metadata, not an attestation."""
    return a["bytes"] == b["bytes"] and a["sha256"] == b["sha256"]


def detect_style(doc, stamp_rel):
    """('list'|'dict', 'stamp'|'repo') inferred from the manifest present."""
    f = (doc or {}).get("files")
    shape = "dict" if isinstance(f, dict) else "list"
    paths = list(declared_entries(doc))
    prefix = stamp_rel.rstrip("/") + "/"
    if paths and all(p.startswith(prefix) for p in paths):
        return shape, "repo"
    return shape, "stamp"


def to_declared_path(repo_rel, stamp_rel, path_style):
    if path_style == "repo":
        return repo_rel
    return os.path.relpath(repo_rel, stamp_rel).replace(os.sep, "/")


# --------------------------------------------------------------------------
# building and rendering
# --------------------------------------------------------------------------

def build(repo_root, stamp_rel, rev, existing_doc, shape, path_style, pkey="path"):
    blobs = {}
    rows = []
    with BatchReader(repo_root) as reader:
        for repo_rel in tracked_files(repo_root, stamp_rel, rev):
            blob = reader.read("%s:%s" % (rev, repo_rel))
            blobs[repo_rel] = blob
            rows.append((to_declared_path(repo_rel, stamp_rel, path_style), repo_rel,
                         len(blob), hashlib.sha256(blob).hexdigest()))
    if not rows:
        raise GitError("no tracked files under %s at rev %r" % (stamp_rel, rev or "<index>"))

    # New folders get sorted order, which is deterministic. An existing
    # manifest keeps the order it already has, with any newly tracked file
    # appended in sorted order, so that regenerating a manifest whose digests
    # are already right rewrites nothing at all. A no-op regeneration is what
    # makes it safe to run this over the whole archive.
    prior_entries = declared_entries(existing_doc)
    prior = list(prior_entries)
    if prior:
        rank = {p: i for i, p in enumerate(prior)}
        rows.sort(key=lambda r: (rank.get(r[0], len(rank)), r[0]))

    def extra_for(decl):
        return prior_entries.get(decl, {}).get("_extra") or {}

    if shape == "dict":
        files = {}
        for decl, _repo_rel, n, sha in rows:
            files[decl] = {"bytes": n, "sha256": sha, **extra_for(decl)}
    else:
        files = [{pkey: decl, "bytes": n, "sha256": sha, **extra_for(decl)}
                 for decl, _repo_rel, n, sha in rows]

    doc = {}
    if isinstance(existing_doc, dict):
        # Narrative fields -- name, status, limitations, source commit -- are
        # the point of the manifest and must survive regeneration untouched,
        # in their original order. Only the derived block is recomputed, and
        # no key is added: an archived manifest that has always lacked
        # `schema` should not acquire one merely because its digests were
        # re-verified. Regeneration of a correct manifest is then a no-op.
        # `files` keeps its original position too, so a manifest that carries
        # trailing narrative (`supersedes`, `changes_from_*`) is not reordered.
        for k, v in existing_doc.items():
            doc[k] = files if k == "files" else v
        if "files" not in doc:
            doc["files"] = files
        return doc, blobs, {decl: repo_rel for decl, repo_rel, _n, _s in rows}

    doc["schema"] = SCHEMA
    doc["stamp"] = os.path.basename(stamp_rel)
    doc["files"] = files
    return doc, blobs, {decl: repo_rel for decl, repo_rel, _n, _s in rows}


def render(doc):
    """Deterministic bytes: 2-space indent, LF endings, one trailing newline.

    Emitted as bytes, never through text mode, so the platform's own newline
    translation cannot reintroduce the very defect this script removes.
    """
    text = json.dumps(doc, indent=2, ensure_ascii=False)
    return text.replace("\r\n", "\n").encode("utf-8") + b"\n"


def load_existing(manifest_abs):
    if not os.path.isfile(manifest_abs):
        return None, None
    with open(manifest_abs, "rb") as fh:
        raw = fh.read()
    try:
        return json.loads(raw.decode("utf-8")), raw
    except (ValueError, UnicodeDecodeError):
        return None, raw


# --------------------------------------------------------------------------
# classification -- never guess CRLF, prove it
# --------------------------------------------------------------------------

def crlf_expansion(blob):
    """The bytes a core.autocrlf=true Windows checkout puts on disk."""
    return blob.replace(b"\r\n", b"\n").replace(b"\n", b"\r\n")


def classify(declared, actual_bytes, actual_sha, blob=None):
    """Name the cause. CRLF is claimed only when the CRLF digest proves it.

    A byte delta alone is suggestive, not evidence -- a file can equally have
    been edited. The declaration is CRLF-derived if and only if hashing the
    CRLF expansion of the committed blob reproduces the declared sha256.
    """
    db, dsha = declared.get("bytes"), declared.get("sha256")
    if blob is not None:
        crlf = crlf_expansion(blob)
        if dsha == hashlib.sha256(crlf).hexdigest() and (db is None or db == len(crlf)):
            return ("CRLF drift, proven: the CRLF expansion of the committed blob is "
                    "exactly %d bytes (%d LF + %d line endings) and hashes to the "
                    "declared sha256" % (len(crlf), len(blob), len(crlf) - len(blob)))
        if dsha == actual_sha:
            return "digest agrees; only the declared byte count is wrong"
        return ("not a CRLF expansion: the declared digest matches neither the committed "
                "blob nor its CRLF form")
    if isinstance(db, int) and db > actual_bytes and dsha != actual_sha:
        return ("byte count is %d higher than the blob, consistent with a CRLF expansion "
                "of a %d-byte file holding %d line endings; unverified, blob unavailable"
                % (db - actual_bytes, actual_bytes, db - actual_bytes))
    return "cause unclassified"


# --------------------------------------------------------------------------
# the two modes
# --------------------------------------------------------------------------

def process(stamp_dir, rev="HEAD", check=False, strict=False, quiet=False,
            path_style="auto", out=None):
    out = out or sys.stdout
    stamp_abs = os.path.abspath(stamp_dir).rstrip(os.sep)
    repo_root = repo_root_of(stamp_abs)
    stamp_rel = os.path.relpath(stamp_abs, repo_root).replace(os.sep, "/")
    manifest_abs = os.path.join(stamp_abs, MANIFEST_NAME)

    existing_doc, existing_raw = load_existing(manifest_abs)
    shape, inferred = detect_style(existing_doc, stamp_rel)
    style = inferred if path_style == "auto" else path_style

    doc, blobs, decl_to_repo = build(repo_root, stamp_rel, rev, existing_doc, shape, style,
                                     entry_path_key(existing_doc))
    new_raw = render(doc)
    truth = declared_entries(doc)

    if not check:
        if existing_raw == new_raw:
            if not quiet:
                out.write("ok    %s unchanged (%d files)\n" % (stamp_rel, len(truth)))
            return 0
        with open(manifest_abs, "wb") as fh:
            fh.write(new_raw)
        out.write("wrote %s/%s (%d files)\n" % (stamp_rel, MANIFEST_NAME, len(truth)))
        return 0

    if existing_raw is None:
        out.write("DRIFT %s: no %s\n" % (stamp_rel, MANIFEST_NAME))
        return 1
    if existing_doc is None:
        out.write("DRIFT %s: %s is not valid JSON\n" % (stamp_rel, MANIFEST_NAME))
        return 1

    old = declared_entries(existing_doc)
    problems = 0
    lines = []
    for p in sorted(set(old) | set(truth)):
        o, n = old.get(p), truth.get(p)
        if o is None:
            problems += 1
            lines.append("  + %s  tracked but undeclared: %d bytes / %s"
                         % (p, n["bytes"], n["sha256"]))
            continue
        if n is None:
            problems += 1
            lines.append("  - %s  declared but not tracked at %s" % (p, rev or "<index>"))
            continue
        if not same_declaration(o, n):
            problems += 1
            blob = blobs.get(decl_to_repo.get(p))
            lines.append("  ! %s" % p)
            lines.append("      declared %s bytes / %s" % (o["bytes"], o["sha256"]))
            lines.append("      actual   %s bytes / %s" % (n["bytes"], n["sha256"]))
            lines.append("      %s" % classify(o, n["bytes"], n["sha256"], blob))

    formatting_differs = existing_raw != new_raw
    if problems:
        out.write("DRIFT %s\n" % stamp_rel)
        out.write("\n".join(lines) + "\n")
        return 1
    if strict and formatting_differs:
        out.write("DRIFT %s: every digest agrees, but the manifest is not byte-identical "
                  "to the generated form (--strict)\n" % stamp_rel)
        return 1
    if not quiet:
        note = "" if not formatting_differs else "; house formatting differs"
        out.write("ok    %s (%d files%s)\n" % (stamp_rel, len(truth), note))
    return 0


def main(argv=None):
    ap = argparse.ArgumentParser(description="Publication manifests from committed bytes.")
    ap.add_argument("stamp_dirs", nargs="+", help="testcode/<stamp> folders")
    ap.add_argument("--check", action="store_true",
                    help="verify only; write nothing; exit 1 on any drift")
    ap.add_argument("--strict", action="store_true",
                    help="with --check, also require byte-identical house formatting")
    ap.add_argument("--rev", default="HEAD",
                    help="revision to read bytes from; '' means the git index")
    ap.add_argument("--path-style", choices=("auto", "stamp", "repo"), default="auto",
                    help="declared path convention; auto follows the existing manifest")
    ap.add_argument("--quiet", action="store_true", help="print only drift")
    args = ap.parse_args(argv)

    worst = 0
    for d in args.stamp_dirs:
        if not os.path.isdir(d):
            print("DRIFT %s: not a directory" % d)
            worst = 1
            continue
        try:
            worst = max(worst, process(d, args.rev, args.check, args.strict,
                                       args.quiet, args.path_style))
        except GitError as exc:
            print("ERROR %s: %s" % (d, exc))
            worst = 1
    return worst


if __name__ == "__main__":
    sys.exit(main())

GlobalGrid2050 Measure Homepage Version

A Python command-line tool in the scripts folder that measures homepage snapshot files for reversible public homepage edits, using SHA-256.

otherglobalgrid2050 / scripts/measure_homepage_version.py · lines 1–47 of 47 · Entry point · pattern measure (shared by 35 files)

What it does
Defines measure, main. Uses libraries __future__, hashlib, json, sys, pathlib. Side effects: reads or writes files.
Inputs & outputs
In: command-line arguments.
Downstream impact
Used directly by 2 files: GlobalGrid2050 Verify VEDIC Homepage Release (globalgrid2050); GlobalGrid2050 Homepage Restore Point Check Workflow (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 VEDIC Homepage Promotion Workflow (globalgrid2050).
What it achieves
Defines `measure`.
Why it matters
Run by GlobalGrid2050 Verify VEDIC Homepage Release and GlobalGrid2050 Homepage Restore Point Check Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
__future__, hashlib, json, sys, pathlib
Used by
run by GlobalGrid2050 Verify VEDIC Homepage Release (script call), run by GlobalGrid2050 Homepage Restore Point Check Workflow (workflow run)
Produces
measure, main
Defines here
measure, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 scripts/measure_homepage_version.py:1-47 · blob 40775c0e4e74352e9b00a4a7b49b39d67076ac09 · GitHub at these lines
State
achieves: name · context: readme_derived · stage: ingest · effects: filesystem · domain stage: unknown · users: none · canonical: production_core
#!/usr/bin/env python3
"""Measure homepage snapshot files for reversible public homepage edits.

This script is intentionally small and dependency-free so it can run in GitHub
Actions and locally without setup. It reports line, word, character, byte and
SHA-256 counts for one or more files.
"""

from __future__ import annotations

import hashlib
import json
import sys
from pathlib import Path


def measure(path: Path) -> dict[str, object]:
    raw = path.read_bytes()
    text = raw.decode("utf-8")
    return {
        "file": str(path),
        "lines": len(text.splitlines()),
        "words": len(text.split()),
        "characters": len(text),
        "bytes": len(raw),
        "sha256": hashlib.sha256(raw).hexdigest(),
    }


def main() -> int:
    if len(sys.argv) < 2:
        print("usage: measure_homepage_version.py <file> [<file> ...]", file=sys.stderr)
        return 1

    paths = [Path(arg) for arg in sys.argv[1:]]
    missing = [str(path) for path in paths if not path.exists()]
    if missing:
        print("missing file(s): " + ", ".join(missing), file=sys.stderr)
        return 2

    results = [measure(path) for path in paths]
    print(json.dumps(results, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

GlobalGrid2050 Modularize V3

A Python command-line tool in the scripts folder that reads substations. It extracts inline script (`extract_inline_script`) and writes modules (`write_modules`).

storageglobalgrid2050 / scripts/modularize_v3.py · lines 1–141 of 141 · Entry point · pattern module_tags (shared by 2 files)

What it does
It loads substations. Defines 10 functions or classes, including fail, normalise_identity, is_already_modular, extract_inline_script, slice_section. Uses libraries __future__, pathlib. Side effects: reads or writes files.
Why it exists in the grid workflow
Domain terms in the code: substations.
Downstream impact
Used directly by 1 file: GlobalGrid2050 Modularize V3 Once Workflow (globalgrid2050).
What it achieves
Defines `fail`, `normalise_identity`, `is_already_modular` and 6 more.
Why it matters
Run by GlobalGrid2050 Modularize V3 Once Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
__future__, pathlib
Used by
run by GlobalGrid2050 Modularize V3 Once Workflow (workflow run)
Produces
fail, normalise_identity, is_already_modular, extract_inline_script, slice_section, write_modules, module_tags, remove_v2_duplicates, verify, main
Defines here
fail, normalise_identity, is_already_modular, extract_inline_script, slice_section, write_modules, module_tags, remove_v2_duplicates, verify, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 scripts/modularize_v3.py:1-141 · blob e608dee8aa9d2d7f6a4dc3e5c51f808ab78cf96d · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem · domain stage: unknown · users: none · canonical: production_core
#!/usr/bin/env python3
from __future__ import annotations

from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
APP_DIR = ROOT / "solar-bess-topology-v3"
HTML_PATH = APP_DIR / "indexforgis-sld-v3.html"

MODULES = {
    "gis-sld-v3-map.js": ("// MAP", "// AGGREGATE STATS"),
    "gis-sld-v3-calculations.js": ("// AGGREGATE STATS", "// FINANCIALS"),
    "gis-sld-v3-finance.js": ("// FINANCIALS", "// RENDER TECHNICAL SUMMARY"),
    "gis-sld-v3-ui-core.js": ("// RENDER TECHNICAL SUMMARY", "// DRAWING"),
    "gis-sld-v3-drawing.js": ("// DRAWING", "// EXPORT"),
    "gis-sld-v3-export.js": ("// EXPORT", "// TAB SWITCHING"),
    "gis-sld-v3-ui.js": ("// TAB SWITCHING", None),
}

MODULE_ORDER = list(MODULES.keys())

HEAD_REPLACEMENTS = {
    "GIS SLD Financial Sandbox V2": "GIS SLD Financial Sandbox V3",
    "gis-sld-v2-config.js": "gis-sld-v3-config.js",
    "gis-sld-v2-helpers.js": "gis-sld-v3-helpers.js",
    "gis-sld-v2-state.js": "gis-sld-v3-state.js",
    "gis-sld-v2-substations.js": "gis-sld-v3-substations.js",
    "gis-sld-v2.css": "gis-sld-v3.css",
}


def fail(message: str) -> None:
    raise SystemExit(f"[modularize_v3] ERROR: {message}")


def normalise_identity(text: str) -> str:
    for old, new in HEAD_REPLACEMENTS.items():
        text = text.replace(old, new)
    return text


def is_already_modular(html: str) -> bool:
    return "V3 modular app scripts" in html and all(name in html for name in MODULE_ORDER)


def extract_inline_script(html: str) -> tuple[str, str]:
    start_marker = "\n<script>\n\"use strict\";"
    start = html.find(start_marker)
    if start < 0:
        if is_already_modular(html):
            return html, ""
        fail("inline app script start not found")

    script_content_start = start + len("\n<script>\n")
    end = html.find("\n</script>", script_content_start)
    if end < 0:
        fail("inline app script end not found")

    script_text = html[script_content_start:end]
    html_without = html[:start] + "\n__V3_MODULE_SCRIPT_TAGS__\n" + html[end + len("\n</script>"):]
    return html_without, script_text


def slice_section(script: str, start_marker: str, end_marker: str | None) -> str:
    start = script.find(start_marker)
    if start < 0:
        fail(f"section start not found: {start_marker}")
    end = len(script) if end_marker is None else script.find(end_marker, start + len(start_marker))
    if end < 0:
        fail(f"section end not found: {end_marker}")
    return script[start:end].strip() + "\n"


def write_modules(script: str) -> None:
    if not script:
        return
    script = script.strip()
    if script.startswith('"use strict";'):
        script = script[len('"use strict";'):].lstrip()
    for filename, (start_marker, end_marker) in MODULES.items():
        body = slice_section(script, start_marker, end_marker)
        (APP_DIR / filename).write_text('"use strict";\n\n' + body, encoding="utf-8")


def module_tags() -> str:
    lines = ["<!-- V3 modular app scripts -->"]
    for filename in MODULE_ORDER:
        lines.append("<" + f"script src=\"{filename}\"></" + "script>")
    return "\n".join(lines)


def remove_v2_duplicates() -> None:
    for path in APP_DIR.glob("gis-sld-v2*"):
        if path.is_file():
            path.unlink()


def verify() -> None:
    required = [
        "index.html",
        "indexforgis-sld-v3.html",
        "gis-sld-v3.css",
        "gis-sld-v3-config.js",
        "gis-sld-v3-helpers.js",
        "gis-sld-v3-state.js",
        "gis-sld-v3-substations.js",
        *MODULE_ORDER,
    ]
    missing = [name for name in required if not (APP_DIR / name).exists()]
    if missing:
        fail("missing files: " + ", ".join(missing))
    html = HTML_PATH.read_text(encoding="utf-8")
    bad = [item for item in ["gis-sld-v2", "Financial Sandbox V2", "__V3_MODULE_SCRIPT_TAGS__"] if item in html]
    if bad:
        fail("Forbidden V3 HTML text remains: " + ", ".join(bad))
    if "\n<script>\n\"use strict\";" in html:
        fail("original inline app script still present in V3 HTML")
    for filename in MODULE_ORDER:
        if filename not in html:
            fail("module tag missing from V3 HTML: " + filename)


def main() -> int:
    if not HTML_PATH.exists():
        fail(f"missing HTML: {HTML_PATH}")
    html = normalise_identity(HTML_PATH.read_text(encoding="utf-8"))
    html_without_script, script = extract_inline_script(html)
    write_modules(script)
    if script:
        html = html_without_script.replace("__V3_MODULE_SCRIPT_TAGS__", module_tags())
        HTML_PATH.write_text(html, encoding="utf-8")
    else:
        HTML_PATH.write_text(html_without_script, encoding="utf-8")
    remove_v2_duplicates()
    verify()
    print("[modularize_v3] V3 modularisation verified")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())