mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-24 10:37:54 -06:00
fix: fetch pack_probe content like the campaign
probes.content.fetch_content and find_romfetch serve ContentStore and pack_probe: RETROBIOS_ROMFETCH is honoured, a transient romfetch exit is retried, the run has a timeout in its own process group, and an exit without a JSON error block no longer raises KeyError.
This commit is contained in:
1 parent
d2984bc466
commit
8255d94e75
3 files changed
+81
-30
No files matched your search
+46
-22
@@ -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())
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user