File size: 2,547 Bytes
ae36cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Install a checksum-pinned official Godot 4 editor (includes headless mode)."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import platform
import stat
import urllib.request
import zipfile

VERSION = "4.5.1-stable"
ASSETS = {
    "Darwin": ("Godot_v4.5.1-stable_macos.universal.zip", "65c27959d02aaacfc131ec7ecb90179ba8045200cb02982bf2be96d117010b8a", "Godot.app/Contents/MacOS/Godot"),
    "Linux": ("Godot_v4.5.1-stable_linux.x86_64.zip", "02ec53d1cc7dbb9cc6355393c61b9ab43d1244751a124f10248a4802830788cd", "Godot_v4.5.1-stable_linux.x86_64"),
}

def install(destination: Path) -> Path:
    system = platform.system()
    if system not in ASSETS or (system == "Linux" and platform.machine() not in ("x86_64", "AMD64")):
        raise RuntimeError("Only macOS universal and Linux x86_64 are supported")
    filename, expected, relative = ASSETS[system]
    destination = destination.expanduser().resolve()
    destination.mkdir(parents=True, exist_ok=True)
    archive = destination / filename
    url = f"https://github.com/godotengine/godot-builds/releases/download/{VERSION}/{filename}"
    if not archive.exists():
        temporary = archive.with_suffix(".download")
        urllib.request.urlretrieve(url, temporary)
        temporary.replace(archive)
    actual = hashlib.sha256(archive.read_bytes()).hexdigest()
    if actual != expected:
        raise RuntimeError(f"Godot archive checksum mismatch: {archive}; remove and retry")
    binary = destination / relative
    if not binary.exists():
        with zipfile.ZipFile(archive) as zf:
            for member in zf.infolist():
                candidate = (destination / member.filename).resolve()
                if not candidate.is_relative_to(destination):
                    raise RuntimeError("Unsafe archive path")
                mode = member.external_attr >> 16
                if stat.S_ISLNK(mode):
                    raise RuntimeError("Unexpected archive symlink")
            zf.extractall(destination)
    binary.chmod(0o755)
    (destination / "installation.json").write_text(json.dumps({"version": VERSION, "url": url, "archive_sha256": actual, "binary_sha256": hashlib.sha256(binary.read_bytes()).hexdigest(), "binary": str(binary)}, indent=2) + "\n")
    return binary

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--destination", type=Path, default=Path(".tools/godot"))
    args = parser.parse_args()
    print(install(args.destination))