|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +PRUNED_ENGINE_DIRECTORIES = ("AI",) |
| 8 | +PRUNED_ENGINE_EXECUTABLES = ("pr-downloader", "spring-dedicated", "spring-headless") |
| 9 | + |
| 10 | + |
| 11 | +def install_engine(*, destination: Path, engine_dir: Path | None, engine_archive: Path | None) -> None: |
| 12 | + if (engine_dir is None) == (engine_archive is None): |
| 13 | + raise RuntimeError("Specify exactly one of --engine-dir or --engine-archive") |
| 14 | + |
| 15 | + if engine_dir is not None: |
| 16 | + shutil.copytree(engine_dir.resolve(), destination, symlinks=True) |
| 17 | + return |
| 18 | + |
| 19 | + if engine_archive is None: |
| 20 | + raise RuntimeError("Engine archive was not provided") |
| 21 | + seven_zip = shutil.which("7z") |
| 22 | + if seven_zip is None: |
| 23 | + raise RuntimeError("7z is required to extract the engine archive") |
| 24 | + destination.mkdir(parents=True) |
| 25 | + subprocess.run( |
| 26 | + [seven_zip, "x", "-y", f"-o{destination}", str(engine_archive.resolve())], |
| 27 | + check=True, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def prune_engine(application_dir: Path, platform: str) -> None: |
| 32 | + executable_suffix = "" if platform == "linux" else ".exe" |
| 33 | + for executable in PRUNED_ENGINE_EXECUTABLES: |
| 34 | + (application_dir / f"{executable}{executable_suffix}").unlink(missing_ok=True) |
| 35 | + |
| 36 | + for directory in PRUNED_ENGINE_DIRECTORIES: |
| 37 | + path = application_dir / directory |
| 38 | + if path.is_symlink() or path.is_file(): |
| 39 | + path.unlink() |
| 40 | + elif path.is_dir(): |
| 41 | + shutil.rmtree(path) |
| 42 | + |
| 43 | + |
| 44 | +def rename_engine(application_dir: Path, platform: str) -> Path: |
| 45 | + source_name = "spring" if platform == "linux" else "spring.exe" |
| 46 | + destination_name = "SpringBoard" if platform == "linux" else "SpringBoard.exe" |
| 47 | + source = application_dir / source_name |
| 48 | + destination = application_dir / destination_name |
| 49 | + if not source.is_file(): |
| 50 | + raise RuntimeError(f"Engine executable is missing: {source}") |
| 51 | + if destination.exists(): |
| 52 | + raise RuntimeError(f"Application executable already exists: {destination}") |
| 53 | + source.rename(destination) |
| 54 | + if platform == "linux" and not destination.stat().st_mode & 0o111: |
| 55 | + raise RuntimeError(f"Engine executable lost its executable mode: {destination}") |
| 56 | + return destination |
0 commit comments