summaryrefslogtreecommitdiff
path: root/scripts/update_build_helper.py
blob: c9436a18ae2d922aa237bcbc5e0bc965a53bdf79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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())