|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def sha256sum(path: Path) -> str: |
| 12 | + digest = hashlib.sha256() |
| 13 | + with path.open("rb") as handle: |
| 14 | + for chunk in iter(lambda: handle.read(1024 * 1024), b""): |
| 15 | + digest.update(chunk) |
| 16 | + return digest.hexdigest() |
| 17 | + |
| 18 | + |
| 19 | +def parse_platform(name: str) -> str: |
| 20 | + lowered = name.lower() |
| 21 | + if "windows" in lowered: |
| 22 | + return "windows" |
| 23 | + if "linux" in lowered: |
| 24 | + return "linux" |
| 25 | + if "macos" in lowered: |
| 26 | + return "macosx" |
| 27 | + raise ValueError(f"Unable to determine platform from filename: {name}") |
| 28 | + |
| 29 | + |
| 30 | +def parse_build_type(name: str) -> str: |
| 31 | + lowered = name.lower() |
| 32 | + if "debug" in lowered: |
| 33 | + return "Debug" |
| 34 | + return "RelWithDebInfo" |
| 35 | + |
| 36 | + |
| 37 | +def build_metadata_entry(path: Path, version: str) -> dict: |
| 38 | + filename = path.name |
| 39 | + return { |
| 40 | + "platform": parse_platform(filename), |
| 41 | + "build_type": parse_build_type(filename), |
| 42 | + "is_lto": "-lto" in filename.lower(), |
| 43 | + "filename": filename, |
| 44 | + "version": version, |
| 45 | + "sha256": sha256sum(path), |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +def main() -> None: |
| 50 | + if len(sys.argv) != 4: |
| 51 | + print( |
| 52 | + "Usage: upload-llvm.py <tag> <target_repo> <workflow_id>", file=sys.stderr |
| 53 | + ) |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + tag, target_repo, workflow_id = sys.argv[1:] |
| 57 | + artifacts_dir = Path("artifacts") |
| 58 | + |
| 59 | + if not artifacts_dir.is_dir(): |
| 60 | + print(f"Artifacts directory not found: {artifacts_dir}", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + artifact_files = sorted( |
| 64 | + p |
| 65 | + for p in artifacts_dir.rglob("*") |
| 66 | + if p.is_file() and p.suffix.lower() != ".json" |
| 67 | + ) |
| 68 | + |
| 69 | + if not artifact_files: |
| 70 | + print("No artifacts found to upload.", file=sys.stderr) |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + version_without_prefix = tag.lstrip("vV") |
| 74 | + metadata = [ |
| 75 | + build_metadata_entry(path, version_without_prefix) for path in artifact_files |
| 76 | + ] |
| 77 | + |
| 78 | + json_path = artifacts_dir / "prebuilt-llvm.json" |
| 79 | + with json_path.open("w", encoding="utf-8") as handle: |
| 80 | + json.dump(metadata, handle, indent=2) |
| 81 | + handle.write("\n") |
| 82 | + |
| 83 | + assets = [str(path) for path in artifact_files] |
| 84 | + assets.append(str(json_path)) |
| 85 | + |
| 86 | + env = os.environ.copy() |
| 87 | + |
| 88 | + print(f"Checking for existing release {tag} in {target_repo}...") |
| 89 | + view_result = subprocess.run( |
| 90 | + ["gh", "release", "view", tag, "--repo", target_repo], |
| 91 | + env=env, |
| 92 | + stdout=subprocess.DEVNULL, |
| 93 | + stderr=subprocess.DEVNULL, |
| 94 | + ) |
| 95 | + |
| 96 | + if view_result.returncode == 0: |
| 97 | + print(f"Deleting existing release {tag} in {target_repo}...") |
| 98 | + subprocess.run( |
| 99 | + ["gh", "release", "delete", tag, "--repo", target_repo, "-y"], |
| 100 | + env=env, |
| 101 | + check=True, |
| 102 | + ) |
| 103 | + |
| 104 | + print(f"Creating release {tag} in {target_repo} with {len(assets)} assets...") |
| 105 | + # fmt: off |
| 106 | + args = [ |
| 107 | + "gh", "release", "create", tag, *assets, |
| 108 | + "--repo", target_repo, |
| 109 | + "--title", tag, |
| 110 | + "--notes", f"Artifacts from workflow run {workflow_id}", |
| 111 | + "--latest", |
| 112 | + ] |
| 113 | + # fmt: on |
| 114 | + subprocess.run(args, env=env, check=True) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments