Files
libretro/scripts/scraper/targets/rocknix_targets_scraper.py
T
Abdessamad Derraz f61c2cb7d4 refactor: one network layer for the scrapers
Each network job had several copies. One is kept per job and
every caller uses it.

- Source revision: model.sourcerevision (source_revision, required_revision,
  platform_revision) replaces BaseTargetScraper's pattern list, the
  exporter's _REVISION, the four launcher_revision copies and RetroBat's
  own YAML reader. It reads the union of the shapes: GitHub raw, blob and
  tree with or without a path after the revision, GitLab raw, blob, tree
  and archive, codeload tar.gz and zip. The target scrapers now answer
  for RetroDECK, NextUI, MinUI and MyMinUI, whose tree URLs end at the
  commit, and the exporter reads GitLab blob and tree and codeload zip.
  EmuDeck and RetroBat still require a 40-hex commit, and each launcher
  reader requires its own repository in the URL.
- Latest release: fetch_github_latest_version, type-checked, is the only
  lookup. mame_hash_scraper, romset_dat_importer and the Lakka target
  scraper call it. romset_dat_importer's NoReleaseError is gone: a
  release without a tag raises RuntimeError.
- Tree listing: github_tree and tree_entries refuse a truncated or
  unreadable listing. The libretro and core-info scrapers share
  core_info_files and fetch_info_file, with libretro's refusal of an empty
  listing. The core-info listing and the RetroDECK, ROCKNIX launcher and
  libretro listings were not checked for truncation before.
- GET: fetch_bytes is the one request path. fetch_text, fetch_present
  (None on 404 only), github_json and download are built on it, so every
  read retries 429 and 5xx, caps the body and raises FetchError, a
  ConnectionError carrying the HTTP status. The target scrapers' own
  fetch_text raised RuntimeError: they now raise FetchError, and an
  optional Batocera board include is skipped on 404 only, not on any
  failure. The exporter's originals, MiSTer, RetroPie, redump and the
  buildbot index go through it too.
- Headers: core.upstream.forge_headers is the one builder. The token goes
  to api.github.com and raw.githubusercontent.com, the host compared
  whole, as Bearer. A URL that only names api.github.com in its query
  got the scrapers' token before, and profile_sync sent its token to
  github.com, whose release downloads redirect to asset storage with the
  header. Scraper reads of raw files now carry the token.
- Submodule commit: submodule_commit checks that the path is a
  submodule. Batocera's target scraper accepted any contents answer with
  a sha, a directory's tree sha included.
- Download: download streams through a scratch file renamed into place,
  retries like fetch_bytes, can keep an existing file and returns the
  Content-Disposition. It replaces the _download of romset_dat_importer
  and game_dat_importer.
2026-11-20 20:02:15 +01:00

293 lines
12 KiB
Python

