Files
OmniNX/scripts/generate-fpslocker-patches.py
T
niklascfw 47229a3fd0 ci: Remove FPSLocker-Warehouse submodule, fetch at build time
Gitea checkout still runs submodule update when .gitmodules exists,
even with submodules: false. Drop the submodule and download the v4
branch archive in CI instead.
2026-09-02 20:58:57 +02:00

220 lines
6.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""Erzeugt variants/oc/SaltySD/plugins/FPSLocker/patches.zip aus FPSLocker-Warehouse.
Das Warehouse liegt unter vendor/FPSLocker-Warehouse (gitignored) und wird beim
OC-Build per Git-Clone oder Archiv-Download geholt.
Archivwurzel ist patches/, passend zu boot_package.ini
(unzip nach SaltySD/plugins/FPSLocker/).
"""
from __future__ import annotations
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
import urllib.error
import urllib.request
import zipfile
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
WAREHOUSE = PROJECT_ROOT / "vendor" / "FPSLocker-Warehouse"
SRC_PARENT = WAREHOUSE / "SaltySD" / "plugins" / "FPSLocker"
SRC = SRC_PARENT / "patches"
DEST = (
PROJECT_ROOT
/ "variants"
/ "oc"
/ "SaltySD"
/ "plugins"
/ "FPSLocker"
/ "patches.zip"
)
WAREHOUSE_GIT_URL = "https://github.com/masagrator/FPSLocker-Warehouse.git"
WAREHOUSE_ZIP_URL = (
"https://github.com/masagrator/FPSLocker-Warehouse/archive/refs/heads/v4.zip"
)
def should_exclude(path: Path) -> bool:
if "__MACOSX" in path.parts:
return True
name = path.name
return name == ".DS_Store" or name.startswith("._")
def warehouse_head() -> str | None:
try:
proc = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
cwd=WAREHOUSE,
check=True,
capture_output=True,
text=True,
)
except (FileNotFoundError, subprocess.CalledProcessError, NotADirectoryError):
return None
return proc.stdout.strip() or None
def fetch_warehouse_archive() -> int:
"""Download v4 branch archive (used in CI when GitHub git clone fails)."""
print(f"Lade FPSLocker-Warehouse (v4) von {WAREHOUSE_ZIP_URL} ...")
try:
with tempfile.TemporaryDirectory() as tmp_name:
tmp = Path(tmp_name)
zip_path = tmp / "warehouse.zip"
with urllib.request.urlopen(WAREHOUSE_ZIP_URL, timeout=120) as response:
zip_path.write_bytes(response.read())
with zipfile.ZipFile(zip_path) as zf:
zf.extractall(tmp)
extracted_dirs = [
path
for path in tmp.iterdir()
if path.is_dir() and path.name.startswith("FPSLocker-Warehouse-")
]
if len(extracted_dirs) != 1:
print(
"Unerwartete Archivstruktur in FPSLocker-Warehouse v4.zip",
file=sys.stderr,
)
return 1
if WAREHOUSE.exists():
shutil.rmtree(WAREHOUSE)
shutil.move(str(extracted_dirs[0]), str(WAREHOUSE))
except (urllib.error.URLError, OSError, zipfile.BadZipFile) as exc:
print(f"Archiv-Download fehlgeschlagen: {exc}", file=sys.stderr)
return 1
if not SRC.is_dir():
print(f"patches/ fehlt nach Archiv-Extraktion: {SRC}", file=sys.stderr)
return 1
sha = warehouse_head()
if sha:
print(f"FPSLocker-Warehouse: {sha}")
else:
print("FPSLocker-Warehouse: v4 (Archiv)")
return 0
def clone_warehouse() -> int:
if WAREHOUSE.exists():
shutil.rmtree(WAREHOUSE)
subprocess.run(
[
"git",
"clone",
"--depth",
"1",
"--branch",
"v4",
WAREHOUSE_GIT_URL,
str(WAREHOUSE),
],
check=True,
)
if not SRC.is_dir():
print(f"patches/ fehlt nach Git-Clone: {SRC}", file=sys.stderr)
return 1
sha = warehouse_head()
if sha:
print(f"FPSLocker-Warehouse: {sha}")
return 0
def ensure_warehouse(*, remote: bool) -> int:
if SRC.is_dir() and not remote:
sha = warehouse_head()
if sha:
print(f"FPSLocker-Warehouse: {sha}")
return 0
if remote and WAREHOUSE.exists():
shutil.rmtree(WAREHOUSE)
label = "Hole neuesten Stand" if remote else "Initialisiere"
print(f"{label} von FPSLocker-Warehouse (v4)...")
if os.environ.get("CI"):
return fetch_warehouse_archive()
try:
return clone_warehouse()
except FileNotFoundError:
print("git nicht gefunden, versuche Archiv-Download...", file=sys.stderr)
return fetch_warehouse_archive()
except subprocess.CalledProcessError:
print("Git-Clone fehlgeschlagen, versuche Archiv-Download...", file=sys.stderr)
if WAREHOUSE.exists():
shutil.rmtree(WAREHOUSE, ignore_errors=True)
return fetch_warehouse_archive()
def update_warehouse() -> int:
return ensure_warehouse(remote=True)
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--update",
action="store_true",
help="Neuesten Stand des v4-Branches holen, bevor das Zip erzeugt wird",
)
return parser.parse_args(argv)
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
if args.update:
rc = update_warehouse()
if rc != 0:
return rc
elif not SRC.is_dir():
rc = ensure_warehouse(remote=False)
if rc != 0:
return rc
if not SRC.is_dir():
print(
"FPSLocker-Warehouse nicht initialisiert oder patches/ fehlt:",
file=sys.stderr,
)
print(f" {SRC}", file=sys.stderr)
print(
"Bitte ausfuehren: python3 scripts/generate-fpslocker-patches.py --update",
file=sys.stderr,
)
return 1
DEST.parent.mkdir(parents=True, exist_ok=True)
DEST.unlink(missing_ok=True)
with zipfile.ZipFile(DEST, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr("patches/", "")
for path in sorted(SRC.rglob("*")):
if should_exclude(path):
continue
rel = path.relative_to(SRC_PARENT).as_posix()
if path.is_dir():
zf.writestr(rel.rstrip("/") + "/", "")
elif path.is_file():
zf.write(path, arcname=rel)
sha = warehouse_head()
extra = f" ({sha})" if sha else ""
print(f"FPSLocker patches.zip erzeugt: {DEST}{extra}")
return 0
if __name__ == "__main__":
sys.exit(main())