95 lines
3.5 KiB
Python
95 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a dependency-free, content-addressed VPS release; no private state."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import shutil
|
|
import tarfile
|
|
import tempfile
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def build(output):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
release = Path(directory) / "ndc-mission-vps"
|
|
shutil.copytree(
|
|
ROOT / "apps/vps-host", release, ignore=shutil.ignore_patterns("__pycache__", "*.pyc")
|
|
)
|
|
shutil.copyfile(
|
|
ROOT / "src/k1link/device_plugins/arnavi_b2/internal.py", release / "ndc_vps/arnavi.py"
|
|
)
|
|
for name in ("usb_protocol.py", "web_protocol.py", "sim_network.py"):
|
|
shutil.copyfile(
|
|
ROOT / "src/k1link/device_plugins/arnavi_b2" / name, release / "ndc_vps" / name
|
|
)
|
|
files = {
|
|
str(p.relative_to(release)): hashlib.sha256(p.read_bytes()).hexdigest()
|
|
for p in sorted(release.rglob("*"))
|
|
if p.is_file()
|
|
}
|
|
(release / "manifest.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"schema": "missioncore.vps-release/v1",
|
|
"environment": "ubuntu-24.04-amd64",
|
|
"files": files,
|
|
},
|
|
sort_keys=True,
|
|
indent=2,
|
|
)
|
|
+ "\n"
|
|
)
|
|
output = Path(output)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
with tarfile.open(output, "w") as archive:
|
|
for file in sorted(release.rglob("*")):
|
|
info = archive.gettarinfo(
|
|
file, arcname="ndc-mission-vps/" + str(file.relative_to(release))
|
|
)
|
|
info.uid = info.gid = info.mtime = 0
|
|
info.uname = info.gname = ""
|
|
info.mode = 0o755 if file.is_dir() else 0o644
|
|
if file.is_file():
|
|
with file.open("rb") as stream:
|
|
archive.addfile(info, stream)
|
|
else:
|
|
archive.addfile(info)
|
|
installer = output.with_suffix(".pyz")
|
|
with zipfile.ZipFile(installer, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
|
content = {"__main__.py": (release / "owner_entry.py").read_bytes()}
|
|
content.update(
|
|
{
|
|
"payload/" + str(p.relative_to(release)): p.read_bytes()
|
|
for p in sorted(release.rglob("*"))
|
|
if p.is_file()
|
|
}
|
|
)
|
|
for name, value in sorted(content.items()):
|
|
info = zipfile.ZipInfo(name, date_time=(2026, 1, 1, 0, 0, 0))
|
|
info.compress_type = zipfile.ZIP_DEFLATED
|
|
info.external_attr = 0o100644 << 16
|
|
archive.writestr(info, value)
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"artifact": str(output),
|
|
"installer": str(installer),
|
|
"installer_sha256": hashlib.sha256(installer.read_bytes()).hexdigest(),
|
|
"sha256": hashlib.sha256(output.read_bytes()).hexdigest(),
|
|
"manifest_sha256": hashlib.sha256(
|
|
(release / "manifest.json").read_bytes()
|
|
).hexdigest(),
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--output", required=True)
|
|
build(parser.parse_args().output)
|