diff --git a/scripts/probes/content.py b/scripts/probes/content.py index 2066bcce3..29b4e8477 100644 --- a/scripts/probes/content.py +++ b/scripts/probes/content.py @@ -6,6 +6,10 @@ import contextlib import json import os import shutil +import subprocess +import sys +import time +from collections.abc import Callable from dataclasses import dataclass from pathlib import Path from typing import TYPE_CHECKING @@ -14,7 +18,7 @@ import yaml from core.hashing import file_digest from core.statefile import now_iso -from probes.host import Unavailable, child_env +from probes.host import NETWORK_DELAYS, Unavailable, child_env, run_group if TYPE_CHECKING: from probes.campaign import Campaign @@ -101,6 +105,44 @@ def folder_size(folder: Path) -> int: return sum(p.stat().st_size for p in folder.rglob("*") if p.is_file()) +def find_romfetch(configured: str = "") -> str | None: + """romfetch as --romfetch or RETROBIOS_ROMFETCH names it, else on PATH.""" + configured = configured or os.environ.get(ROMFETCH_ENV, "") + if configured: + return configured if Path(configured).is_file() or shutil.which(configured) else None + return shutil.which("romfetch") + + +def _stderr(message: str) -> None: + print(message, file=sys.stderr) + + +def fetch_content(content: Content, content_dir: Path, romfetch: str, *, + run: Callable[..., subprocess.CompletedProcess] = run_group, + sleep: Callable[[float], None] = time.sleep, + delays: tuple[float, ...] = NETWORK_DELAYS, + log: Callable[[str], None] = _stderr) -> None: + """romfetch get, run again after each delay while it fails for a + transient reason. Unavailable when it does not deliver.""" + argv = [romfetch, "get", content.romfetch_system, content.name, "--json", "-q", + "--content-dir", str(content_dir)] + for attempt, delay in enumerate((*delays, None)): + proc = run(argv, capture_output=True, text=True, check=False, + timeout=6 * 3600, env=child_env(romfetch=True)) + if proc.returncode == 0: + log(f"romfetch {content.name}: {proc.stdout.strip()[:300]}") + return + code = "unknown" + with contextlib.suppress(ValueError, AttributeError, KeyError, TypeError): + code = json.loads(proc.stdout)["error"]["code"] + if proc.returncode != ROMFETCH_TRANSIENT or delay is None: + raise Unavailable(f"content: romfetch exit {proc.returncode} ({code}) " + f"for {content.name!r}") + log(f"romfetch {content.name}: exit {proc.returncode} ({code}), " + f"retry {attempt + 1} in {delay}s") + sleep(delay) + + class ContentStore: """Content on disk, its users counted, the least recently used evicted past the cap. Only what the campaign fetched is ever deleted, and never @@ -137,10 +179,7 @@ class ContentStore: return folder def romfetch(self) -> str | None: - configured = self.c.settings.romfetch or os.environ.get(ROMFETCH_ENV, "") - if configured: - return configured if Path(configured).is_file() or shutil.which(configured) else None - return shutil.which("romfetch") + return find_romfetch(self.c.settings.romfetch) def fetch(self, content: Content) -> None: command = self.romfetch() @@ -148,23 +187,8 @@ class ContentStore: raise Unavailable(f"content: romfetch not found, {content.catalog} entry {content.name!r}") if not self.c.settings.network: raise Unavailable(f"content: offline, {content.catalog} entry {content.name!r}") - argv = [command, "get", content.romfetch_system, content.name, "--json", "-q", - "--content-dir", str(self.c.paths.content)] - for attempt, delay in enumerate((*self.c.settings.delays, None)): - proc = self.c.host.run(argv, capture_output=True, text=True, check=False, - timeout=6 * 3600, env=child_env(romfetch=True)) - if proc.returncode == 0: - self.c.log(f"romfetch {content.name}: {proc.stdout.strip()[:300]}") - return - code = "unknown" - with contextlib.suppress(ValueError, AttributeError): - code = json.loads(proc.stdout)["error"]["code"] - if proc.returncode != ROMFETCH_TRANSIENT or delay is None: - raise Unavailable(f"content: romfetch exit {proc.returncode} ({code}) " - f"for {content.name!r}") - self.c.log(f"romfetch {content.name}: exit {proc.returncode} ({code}), " - f"retry {attempt + 1} in {delay}s") - self.c.host.sleep(delay) + fetch_content(content, self.c.paths.content, command, run=self.c.host.run, + sleep=self.c.host.sleep, delays=self.c.settings.delays, log=self.c.log) def release(self, content: Content, user: str) -> None: users = self.refs.get(content.key, set()) diff --git a/scripts/probes/packprobe.py b/scripts/probes/packprobe.py index bcd745005..31e5783ca 100644 --- a/scripts/probes/packprobe.py +++ b/scripts/probes/packprobe.py @@ -23,10 +23,10 @@ from model.platforms import load_platform_config from model.profiles import load_emulator_profiles from model.systems import norm_system_id from model.targets import load_target_config -from probes.content import Content, load_content, load_file, verify_identity +from probes.content import Content, fetch_content, find_romfetch, load_content, load_file, verify_identity from probes.cores import buildbot_listing, cached_nightly from probes.entries import entry_dest -from probes.host import CONTENT_FILE, PROBE_TIMEOUT, Unavailable, child_env, run_group, run_when_idle +from probes.host import CONTENT_FILE, PROBE_TIMEOUT, Unavailable, run_group, run_when_idle from probes.packlayout import ( Layout, ProbeFailure, @@ -246,14 +246,10 @@ def content_folder(content: Content, fetch: bool) -> Path: """The game's folder, fetched through romfetch when absent and asked to.""" folder = CONTENT_DIR / content.romfetch_system / content.name if not (folder / "identity.json").is_file() and fetch: - romfetch = shutil.which("romfetch") + romfetch = find_romfetch() if not romfetch: raise Unavailable("romfetch not found") - proc = subprocess.run([romfetch, "get", content.romfetch_system, content.name, "--json", "-q", - "--content-dir", str(CONTENT_DIR)], capture_output=True, text=True, - check=False, env=child_env(romfetch=True)) - if proc.returncode != 0: - raise Unavailable(f"romfetch exit {proc.returncode} for {content.name!r}") + fetch_content(content, CONTENT_DIR, romfetch) verify_identity(folder) return folder diff --git a/tests/test_pack_probe.py b/tests/test_pack_probe.py index 967a9d87c..750b359d4 100644 --- a/tests/test_pack_probe.py +++ b/tests/test_pack_probe.py @@ -255,6 +255,37 @@ class SitePages(unittest.TestCase): self.assertEqual(sitemarkup.inline_html("[x](../a.md)"), 'x') +class ContentFetch(unittest.TestCase): + """pack_probe fetches its games as the campaign does: romfetch named by + RETROBIOS_ROMFETCH, a transient failure tried again.""" + + def test_the_configured_romfetch_is_retried(self) -> None: + import subprocess + import unittest.mock + + from probes import content as probecontent + + work = TMP / "fetch" + shutil.rmtree(work, ignore_errors=True) + work.mkdir(parents=True) + fetcher = work / "my-romfetch" + fetcher.write_text("", encoding="utf-8") + game = probecontent.Content("snes", "snes", "Game (USA)", "no-intro", "", "usa", "", 60, 10, + False, None) + codes = [3, 0] + calls = [] + + def run(argv, **_kwargs): + calls.append(argv[0]) + return subprocess.CompletedProcess(argv, codes[len(calls) - 1], "{}", "") + + with unittest.mock.patch.dict(os.environ, {"RETROBIOS_ROMFETCH": str(fetcher), "PATH": ""}): + romfetch = probecontent.find_romfetch() + probecontent.fetch_content(game, work, romfetch, run=run, sleep=lambda _s: None, + log=lambda _m: None) + self.assertEqual(calls, [str(fetcher), str(fetcher)]) + + class Lock(unittest.TestCase): """The probe lock is held for one core_probe run and released after it."""