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.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 20:02:15 +01:00
1 parent c25426f14c
commit f61c2cb7d4
43 files changed
+791 -670

No files matched your search

+9 -48
View File
@@ -32,12 +32,9 @@ from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
import shutil
import sys
import urllib.parse
import urllib.request
import zipfile
from collections.abc import Callable, Iterator
from dataclasses import dataclass
@@ -50,6 +47,7 @@ from model.profiles import load_emulator_profiles
from model.platforms import load_platform_config
from core.cli import add_repo_inputs, run_cli
from model.dumpcatalog import build_snapshot, load_provenance_snapshots, write_snapshot
from .base_scraper import download, fetch_github_latest_version, github_json
from .logiqx_parser import LogiqxDat, parse_logiqx
MAX_MEMBER_SIZE = 200 * 1024 * 1024
@@ -481,37 +479,20 @@ def _iter_dat_contents(pack: Path) -> Iterator[tuple[str, bytes]]:
MAME_LISTXML_URL = (
"https://github.com/mamedev/mame/releases/download/{tag}/{tag}lx.zip"
)
MAME_LATEST_API = "https://api.github.com/repos/mamedev/mame/releases/latest"
MAME_REPO = "mamedev/mame"
FBNEO_API = "https://api.github.com/repos/libretro/FBNeo"
def _api_json(url: str) -> object:
from .base_scraper import github_headers
request = urllib.request.Request(url, headers=github_headers())
with urllib.request.urlopen(request, timeout=60) as response:
return json.load(response)
class NoReleaseError(ValueError):
def __init__(self) -> None:
super().__init__("the newest MAME release carries no tag")
def latest_mame_tag() -> str:
"""Tag of the newest MAME release, the one ``--fetch`` takes by default."""
payload = _api_json(MAME_LATEST_API)
tag = payload.get("tag_name") if isinstance(payload, dict) else None
if not tag:
raise NoReleaseError
return str(tag)
"""An api.github.com answer, ConnectionError when it cannot be read."""
return github_json(url, f"cannot read {url}", timeout=60, error=ConnectionError)
def fetch_tag(source: str, requested: str) -> str:
"""The revision a ``--fetch`` names: bare, the newest MAME release or
the tip of FBNeo's default branch."""
if source == "mame" and requested == "latest":
tag = latest_mame_tag()
tag = fetch_github_latest_version(MAME_REPO)
print(f" Derniere release MAME : {tag}")
return tag
return requested
@@ -529,25 +510,6 @@ def git_blob_sha(data: bytes) -> str:
return digest.hexdigest()
def _download(url: str, destination: Path) -> Path:
"""Fetch *url* to *destination* unless it is already there."""
if destination.is_file():
return destination
destination.parent.mkdir(parents=True, exist_ok=True)
request = urllib.request.Request(url, headers={"User-Agent": "retrobios"})
scratch = destination.with_suffix(destination.suffix + f".{os.getpid()}.part")
try:
with urllib.request.urlopen(request, timeout=300) as response, scratch.open(
"wb"
) as handle:
shutil.copyfileobj(response, handle)
scratch.replace(destination)
finally:
if scratch.exists():
scratch.unlink()
return destination
def fetch_pack(
source: str, tag: str, cache_dir: Path
) -> tuple[Path, dict[str, str]]:
@@ -560,10 +522,9 @@ def fetch_pack(
FBNeo, whose files change under a constant header version.
"""
if source == "mame":
return (
_download(MAME_LISTXML_URL.format(tag=tag), cache_dir / f"{tag}lx.zip"),
{},
)
listxml = cache_dir / f"{tag}lx.zip"
download(MAME_LISTXML_URL.format(tag=tag), listxml, skip_existing=True)
return listxml, {}
# The revision is resolved to a commit first and everything is read at
# that commit: a branch URL is served through a CDN cache, so the bytes
@@ -587,7 +548,7 @@ def fetch_pack(
# only while its blob sha is the one the repository lists.
if destination.is_file() and git_blob_sha(destination.read_bytes()) != item["sha"]:
destination.unlink()
_download(item["download_url"], destination)
download(item["download_url"], destination, skip_existing=True)
if git_blob_sha(destination.read_bytes()) != item["sha"]:
destination.unlink()
raise BlobMismatchError(