69 lines
2.8 KiB
Python
69 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Package the four-file scroll repair for the registered dc-cms-site-nodedc runner."""
|
|
import argparse
|
|
import gzip
|
|
import hashlib
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
import tarfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ENTRIES = (
|
|
"assets/custom/css/app.css",
|
|
"assets/custom/css/out.css",
|
|
"index.html",
|
|
"templates/pages/home/shell.html",
|
|
)
|
|
|
|
|
|
def bundle(root, commit, patch_id):
|
|
if not re.fullmatch(r"[0-9a-f]{40}", commit):
|
|
raise ValueError("full_source_commit_required")
|
|
if not re.fullmatch(r"nodedc-scroll-layout-[0-9]{8}-[0-9]{3}", patch_id):
|
|
raise ValueError("invalid_patch_id")
|
|
files = {
|
|
"manifest.env": (f"id={patch_id}\ncomponent=dc-cms-site-nodedc\ntype=app-overlay\n").encode(),
|
|
"files.txt": ("\n".join(ENTRIES) + "\n").encode(),
|
|
}
|
|
for entry in ENTRIES:
|
|
mode = subprocess.check_output(["git", "ls-tree", commit, "--", entry], cwd=root, text=True)
|
|
if not mode.startswith("100644 blob "):
|
|
raise ValueError(f"regular_committed_file_required:{entry}")
|
|
files["payload/" + entry] = subprocess.check_output(["git", "show", f"{commit}:{entry}"], cwd=root)
|
|
output = io.BytesIO()
|
|
with gzip.GzipFile(fileobj=output, mode="wb", filename="", mtime=0) as compressed:
|
|
with tarfile.open(fileobj=compressed, mode="w", format=tarfile.USTAR_FORMAT) as archive:
|
|
for name, content in sorted(files.items()):
|
|
info = tarfile.TarInfo(name)
|
|
info.size, info.mode, info.mtime = len(content), 0o644, 0
|
|
archive.addfile(info, io.BytesIO(content))
|
|
return output.getvalue()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--source-commit", required=True)
|
|
parser.add_argument("--patch-id", required=True)
|
|
parser.add_argument("--output-dir", required=True, type=Path)
|
|
args = parser.parse_args()
|
|
actual = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip()
|
|
if actual != args.source_commit:
|
|
raise SystemExit("source_commit_mismatch")
|
|
if subprocess.check_output(["git", "status", "--porcelain"], cwd=ROOT, text=True).strip():
|
|
raise SystemExit("clean_committed_source_required")
|
|
data = bundle(ROOT, actual, args.patch_id)
|
|
args.output_dir.mkdir(parents=True, exist_ok=True)
|
|
target = args.output_dir / (args.patch_id + ".tgz")
|
|
with target.open("xb") as stream:
|
|
stream.write(data)
|
|
print(json.dumps({"artifact": str(target.resolve()), "patchId": args.patch_id,
|
|
"component": "dc-cms-site-nodedc", "sourceCommit": actual,
|
|
"sha256": hashlib.sha256(data).hexdigest(), "files": ENTRIES}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|