"""Scraper for ROCKNIX per-device emulator availability.
Sources (ROCKNIX/distribution, at the release tag platforms/rocknix.yml names):
- projects/ROCKNIX/packages/virtual/emulators/package.mk -- the cores every
image carries, those aarch64 adds, and a `case "${DEVICE}"` block per SoC
- projects/ROCKNIX/devices/<DEVICE>/options -- ENABLE_32BIT, which the 32-bit
cores of a device depend on (distributions/ROCKNIX/options defaults it on)
- each libretro package's package.mk -- the cores its makeinstall_target
copies to ${INSTALL}/usr/lib/libretro (vice-lr installs ten of them)
An H700 or RK3326 handheld carries neither Dolphin nor AetherSX2: without a
target list, its pack shipped the PS2, GameCube and PS3 firmware anyway.
"""
from __future__ import annotations
import re
import sys
from datetime import datetime, timezone
from . import BaseTargetScraper, target_cli
from ..base_scraper import fetch_text, github_json, github_tree
from ..rocknix_packages import standalone_emulators
PLATFORM_NAME = "rocknix"
RAW_BASE = "https://raw.githubusercontent.com/ROCKNIX/distribution/{tag}"
REPO = "ROCKNIX/distribution"
GITHUB_API = f"https://api.github.com/repos/{REPO}/contents"
EMULATORS_MK = "projects/ROCKNIX/packages/virtual/emulators/package.mk"
DEVICES_DIR = "projects/ROCKNIX/devices"
# `NAME="a b \
# c"` or `NAME+=" a b"`, continuation lines joined first.
_ASSIGN_RE = re.compile(r'^\s*(?:\[[^\]]*\]\s*&&\s*)?(\w+)(\+?=)"([^"]*)"', re.MULTILINE)
_CASE_RE = re.compile(r'case "\$\{DEVICE\}" in(.*?)\besac\b', re.DOTALL)
_ARM_RE = re.compile(r"^\s*([A-Z0-9|]+)\)\s*$", re.MULTILINE)
_AARCH64_RE = re.compile(
r'if \[ "\$\{ARCH\}" = "aarch64" \]; then(.*?)\bfi\b', re.DOTALL
)
def _assignments(text: str) -> list[tuple[str, str, list[str]]]:
"""(variable, operator, words) of each quoted assignment, in order."""
joined = re.sub(r"\\\n", " ", text)
return [(name, op, value.split()) for name, op, value in _ASSIGN_RE.findall(joined)]
def _apply(state: dict[str, list[str]], text: str, with_32bit: bool) -> None:
for name, op, words in _assignments(text):
if name == "EMUS_32BIT" and not with_32bit:
continue
if op == "=":
state[name] = list(words)
else:
state.setdefault(name, []).extend(words)
def parse_device_cores(
package_mk: str, device: str, arch: str, with_32bit: bool
) -> tuple[list[str], list[str]]:
"""(libretro packages, standalone packages) one device's image carries."""
case = _CASE_RE.search(package_mk)
head = package_mk[: case.start()] if case else package_mk
aarch64 = _AARCH64_RE.search(head)
base = head if aarch64 is None else head[: aarch64.start()] + head[aarch64.end():]
state: dict[str, list[str]] = {}
_apply(state, base, with_32bit)
if arch == "aarch64" and aarch64 is not None:
_apply(state, aarch64.group(1), with_32bit)
if case is not None:
arms = list(_ARM_RE.finditer(case.group(1)))
for index, arm in enumerate(arms):
if device not in arm.group(1).split("|"):
continue
end = arms[index + 1].start() if index + 1 < len(arms) else len(case.group(1))
_apply(state, case.group(1)[arm.end():end], with_32bit)
libretro = state.get("LIBRETRO_CORES", []) + [
p for p in state.get("EMUS_32BIT", []) if re.search(r"[-_]lr$", p)
]
standalone = state.get("PKG_EMUS", []) + [
p for p in state.get("EMUS_32BIT", []) if not re.search(r"[-_]lr$", p)
]
return libretro, standalone
_INSTALL_BODY_RE = re.compile(r"^makeinstall_target\(\)\s*\{\n(.*?)^\}", re.M | re.S)
_COPY_RE = re.compile(r"\bcp\s+(.*?)\s+\$\{?INSTALL\}?/usr/lib/libretro(?:/(\S*))?")
_CORE_FILE_RE = re.compile(r"([\w+.-]+)_libretro\.(?:so\b|\{[^}]*\bso\b[^}]*\})")
_TEST_RE = re.compile(r'\[\s*"\$\{(\w+)\}"\s*=\s*"([^"]*)"\s*\]')
def _holds(condition: str, values: dict[str, str]) -> bool:
tests = _TEST_RE.findall(condition)
if not tests or re.sub(_TEST_RE, "", condition).strip(" ;&") not in ("", "then"):
raise RuntimeError(f"makeinstall_target tests {condition.strip()!r}")
for variable, value in tests:
if variable not in values:
raise RuntimeError(f"makeinstall_target tests ${{{variable}}}")
if values[variable] != value:
return False
return True
def _copied_cores(line: str) -> list[str]:
"""The core files one line of makeinstall_target copies, by installed name."""
found: list[str] = []
for sources, target in _COPY_RE.findall(line):
if named := _CORE_FILE_RE.search(target):
found.append(named.group(1))
continue
found.extend(
named.group(1) for source in sources.split()
if not source.startswith("-")
and (named := _CORE_FILE_RE.search(source.rsplit("/", 1)[-1]))
)
return found
def installed_cores(package_mk: str, arch: str, with_32bit: bool) -> list[str]:
"""The cores a libretro package installs on a device.
The name is that of the file makeinstall_target copies to
${INSTALL}/usr/lib/libretro ($INSTALL in some), renamed when the copy names its target:
beetle-psx-lr copies mednafen_psx_libretro.so as beetle_psx_libretro.so,
vice-lr ten EMUTYPE builds. pcsx_rearmed-lr picks its file by
TARGET_ARCH and adds the 32-bit build when ENABLE_32BIT is on, desmume-lr
copies the 32-bit build on aarch64 (ARCH). A test on another variable,
or a package that installs no core, is an error.
"""
body = _INSTALL_BODY_RE.search(package_mk)
if body is None:
raise RuntimeError("package.mk has no makeinstall_target")
# config/options:36 sets TARGET_ARCH="${ARCH}".
values = {"ARCH": arch, "TARGET_ARCH": arch, "ENABLE_32BIT": "true" if with_32bit else "false"}
found: list[str] = []
stack: list[bool] = []
in_case = False
for raw in body.group(1).splitlines():
line = raw.strip()
if re.match(r"case \$\{TARGET_ARCH\} in$", line):
in_case = True
stack.append(False)
continue
if line == "esac":
in_case = False
stack.pop()
continue
if in_case and (arm := re.match(r"([\w|*]+)\)\s*(.*)$", line)):
stack[-1] = arch in arm.group(1).split("|") or arm.group(1) == "*"
line = arm.group(2)
if line.startswith("if "):
stack.append(_holds(line[3:], values))
continue
if line == "else":
stack[-1] = not stack[-1]
continue
if line == "fi":
stack.pop()
continue
if not all(stack):
continue
found.extend(_copied_cores(line))
if not found:
raise RuntimeError("makeinstall_target installs no libretro core")
return sorted(set(found))
def package_paths(tree: list[str]) -> dict[str, str]:
"""package name -> its package.mk, a ROCKNIX project package first.
The build takes projects/ROCKNIX/packages/<...>/<name> over
packages/<...>/<name> when both exist.
"""
found: dict[str, str] = {}
for path in sorted(tree, key=lambda p: not p.startswith("projects/ROCKNIX/")):
if path.endswith("/package.mk"):
found.setdefault(path.rsplit("/", 2)[-2], path)
return found
# A package that builds nothing itself and pulls other cores in. idtech-lr
# installs ecwolf, prboom, tyrquake and vitaquake2 (vitaquake3 and boom3 on
# x86_64). No profile could name it.
METAPACKAGES = {"idtech-lr": "projects/ROCKNIX/packages/emulators/libretro/idtech-lr/package.mk"}
_IF_RE = re.compile(r'^if \[ "\$\{(\w+)\}" = "?([\w-]+)"? \]; then\n(.*?)^fi$',
re.MULTILINE | re.DOTALL)
def metapackage_cores(package_mk: str, arch: str) -> list[str]:
"""The -lr packages a metapackage depends on for one architecture.
OPENGLES_SUPPORT is yes on every ROCKNIX device (config/graphic, from
OPENGLES="mesa" in projects/ROCKNIX/options), and TARGET_ARCH is the device's.
"""
found: list[str] = []
for variable, value, body in _IF_RE.findall(package_mk):
if variable == "TARGET_ARCH" and value != arch:
continue
if variable not in ("TARGET_ARCH", "OPENGLES_SUPPORT"):
continue
for _name, _op, words in _assignments(body):
found += [w for w in words if re.search(r"[-_]lr$", w)]
return found
class Scraper(BaseTargetScraper):
"""Per-device core lists from ROCKNIX's emulators metapackage."""
def __init__(self, url: str = "https://github.com/ROCKNIX/distribution") -> None:
super().__init__(url=url)
self.tag = ""
def _raw(self, path: str) -> str:
return fetch_text(f"{RAW_BASE.format(tag=self.tag)}/{path}")
def _devices(self) -> list[str]:
url = f"{GITHUB_API}/{DEVICES_DIR}?ref={self.tag}"
listing = github_json(url, f"cannot list {DEVICES_DIR} at {self.tag}")
devices = [item["name"] for item in listing
if isinstance(item, dict) and item.get("type") == "dir"]
if not devices:
raise RuntimeError(f"no device in {DEVICES_DIR} at {self.tag}")
return sorted(devices)
def _tree(self) -> list[str]:
return [entry["path"] for entry in github_tree(REPO, self.tag)]
def device_builds(self, tag: str | None = None) -> dict[str, tuple[str, list[str], list[str]]]:
"""{device: (architecture, libretro cores, standalone packages)}.
Read at `tag`, by default the revision platforms/rocknix.yml was
scraped at: metapackages resolved to the packages they pull in, and
each libretro package to the cores it installs on the device.
"""
self.tag = tag or self.pinned_revision(PLATFORM_NAME)
package_mk = self._raw(EMULATORS_MK)
if "LIBRETRO_CORES" not in package_mk:
raise RuntimeError(f"no LIBRETRO_CORES in {EMULATORS_MK} at {self.tag}")
paths = package_paths(self._tree())
texts: dict[str, str] = {}
def cores_of(package: str, arch: str, with_32bit: bool) -> list[str]:
if package not in paths:
raise RuntimeError(f"no package.mk for {package} at {self.tag}")
if package not in texts:
texts[package] = self._raw(paths[package])
try:
return installed_cores(texts[package], arch, with_32bit)
except RuntimeError as e:
raise RuntimeError(f"{paths[package]}: {e}") from e
builds: dict[str, tuple[str, list[str], list[str]]] = {}
for device in self._devices():
options = self._raw(f"{DEVICES_DIR}/{device}/options")
arch = "x86_64" if device == "AMD64" else "aarch64"
with_32bit = not re.search(r'ENABLE_32BIT="false"', options)
libretro, standalone = parse_device_cores(package_mk, device, arch, with_32bit)
for meta, path in METAPACKAGES.items():
if meta in libretro:
libretro.remove(meta)
libretro += metapackage_cores(self._raw(path), arch)
cores = sorted({c for package in libretro for c in cores_of(package, arch, with_32bit)})
builds[device.lower()] = (arch, cores, standalone)
return builds
def fetch_targets(self) -> dict:
targets: dict[str, dict] = {}
for device, (arch, libretro, standalone) in self.device_builds().items():
# A standalone package is kept under both spellings, as the
# platform scraper keeps it: vice-sa is the only name
# vice-standalone answers to, vice that of the libretro profile.
cores = sorted({*libretro, *standalone_emulators(standalone)})
targets[device] = {"architecture": arch, "cores": cores}
print(f" {device} ({arch}): {len(cores)} cores", file=sys.stderr)
return {
"platform": PLATFORM_NAME,
"source": f"{self.url}/tree/{self.tag}/{EMULATORS_MK}",
"scraped_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"targets": targets,
}
def main() -> None:
target_cli(Scraper, "Scrape ROCKNIX per-device cores")
if __name__ == "__main__":
main()