|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tarfile |
| 10 | +import urllib.request |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def run(command: list[str], *, input_text: str | None = None) -> None: |
| 15 | + print("+", " ".join(command), flush=True) |
| 16 | + subprocess.run(command, check=True, text=True, input=input_text) |
| 17 | + |
| 18 | + |
| 19 | +def capture(command: list[str]) -> str: |
| 20 | + return subprocess.check_output(command, text=True).strip() |
| 21 | + |
| 22 | + |
| 23 | +def require_env(name: str) -> str: |
| 24 | + value = os.environ.get(name) |
| 25 | + if not value: |
| 26 | + raise SystemExit(f"Missing required environment variable: {name}") |
| 27 | + return value |
| 28 | + |
| 29 | + |
| 30 | +def write_env(name: str, value: str) -> None: |
| 31 | + github_env = os.environ.get("GITHUB_ENV") |
| 32 | + if github_env: |
| 33 | + with open(github_env, "a", encoding="utf-8") as handle: |
| 34 | + handle.write(f"{name}={value}\n") |
| 35 | + os.environ[name] = value |
| 36 | + |
| 37 | + |
| 38 | +def append_path(value: str) -> None: |
| 39 | + github_path = os.environ.get("GITHUB_PATH") |
| 40 | + if github_path: |
| 41 | + with open(github_path, "a", encoding="utf-8") as handle: |
| 42 | + handle.write(f"{value}\n") |
| 43 | + |
| 44 | + |
| 45 | +def prepend_env_path(name: str, value: str) -> None: |
| 46 | + current = os.environ.get(name, "") |
| 47 | + write_env(name, f"{value}:{current}" if current else value) |
| 48 | + |
| 49 | + |
| 50 | +def download(url: str, destination: Path) -> None: |
| 51 | + destination.parent.mkdir(parents=True, exist_ok=True) |
| 52 | + with urllib.request.urlopen(url) as response, open(destination, "wb") as handle: |
| 53 | + shutil.copyfileobj(response, handle) |
| 54 | + |
| 55 | + |
| 56 | +def extract_tarball(archive: Path, destination: Path) -> None: |
| 57 | + destination.mkdir(parents=True, exist_ok=True) |
| 58 | + with tarfile.open(archive, "r:gz") as tar_handle: |
| 59 | + tar_handle.extractall(destination) |
| 60 | + |
| 61 | + |
| 62 | +def cygpath(mode: str, path: str | Path) -> str: |
| 63 | + return capture(["cygpath", f"-{mode}", str(path)]) |
| 64 | + |
| 65 | + |
| 66 | +def find_first_existing(paths: list[Path]) -> Path: |
| 67 | + for path in paths: |
| 68 | + if path.exists(): |
| 69 | + return path |
| 70 | + raise SystemExit(f"Unable to locate any of: {', '.join(str(path) for path in paths)}") |
| 71 | + |
| 72 | + |
| 73 | +def find_file(root_candidates: list[Path], filename: str) -> Path | None: |
| 74 | + for root in root_candidates: |
| 75 | + if not root.exists(): |
| 76 | + continue |
| 77 | + for current_root, _, files in os.walk(root): |
| 78 | + if filename in files: |
| 79 | + return Path(current_root) / filename |
| 80 | + return None |
| 81 | + |
| 82 | + |
| 83 | +def repo_root() -> Path: |
| 84 | + return Path(__file__).resolve().parents[2] |
| 85 | + |
| 86 | + |
| 87 | +def main_error(message: str) -> None: |
| 88 | + print(message, file=sys.stderr) |
| 89 | + raise SystemExit(1) |
0 commit comments