Files
NODEDC_PLATFORM/infra/gitea-ci/build_actions_artifact.py
T

66 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Build the data-only, fixed Gitea Actions activation transition."""
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[2]
SOURCE = Path(__file__).resolve().parent
ENTRY = "deployment/actions-enable-v1.json"
def descriptor_entry(patch_id):
match = re.fullmatch(r"gitea-actions-enable-(v[12])-[a-zA-Z0-9._-]{1,65}", patch_id)
if not match:
raise ValueError("invalid_actions_transition_identity")
return "deployment/actions-enable-" + match.group(1) + ".json"
def bundle(patch_id):
entry = descriptor_entry(patch_id)
descriptor = json.loads((SOURCE / Path(entry).name).read_text())
descriptor["releaseId"] = patch_id
files = {
"manifest.env": ("id=" + patch_id + "\ncomponent=gitea-actions-config\ntype=app-overlay\n").encode(),
"files.txt": (entry + "\n").encode(),
"payload/" + entry: (json.dumps(descriptor, indent=2) + "\n").encode(),
}
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, data in sorted(files.items()):
info = tarfile.TarInfo(name)
info.size, info.mode, info.mtime = len(data), 0o644, 0
archive.addfile(info, io.BytesIO(data))
return output.getvalue()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--patch-id", required=True)
parser.add_argument("--source-commit", required=True)
parser.add_argument("--output-dir", type=Path, required=True)
args = parser.parse_args()
actual = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip()
if actual != args.source_commit or subprocess.check_output(["git", "status", "--porcelain"], cwd=ROOT, text=True).strip():
raise SystemExit("clean_matching_commit_required")
data = bundle(args.patch_id)
args.output_dir.mkdir(parents=True, exist_ok=True)
path = args.output_dir / ("nodedc-" + args.patch_id + ".tgz")
with path.open("xb") as stream:
stream.write(data)
print(json.dumps({"artifact": str(path.resolve()), "patchId": args.patch_id,
"component": "gitea-actions-config", "sourceCommit": actual,
"sha256": hashlib.sha256(data).hexdigest(), "entries": [descriptor_entry(args.patch_id)],
"services": ["gitea"], "newPorts": [], "productionDeployEnabled": False}))
if __name__ == "__main__":
main()