From 79c3c02885c70fa843b47cea8a83e1ca143e16b1 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:33:40 +0100 Subject: [PATCH] fix: target scrapers exit on errors like scrapers --- scripts/scraper/base_scraper.py | 10 +++--- scripts/scraper/targets/__init__.py | 9 +++-- .../targets/retroarch_targets_scraper.py | 36 ++----------------- tests/test_target_scrapers.py | 13 +++++++ 4 files changed, 26 insertions(+), 42 deletions(-) diff --git a/scripts/scraper/base_scraper.py b/scripts/scraper/base_scraper.py index c73d62c22..8201b3f16 100644 --- a/scripts/scraper/base_scraper.py +++ b/scripts/scraper/base_scraper.py @@ -518,7 +518,7 @@ def _quoted_str(dumper: yaml.SafeDumper, data: str) -> yaml.ScalarNode: _PlatformDumper.add_representer(str, _quoted_str) -def _exit_on_scrape_error(error: Exception) -> NoReturn: +def exit_on_scrape_error(error: Exception) -> NoReturn: """Print what failed and what to try, then exit 1 without a traceback.""" print(f"Error: {error}", file=sys.stderr) refused = re.search(r"\b(403|429)\b|rate limit", str(error), re.I) @@ -617,13 +617,13 @@ def _write_output( try: config = generate() except (OSError, ValueError, RuntimeError) as e: - _exit_on_scrape_error(e) + exit_on_scrape_error(e) else: config = _generic_config(reqs) if scraper.lists_files and not any( (system or {}).get("files") for system in (config.get("systems") or {}).values() ): - _exit_on_scrape_error(RuntimeError( + exit_on_scrape_error(RuntimeError( f"{scraper_class.__module__}: the generated platform lists no file" )) output_path = Path(output) @@ -658,11 +658,11 @@ def scraper_cli( scraper = scraper_class(revision=args.revision) if takes_revision else scraper_class() reqs = scraper.fetch_requirements() except (OSError, ValueError, RuntimeError) as e: - _exit_on_scrape_error(e) + exit_on_scrape_error(e) if scraper.lists_files and not reqs: # Written, an empty read became a platform with no system that # verify and the pack count 0/0 and call consistent. - _exit_on_scrape_error(RuntimeError( + exit_on_scrape_error(RuntimeError( f"{scraper_class.__module__}: upstream returned no BIOS entry" )) diff --git a/scripts/scraper/targets/__init__.py b/scripts/scraper/targets/__init__.py index 77375a88c..444bef32e 100644 --- a/scripts/scraper/targets/__init__.py +++ b/scripts/scraper/targets/__init__.py @@ -16,7 +16,7 @@ from model.sourcerevision import platform_revision # Every target scraper reads the network through the platform scrapers' # helpers: one retry policy, one size cap, one token rule. -from ..base_scraper import fetch_text, forge_headers +from ..base_scraper import exit_on_scrape_error, fetch_text, forge_headers __all__ = ["BaseTargetScraper", "discover_target_scrapers", "fetch_text", "forge_headers", "target_cli"] @@ -66,8 +66,11 @@ def target_cli(scraper_class: type[BaseTargetScraper], description: str, unit: s parser.add_argument("--output", "-o", help="Output YAML file") args = parser.parse_args() - scraper = scraper_class() - data = scraper.fetch_targets() + try: + scraper = scraper_class() + data = scraper.fetch_targets() + except (OSError, ValueError, RuntimeError) as e: + exit_on_scrape_error(e) if args.dry_run: for name, info in data["targets"].items(): print(f" {name} ({info['architecture']}): {len(info['cores'])} {unit}") diff --git a/scripts/scraper/targets/retroarch_targets_scraper.py b/scripts/scraper/targets/retroarch_targets_scraper.py index 98d6cf6b2..655f824c6 100644 --- a/scripts/scraper/targets/retroarch_targets_scraper.py +++ b/scripts/scraper/targets/retroarch_targets_scraper.py @@ -20,14 +20,11 @@ Buildbot structure varies by platform: from __future__ import annotations -import argparse import re import sys from datetime import datetime, timezone -import yaml - -from . import BaseTargetScraper, fetch_text +from . import BaseTargetScraper, fetch_text, target_cli PLATFORM_NAME = "retroarch" @@ -165,36 +162,7 @@ class Scraper(BaseTargetScraper): def main() -> None: - parser = argparse.ArgumentParser( - allow_abbrev=False, - description="Scrape RetroArch buildbot nightly targets" - ) - parser.add_argument("--dry-run", action="store_true", help="Show target summary") - parser.add_argument("--output", "-o", help="Output YAML file") - args = parser.parse_args() - - scraper = Scraper() - data = scraper.fetch_targets() - - total_cores = sum(len(t["cores"]) for t in data["targets"].values()) - print( - f"\n{len(data['targets'])} targets, {total_cores} total core entries", - file=sys.stderr, - ) - - if args.dry_run: - for name, info in sorted(data["targets"].items()): - print( - f" {name:30s} {info['architecture']:10s} {len(info['cores']):>4d} cores" - ) - return - - if args.output: - scraper.write_output(data, args.output) - print(f"Written to {args.output}") - return - - print(yaml.dump(data, default_flow_style=False, sort_keys=False)) + target_cli(Scraper, "Scrape RetroArch buildbot nightly targets") if __name__ == "__main__": diff --git a/tests/test_target_scrapers.py b/tests/test_target_scrapers.py index f1cbb598f..b2b61d8b3 100644 --- a/tests/test_target_scrapers.py +++ b/tests/test_target_scrapers.py @@ -118,6 +118,19 @@ class FailedRequestsStopTheScrape(unittest.TestCase): self.assertEqual(targets["x86_64"]["cores"], ["mgba"]) self.assertNotIn("x86_wow64", targets) + def test_the_command_line_names_the_failure(self): + """target_cli let the RuntimeError out as a traceback, without the + `gh auth login` hint scraper_cli gives on a refused API call.""" + stderr = io.StringIO() + with mock.patch("urllib.request.urlopen", _refuse), mock.patch( + "sys.argv", ["retroarch_targets_scraper", "--dry-run"] + ), mock.patch("sys.stderr", stderr), mock.patch( + "scripts.scraper.base_scraper.github_token", return_value="" + ), self.assertRaises(SystemExit) as raised: + retroarch_targets_scraper.main() + self.assertEqual(raised.exception.code, 1) + self.assertIn("Error:", stderr.getvalue()) + class BatoceraKconfig(unittest.TestCase): """The Batocera target scraper read every select as unconditional and