Files
libretro/scripts/scraper/targets/amberelec_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

132 lines
5.3 KiB
Python

"""Scraper for AmberELEC per-device emulator availability.
Sources (AmberELEC/AmberELEC, at the commit platforms/amberelec.yml names):
- Makefile -- one `DEVICE=<name> ARCH=<arch> ./scripts/build_distro` target
per image (RG351P, RG351V, RG351MP, RG552, RG353, RG503 at a21975bf)
- packages/amberelec/package.mk -- LIBRETRO_CORES_EXTRA joins PKG_EMUS
only on the devices its `if` names (lines 23-31 at a21975bf: RG552,
RG353, RG503), and on every other device makeinstall_target deletes
each of those cores from es_systems.cfg (lines 45-58)
- packages/ui/emulationstation/config/es_systems.cfg -- read the way the
platform scraper reads it, after that deletion
An RG351P image carries neither melonDS nor bsnes: without a target list,
its pack shipped the Nintendo DS firmware anyway.
"""
from __future__ import annotations
import re
import sys
from datetime import datetime, timezone
from . import BaseTargetScraper, target_cli
from ..amberelec_scraper import BUILDS_OF, ES_SYSTEMS_TEMPLATE, RUNNERS
from ..base_scraper import fetch_text
from ..batocera_scraper import _not_cores
from ..es_emulators import es_cores, xml_systems
PLATFORM_NAME = "amberelec"
RAW_BASE = "https://raw.githubusercontent.com/AmberELEC/AmberELEC/{ref}"
META_MK = "packages/amberelec/package.mk"
_DEVICE_RE = re.compile(
r"^(\w+):\n\tDEVICE=(\w+) ARCH=(\w+) \./scripts/build_distro$", re.MULTILINE
)
_EXTRA_RE = re.compile(r'LIBRETRO_CORES_EXTRA="([^"]*)"')
# The `if` whose body adds the extra cores to PKG_EMUS.
_EXTRA_IF_RE = re.compile(
r"if \[\[ (?P<test>[^\n]*?) \]\]; then\s*\n\s*PKG_EMUS\+=\" \$\{LIBRETRO_CORES_EXTRA\}\""
)
# What makeinstall_target does to es_systems.cfg on the other devices.
_REMOVAL_LINES = (
"for CORE in ${LIBRETRO_CORES_EXTRA}; do",
'sed -i "s|<core>$CORE</core>||g"',
r"sed -i -e '/<emulator name/{:a;N;/<\/emulator>/!ba;/melonds/d}'",
)
_EMULATOR_BLOCK_RE = re.compile(r"[^\n]*<emulator name.*?</emulator>[^\n]*\n?", re.DOTALL)
def devices(makefile: str) -> dict[str, str]:
"""{device: architecture} of each image the Makefile builds."""
found = {device: arch for target, device, arch in _DEVICE_RE.findall(makefile)
if target == device}
if not found:
raise RuntimeError("the AmberELEC Makefile builds no device image")
return found
def extra_cores(package_mk: str) -> tuple[list[str], set[str]]:
"""(the extra cores, the devices that keep them) from the metapackage."""
listed = _EXTRA_RE.search(package_mk)
test = _EXTRA_IF_RE.search(package_mk)
if listed is None or test is None:
raise RuntimeError(f"{META_MK}: no LIBRETRO_CORES_EXTRA or no test adding it")
missing = [line for line in _REMOVAL_LINES if line not in package_mk]
if missing:
raise RuntimeError(f"{META_MK}: makeinstall_target no longer reads {missing[0]!r}")
keep = set(re.findall(r'"\$\{DEVICE\}" == "(\w+)"', test.group("test")))
for alternatives in re.findall(r'"\$\{DEVICE\}" =~ \^\(([\w|]+)\)\$', test.group("test")):
keep.update(alternatives.split("|"))
if not keep:
raise RuntimeError(f"{META_MK}: the test adding the extra cores names no device")
return listed.group(1).split(), keep
def without_cores(es_systems: str, cores: list[str]) -> str:
"""es_systems.cfg as makeinstall_target leaves it on a device without them."""
for core in cores:
if core == "melonds":
es_systems = _EMULATOR_BLOCK_RE.sub(
lambda block: "" if "melonds" in block.group(0) else block.group(0),
es_systems,
)
es_systems = es_systems.replace(f"<core>{core}</core>", "")
return es_systems
def device_cores(es_systems: str, extra: list[str], keeps_extra: bool) -> list[str]:
"""Every core and standalone emulator one device's es_systems.cfg offers."""
text = es_systems if keeps_extra else without_cores(es_systems, extra)
cores, _standalone, _dual = es_cores(
xml_systems(text), RUNNERS, BUILDS_OF, _not_cores(PLATFORM_NAME)
)
return sorted(set(cores))
class Scraper(BaseTargetScraper):
"""Per-device core lists from AmberELEC's metapackage."""
def __init__(self, url: str = "https://github.com/AmberELEC/AmberELEC") -> None:
super().__init__(url=url)
self.ref = ""
def _raw(self, path: str) -> str:
return fetch_text(f"{RAW_BASE.format(ref=self.ref)}/{path}")
def fetch_targets(self) -> dict:
self.ref = self.pinned_revision(PLATFORM_NAME)
extra, keep = extra_cores(self._raw(META_MK))
es_systems = fetch_text(ES_SYSTEMS_TEMPLATE.format(ref=self.ref))
targets: dict[str, dict] = {}
for device, arch in sorted(devices(self._raw("Makefile")).items()):
cores = device_cores(es_systems, extra, device in keep)
targets[device.lower()] = {"architecture": arch, "cores": cores}
print(f" {device.lower()} ({arch}): {len(cores)} cores", file=sys.stderr)
return {
"platform": PLATFORM_NAME,
"source": f"{self.url}/blob/{self.ref}/{META_MK}",
"scraped_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"targets": targets,
}
def main() -> None:
target_cli(Scraper, "Scrape AmberELEC per-device cores")
if __name__ == "__main__":
main()