Files
libretro/scripts/model/sourcerevision.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

69 lines
2.6 KiB
Python

"""The upstream revision a platform YAML's `source:` URL names.
A scraper writes the URL of the file it read at the tag or commit it read
it at. The target scrapers, the launcher extractors and the exporter all
read that revision back, so the URL shapes are recognised in one place.
"""
from __future__ import annotations
import re
from pathlib import Path
from model.safeparse import yaml_load
# Each pattern captures the revision. A trailing file path is optional: a
# tree URL names the whole repository at a commit (RetroDECK, MinUI).
_REVISION_PATTERNS = (
re.compile(r"^https://raw\.githubusercontent\.com/[^/]+/[^/]+/([^/]+)(?:/|$)"),
re.compile(r"^https://github\.com/[^/]+/[^/]+/(?:blob|tree)/([^/]+)(?:/|$)"),
re.compile(r"^https://gitlab\.com/.+?/-/(?:raw|blob|tree|archive)/([^/]+)(?:/|$)"),
re.compile(r"^https://codeload\.github\.com/[^/]+/[^/]+/(?:tar\.gz|zip)/([^/]+)$"),
)
_COMMIT = re.compile(r"[0-9a-f]{40}")
def source_revision(url: str) -> str:
"""The tag or commit a forge URL is read at, '' when it names none."""
for pattern in _REVISION_PATTERNS:
match = pattern.match(url)
if match:
return match.group(1)
return ""
def required_revision(url: str, repo: str = "", commit: bool = False) -> str:
"""The revision `url` names, ValueError when it names none.
`repo` (owner/name) refuses a URL of another repository, `commit`
refuses a tag or a branch where only a commit pins the read.
"""
revision = source_revision(url)
if repo and f"/{repo}/" not in url:
revision = ""
if commit and not _COMMIT.fullmatch(revision):
revision = ""
if not revision:
kind = "commit" if commit else "revision"
raise ValueError(f"{url} names no {repo + ' ' if repo else ''}{kind}")
return revision
def platform_revision(platform_file: Path, repo: str = "", commit: bool = False) -> str:
"""The revision a platform YAML was scraped at, RuntimeError when its
source names none (`repo` and `commit` as for required_revision).
A target list or a launcher extraction describes the same release as
the platform's BIOS list: moving to a new release is a rescrape of the
platform first.
"""
with platform_file.open(encoding="utf-8") as handle:
source = str((yaml_load(handle) or {}).get("source") or "")
try:
return required_revision(source, repo, commit)
except ValueError as exc:
raise RuntimeError(
f"platforms/{platform_file.name} names no pinned revision in its "
f"source ({source!r}): rescrape the platform first"
) from exc