#!/usr/bin/env python3 import argparse import importlib.util import os from pathlib import Path import py_compile import shutil import tempfile DESTINATION = ( Path(__file__).resolve().parents[1] / "src" / "sailfish_devel_mcp" / "vendor" / "build_sailfishos.py" ) def helper_version(path: Path) -> str: spec = importlib.util.spec_from_file_location("candidate_build_sailfishos", path) if spec is None or spec.loader is None: raise RuntimeError(f"could not load helper: {path}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) version = getattr(module, "HELPER_VERSION", None) if not isinstance(version, str) or not version: raise RuntimeError(f"helper has no HELPER_VERSION: {path}") return version def main() -> int: parser = argparse.ArgumentParser(description="Update the vendored build-sailfishos helper") parser.add_argument("source", type=Path, help="Canonical build_sailfishos.py") args = parser.parse_args() source = args.source.expanduser().resolve() if not source.is_file(): parser.error(f"source helper not found: {source}") version = helper_version(source) py_compile.compile(str(source), doraise=True) DESTINATION.parent.mkdir(parents=True, exist_ok=True) descriptor, temporary_name = tempfile.mkstemp( prefix=f".{DESTINATION.name}.", suffix=".tmp", dir=DESTINATION.parent, ) os.close(descriptor) temporary = Path(temporary_name) try: shutil.copyfile(source, temporary) temporary.chmod(0o755 if source.stat().st_mode & 0o111 else 0o644) os.replace(temporary, DESTINATION) finally: try: temporary.unlink() except FileNotFoundError: pass print(f"vendored build helper {version}: {DESTINATION}") return 0 if __name__ == "__main__": raise SystemExit(main())