import argparse import hashlib import json import os import subprocess import sys import tarfile import tempfile import time import urllib.parse import urllib.request from pathlib import Path SERVER = "https://git.annodev.software" REPOSITORY = "annodev/anno-base" MARKER = ".annobase-version" def setting(path, key): value = json.loads(Path(path).read_text(encoding="utf-8-sig")) for part in key.split("."): value = value[part] text = str(value).strip() if not text: raise ValueError(f"{path} has an empty {key}") return text def open_url(url, token): if urllib.parse.urlparse(url).netloc != urllib.parse.urlparse(SERVER).netloc: raise ValueError(f"{url} is not on {SERVER}") request = urllib.request.Request(url, headers={"Authorization": "token " + token}) return urllib.request.urlopen(request, timeout=900) def release_assets(version, token): with open_url(f"{SERVER}/api/v1/repos/{REPOSITORY}/releases/tags/v{version}", token) as response: release = json.load(response) if release.get("draft"): raise ValueError(f"AnnoBase v{version} is a draft release") return {asset["name"]: asset["browser_download_url"] for asset in release["assets"]} def download(url, token, target): digest = hashlib.sha256() with open_url(url, token) as response, target.open("wb") as stream: while chunk := response.read(1024 * 1024): stream.write(chunk) digest.update(chunk) return digest.hexdigest() def extract(archive, destination): with tarfile.open(archive) as tar: if hasattr(tarfile, "data_filter"): tar.extractall(destination, filter="data") else: tar.extractall(destination) def build_frontend(destination, packages): folder = destination / "source" / "frontend" subprocess.run(["bun", "install", "--frozen-lockfile"], cwd=folder, check=True) for package in packages: subprocess.run(["bun", "run", "--cwd", f"packages/{package}", "build"], cwd=folder, check=True) def fetch(version, destination, token): marker = destination / MARKER if marker.is_file() and marker.read_text().strip() == version: print(f"AnnoBase {version} is already in {destination}") return if destination.exists(): if not marker.is_file(): sys.exit(f"{destination} exists and was not made by this step") destination.rename(destination.with_name(f"{destination.name}.replaced.{int(time.time())}")) assets = release_assets(version, token) with tempfile.TemporaryDirectory(prefix="annobase-") as temp: temp = Path(temp) manifest_path = temp / "manifest.json" download(assets["manifest.json"], token, manifest_path) manifest = json.loads(manifest_path.read_text()) if manifest["version"] != version: sys.exit(f"The manifest of v{version} names version {manifest['version']}") archive = temp / manifest["file"] if download(assets[manifest["file"]], token, archive) != manifest["sha256"]: sys.exit(f"{manifest['file']} does not match the sha256 of its manifest") destination.mkdir(parents=True) extract(archive, destination) marker.write_text(version + "\n") print(f"AnnoBase {version} ({manifest['commit']}) is in {destination}") def main(): workspace = Path(os.environ.get("GITHUB_WORKSPACE", ".")) parser = argparse.ArgumentParser() parser.add_argument("--settings", required=True) parser.add_argument("--key", default="AnnoBase.Version") parser.add_argument("--destination", default=str(workspace / "AnnoBase")) parser.add_argument("--frontend-packages", default="") args = parser.parse_args() token = os.environ.get("ANNOBASE_TOKEN", "").strip() if not token: sys.exit("ANNOBASE_TOKEN is empty") settings = Path(args.settings) if not settings.is_absolute(): settings = workspace / settings destination = Path(args.destination) if not destination.is_absolute(): destination = workspace / destination fetch(setting(settings, args.key), destination, token) packages = args.frontend_packages.replace(",", " ").split() if packages: build_frontend(destination, packages) if __name__ == "__main__": main()