Files
marqdo-cli/tools/pack-stdlib.py
T
cflmy 1f6fd644a0 feat(ext): single callable binding, provider v2 CLI, capability handlers, stdlib aliases
Implements RFC-EXT-0001 PR-00..PR-07 first pass:
- CallSiteKey/CallBindingTable + one frontend binding pass; runtime dispatch by identity
- prepare composition root shared by check/run/inspect/Analysis; exact plugin reconciliation
- provider.json v2 --provider/--allow-provider worker CLI with fail-closed verification
- capability handler registry with real fs.read and audit
- verified stdlib public aliases replace print_override/AST scan
- docs: specs, ADR-0054/0055/0057/0058, README, CI/release gates, evidence

PR-04 extension selection/lock v4 and final frozen-binary evidence remain.
2026-11-22 00:17:36 +08:00

109 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Build a deterministic offline marqdo-stdlib package.
The archive is intended for the separate external stdlib repository/release
workflow, not for the CLI's in-repo development fallback. It contains the
Markdown stdlib tree, stdlib.json and README.md, with stable mtimes/ownership.
"""
import hashlib
import io
import json
import os
import tarfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
STDLIB = ROOT / "stdlib"
DIST = ROOT / "dist"
def aliases_digest(manifest: dict) -> str:
digest = hashlib.sha256()
for alias in sorted(
manifest.get("public_aliases", []),
key=lambda a: (a["target_module"], a["target_function"]),
):
digest.update(alias["target_module"].encode())
digest.update(b"\0")
digest.update(alias["target_function"].encode())
digest.update(b"\0")
for name in sorted(alias["public_names"]):
digest.update(name.encode())
digest.update(b"\0")
return f"sha256:{digest.hexdigest()}"
def tree_digest() -> str:
digest = hashlib.sha256()
for path in sorted(
STDLIB.rglob("*.mq.md"), key=lambda p: p.relative_to(STDLIB).as_posix()
):
relative = path.relative_to(STDLIB).as_posix()
digest.update(relative.encode())
digest.update(b"\0")
digest.update(path.read_bytes())
digest.update(b"\0")
return digest.hexdigest()
def main() -> int:
manifest = json.loads((STDLIB / "stdlib.json").read_text())
version = manifest["version"]
digest = tree_digest()
if manifest["digest"] != f"sha256:{digest}":
raise SystemExit("stdlib.json digest does not match the tree")
alias_digest = aliases_digest(manifest)
if manifest.get("aliases_digest") != alias_digest:
raise SystemExit("stdlib.json aliases_digest does not match public_aliases")
files = sorted(
[p for p in STDLIB.rglob("*") if p.is_file() and p.name != "stdlib.json"]
+ [STDLIB / "stdlib.json"],
key=lambda p: p.relative_to(STDLIB).as_posix(),
)
DIST.mkdir(exist_ok=True)
archive = DIST / f"marqdo-stdlib-{version}.tar.gz"
with archive.open("wb") as raw:
with gzip_open(raw) as gz:
with tarfile.open(fileobj=gz, mode="w") as tar:
for path in files:
relative = path.relative_to(STDLIB).as_posix()
info = tarfile.TarInfo(name=relative)
data = path.read_bytes()
info.size = len(data)
info.mode = 0o644
info.uid = info.gid = 0
info.uname = info.gname = "root"
info.mtime = 0
tar.addfile(info, io.BytesIO(data))
archive_sha = hashlib.sha256(archive.read_bytes()).hexdigest()
descriptor = {
"schema": "marqdo.stdlib-package",
"schema_version": 1,
"name": manifest["name"],
"version": version,
"tree_digest": f"sha256:{digest}",
"aliases_digest": alias_digest,
"archive": archive.name,
"archive_sha256": f"sha256:{archive_sha}",
"files": [p.relative_to(STDLIB).as_posix() for p in files],
}
descriptor_path = DIST / f"marqdo-stdlib-{version}.json"
descriptor_path.write_text(json.dumps(descriptor, indent=2) + "\n")
(DIST / f"{archive.name}.sha256").write_text(
f"{archive_sha} {archive.name}\n"
)
print(json.dumps(descriptor, indent=2))
return 0
def gzip_open(raw):
import gzip
return gzip.GzipFile(fileobj=raw, mode="wb", mtime=0, filename="")
if __name__ == "__main__":
raise SystemExit(main())