diff --git a/scripts/check_buildbot_system.py b/scripts/check_buildbot_system.py index 48240b5f4..c92ef170f 100644 --- a/scripts/check_buildbot_system.py +++ b/scripts/check_buildbot_system.py @@ -19,12 +19,14 @@ import os import sys from core.cli import InputMissing from core.paths import DATA_DIRS_FILE +from model.registry import data_dir_registry_at +from packs.datadirs import refresh_keys from scraper.buildbot_system import ( check, pending, print_report, unanswered, - update_changed, + updated, ) @@ -60,7 +62,11 @@ def main() -> None: print_report(report) if args.update: - failed = update_changed(report) + # The registry checked is the one refreshed, read in this process: + # a child run by relative path read the default registry, and failed + # outside the repository root. + registry = data_dir_registry_at(args.registry, required=True) + failed = refresh_keys(registry, updated(report), force=True) if failed: print(f"ERROR: refresh failed for {', '.join(failed)}", file=sys.stderr) sys.exit(1) diff --git a/scripts/packs/datadirs.py b/scripts/packs/datadirs.py index cecc6a8fc..892586440 100644 --- a/scripts/packs/datadirs.py +++ b/scripts/packs/datadirs.py @@ -579,6 +579,31 @@ def refresh_all( return results +def refresh_keys( + registry: dict[str, dict], + keys: list[str], + *, + force: bool = False, + versions_path: str = VERSIONS_FILE, +) -> list[str]: + """Refresh the named entries of a registry and return the keys whose + refresh failed.""" + failed: list[str] = [] + refreshed = False + for key in keys: + log.info("refreshing %s ...", key) + outcome = refresh_entry(key, registry[key], force=force, versions_path=versions_path) + if outcome is None: + log.error("refresh of %s failed", key) + failed.append(key) + refreshed = refreshed or bool(outcome) + if refreshed: + from resolve.datadirs import forget_data_dir_index + + forget_data_dir_index() + return failed + + def summarize(outcomes: dict[str, bool | None], dry_run: bool) -> str: """One closing line: how many entries changed, were current, failed.""" refreshed = sum(1 for o in outcomes.values() if o is True) diff --git a/scripts/scraper/buildbot_system.py b/scripts/scraper/buildbot_system.py index 691c5acce..bbf136845 100644 --- a/scripts/scraper/buildbot_system.py +++ b/scripts/scraper/buildbot_system.py @@ -3,8 +3,6 @@ from __future__ import annotations import logging -import subprocess -import sys import urllib.error import urllib.parse @@ -104,26 +102,9 @@ def print_report(report: dict) -> None: print(f"\nSummary: {', '.join(parts)}") -def update_changed(report: dict) -> list[str]: - """Refresh entries that have changed and return the keys whose refresh failed.""" - failed: list[str] = [] - for e in report.get("entries", []): - if e["status"] == "UPDATED" and e.get("key"): - log.info("refreshing %s ...", e["key"]) - result = subprocess.run( - [ - sys.executable, - "scripts/refresh_data_dirs.py", - "--force", - "--key", - e["key"], - ], - check=False, - ) - if result.returncode != 0: - log.error("refresh of %s failed (exit %d)", e["key"], result.returncode) - failed.append(e["key"]) - return failed +def updated(report: dict) -> list[str]: + """Keys of the tracked entries whose remote version changed.""" + return [e["key"] for e in report.get("entries", []) if e["status"] == "UPDATED" and e.get("key")] def pending(report: dict) -> list[dict]: diff --git a/tests/sources.py b/tests/sources.py index 33587a316..5c4ad20a2 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -61,7 +61,7 @@ COMMANDS = { "generate_installer_doc.py": ("installer_build/installerdoc.py",), "commit_rules.py": ("devtools/commitrules.py",), "pr_checks.py": ("devtools/prchecks.py",), - "check_buildbot_system.py": ("scraper/buildbot_system.py",), + "check_buildbot_system.py": ("scraper/buildbot_system.py", "packs/datadirs.py"), "generate_site.py": ( "sitegen/config.py", "sitegen/emulatorsindex.py", "sitegen/places.py", "sitegen/home.py", "sitegen/versions.py", "sitegen/guidepage.py", "sitegen/install.py", "sitegen/getpages.py", diff --git a/tests/test_refresh_failures.py b/tests/test_refresh_failures.py index 85c497dd7..fc6379d46 100644 --- a/tests/test_refresh_failures.py +++ b/tests/test_refresh_failures.py @@ -10,6 +10,8 @@ from __future__ import annotations import tests +import contextlib +import io import json import os import shutil @@ -185,14 +187,45 @@ class RefreshFailuresAreFailures(unittest.TestCase): def test_a_failed_refresh_is_returned_and_exits_nonzero(self): report = {"entries": [{"status": "UPDATED", "key": "dolphin-sys"}, {"status": "OK", "key": "ppsspp-assets"}]} - with mock.patch.object( - buildbot_system.subprocess, "run", - return_value=subprocess.CompletedProcess([], 1), - ): - self.assertEqual(buildbot_system.update_changed(report), ["dolphin-sys"]) + self.assertEqual(buildbot_system.updated(report), ["dolphin-sys"]) + registry = {"dolphin-sys": {"local_cache": "data/dolphin-sys"}} + with mock.patch.object(datadirs, "refresh_entry", return_value=None): + self.assertEqual(datadirs.refresh_keys(registry, ["dolphin-sys"]), ["dolphin-sys"]) source = (REPO_ROOT / "scripts" / "check_buildbot_system.py").read_text(encoding="utf-8") - self.assertIn("failed = update_changed(report)", source) - self.assertIn("sys.exit(1)", source[source.index("failed = update_changed(report)"):]) + self.assertIn("failed = refresh_keys(", source) + self.assertIn("sys.exit(1)", source[source.index("failed = refresh_keys("):]) + + def test_update_refreshes_from_the_registry_it_checked(self): + """--update ran scripts/refresh_data_dirs.py by a relative path and + without --registry: the refresh read the default registry, and from + any other directory the child was not found.""" + import check_buildbot_system as cbs + + with tempfile.TemporaryDirectory() as tmp: + registry = Path(tmp, "other.yml") + registry.write_text( + "data_directories:\n" + " elsewhere:\n" + " source_url: https://buildbot.libretro.com/assets/system/Else.zip\n" + " source_type: zip\n" + f" local_cache: {tmp}/else\n", + encoding="utf-8", + ) + seen: list[tuple[str, dict]] = [] + + def refresh(key, entry, **_kwargs): + seen.append((key, entry)) + return True + + with mock.patch.object(buildbot_system, "fetch_index", return_value={"Else.zip"}), \ + mock.patch.object(buildbot_system, "get_remote_etag", return_value='"new"'), \ + mock.patch.object(datadirs, "refresh_entry", side_effect=refresh), \ + mock.patch.object(sys, "argv", ["check_buildbot_system.py", "--update", + "--registry", str(registry)]), \ + contextlib.redirect_stdout(io.StringIO()): + cbs.main() + self.assertEqual([key for key, _entry in seen], ["elsewhere"]) + self.assertEqual(seen[0][1]["local_cache"], f"{tmp}/else") class AnEmptyExtractionKeepsTheCache(unittest.TestCase):