From 6172cb65c04f7e5ac636fc37c792a63a3dbbd616 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:49:59 +0100 Subject: [PATCH] refactor: recipes command imports its packages --- .github/workflows/deploy-site.yml | 8 +++---- scripts/acquire/__init__.py | 2 +- scripts/recipes/__init__.py | 2 +- scripts/recipes/capture.py | 3 +-- scripts/romset_recipes.py | 36 +++++++++++++------------------ tests/test_archive_build.py | 2 +- tests/test_auto_fetch_lookup.py | 13 ++++++----- tests/test_layers.py | 21 +++++++++--------- 8 files changed, 38 insertions(+), 49 deletions(-) diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml index b08d21677..538ffb7dd 100644 --- a/.github/workflows/deploy-site.yml +++ b/.github/workflows/deploy-site.yml @@ -11,7 +11,7 @@ on: - "exports/**" # Every script the build runs or imports, and no other # (tests/test_workflow_paths.py). - - "scripts/archive_build.py" + - "scripts/acquire/provenance.py" - "scripts/build_port.py" - "scripts/core/__init__.py" - "scripts/core/anchors.py" @@ -25,10 +25,8 @@ on: - "scripts/core/paths.py" - "scripts/core/romdir.py" - "scripts/core/sect233r1.py" - - "scripts/core/torrentzip.py" - "scripts/core/upstream.py" - "scripts/core/ziptools.py" - - "scripts/derivations.py" - "scripts/generate_event_schema.py" - "scripts/generate_readme.py" - "scripts/generate_site.py" @@ -70,7 +68,8 @@ on: - "scripts/packs/slots.py" - "scripts/packs/verdicts.py" - "scripts/pr_checks.py" - - "scripts/provenance_report.py" + - "scripts/recipes/__init__.py" + - "scripts/recipes/ledger.py" - "scripts/refresh_data_dirs.py" - "scripts/release_record.py" - "scripts/resolve/__init__.py" @@ -98,7 +97,6 @@ on: - "scripts/resolve/truth.py" - "scripts/resolve/validation.py" - "scripts/restore_large_files.py" - - "scripts/romset_recipes.py" - "scripts/site_markdown.py" - "scripts/sitegen/__init__.py" - "scripts/sitegen/agents.py" diff --git a/scripts/acquire/__init__.py b/scripts/acquire/__init__.py index 2d4d5ea5b..977700f28 100644 --- a/scripts/acquire/__init__.py +++ b/scripts/acquire/__init__.py @@ -1 +1 @@ -"""Acquisition: dump-catalog coverage, dumps a held file already contains, and the searches that find a missing file.""" +"""Acquisition: catalog coverage, dumps a held file contains, and the searches for a missing file.""" diff --git a/scripts/recipes/__init__.py b/scripts/recipes/__init__.py index 7350424af..394348336 100644 --- a/scripts/recipes/__init__.py +++ b/scripts/recipes/__init__.py @@ -1 +1 @@ -"""Archive recipes: romset identification, rebuilt pinned archives, and the recipe and build of every archive the collection holds.""" +"""Archive recipes: romset identification, rebuilt pinned archives, each held archive's recipe and build.""" diff --git a/scripts/recipes/capture.py b/scripts/recipes/capture.py index 9a082fa5c..36e4d134c 100644 --- a/scripts/recipes/capture.py +++ b/scripts/recipes/capture.py @@ -13,7 +13,7 @@ from typing import BinaryIO from core.torrentzip import write_torrentzip, zlib_ng_version from recipes.ledger import database_archives from recipes.members import _member_sha1, _opener - +from recipes.romsets import collect_recipes, load_dat_recipes _TORRENTZIP_COMMENT = b"TORRENTZIPPED-" @@ -140,7 +140,6 @@ def recipe_index(emulators_dir: str, recipes_dir: str) -> dict[MemberList, set[s knows the set under another name (``aga.zip`` is MAME's ``isa_aga``). """ from model.profiles import load_emulator_profiles - from recipes.romsets import collect_recipes, load_dat_recipes index: dict[MemberList, set[str]] = defaultdict(set) profiles = load_emulator_profiles(emulators_dir, skip_aliases=False) diff --git a/scripts/romset_recipes.py b/scripts/romset_recipes.py index a9b78a538..e9c9efe14 100644 --- a/scripts/romset_recipes.py +++ b/scripts/romset_recipes.py @@ -51,15 +51,16 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from core.cli import require_directory -from core.paths import REPO_ROOT +from core.torrentzip import zlib_ng_version from model.database import load_database from model.profiles import load_emulator_profiles +from recipes.build import AtomIndex, build_archive, select +from recipes.capture import ZlibNgError, capture, container_pins, recipe_index from recipes.identify import identify_report, missing_report, write_reconstructions +from recipes.ledger import ARCHIVES_FILE, load_ledger, write_ledger from recipes.pool import AtomPool, DIGEST_CACHE from recipes.romsets import collect_recipes, load_dat_recipes, merge_recipes -ARCHIVES_FILE = REPO_ROOT / "recipes" / "archives.json" - def print_missing(gap: dict) -> None: """The pinned archives the collection lacks, each one named.""" @@ -87,22 +88,19 @@ def print_missing(gap: dict) -> None: def run_capture(args: argparse.Namespace, db: dict) -> int: """Write recipes/archives.json for every archive the database lists.""" - from recipes import capture - from recipes import ledger as recipes_ledger - try: - records, failed = capture.capture( + records, failed = capture( db, - recipes_ledger.load_ledger(Path(args.archives)), - capture.recipe_index(args.emulators_dir, args.dat_recipes), - capture.container_pins(args.platforms_dir, args.emulators_dir), + load_ledger(Path(args.archives)), + recipe_index(args.emulators_dir, args.dat_recipes), + container_pins(args.platforms_dir, args.emulators_dir), ) - except capture.ZlibNgError as exc: + except ZlibNgError as exc: print(f"ERROR: {exc}", file=sys.stderr) return 2 for path, reason in sorted(failed.items()): print(f" UNREADABLE: {path}: {reason}", file=sys.stderr) - changed = recipes_ledger.write_ledger(records, Path(args.archives)) + changed = write_ledger(records, Path(args.archives)) by_class: dict[str, int] = defaultdict(int) for record in records.values(): by_class[record["rebuild"]] += 1 @@ -119,33 +117,29 @@ def run_capture(args: argparse.Namespace, db: dict) -> int: def run_build(args: argparse.Namespace, db: dict) -> int: """Build one archive, or all of them, from the members the collection holds.""" - from core import torrentzip - from recipes import build as recipes_build - from recipes import ledger as recipes_ledger - if not args.output_dir: print("ERROR: --build and --build-all need --output-dir", file=sys.stderr) return 2 - ledger = recipes_ledger.load_ledger(Path(args.archives)) + ledger = load_ledger(Path(args.archives)) if args.build_all: records = [ledger[path] for path in sorted(ledger)] else: - records = recipes_build.select(ledger, args.build) + records = select(ledger, args.build) if not records: print(f"ERROR: no archive recipe named {args.build}", file=sys.stderr) return 2 - ng = torrentzip.zlib_ng_version() + ng = zlib_ng_version() if ng: print( f" zlib-ng {ng} in use: TorrentZip bytes are stock zlib's output, so no " "build is claimed exact here; exact recipes are checked by their members" ) root = Path(args.bios_dir).resolve().parent - atoms = recipes_build.AtomIndex.from_collection(ledger, db, root) + atoms = AtomIndex.from_collection(ledger, db, root) counts: dict[str, int] = defaultdict(int) own: list[tuple[str, int]] = [] for record in records: - result = recipes_build.build_archive(record, atoms, Path(args.output_dir), root) + result = build_archive(record, atoms, Path(args.output_dir), root) counts[result.status] += 1 if result.own_members: own.append((result.path, result.own_members)) diff --git a/tests/test_archive_build.py b/tests/test_archive_build.py index c9d7fcaa3..8e9e20abf 100644 --- a/tests/test_archive_build.py +++ b/tests/test_archive_build.py @@ -199,7 +199,7 @@ class Capture(Fixture): self.assertEqual(records["bios/set.zip"]["members"][0]["sha1"], "kept") def test_zlib_ng_refuses_to_capture(self): - with mock.patch.object(recipes_build, "zlib_ng_version", lambda: "2.2.4"), \ + with mock.patch.object(recipes_capture, "zlib_ng_version", lambda: "2.2.4"), \ self.assertRaises(recipes_capture.ZlibNgError): self.capture() diff --git a/tests/test_auto_fetch_lookup.py b/tests/test_auto_fetch_lookup.py index 0002f787e..6a56c45db 100644 --- a/tests/test_auto_fetch_lookup.py +++ b/tests/test_auto_fetch_lookup.py @@ -15,7 +15,6 @@ import urllib.error from pathlib import Path from unittest import mock -import auto_fetch from acquire import fetch PAYLOAD = b"keyboard mcu" @@ -48,21 +47,21 @@ def _serving(payloads: dict[str, bytes]): class SoftwareHeritageAnswersByContent(unittest.TestCase): def test_the_bytes_it_serves_are_kept_when_the_hash_holds(self): urlopen, asked = _serving({f"{fetch.SOFTWARE_HERITAGE}/sha1:{SHA1}/raw/": PAYLOAD}) - with mock.patch.object(auto_fetch.urllib.request, "urlopen", urlopen): + with mock.patch.object(fetch.urllib.request, "urlopen", urlopen): data = fetch.step3b_software_heritage({"name": "D8039HLC.rom", "sha1": SHA1}) self.assertEqual(data, PAYLOAD) self.assertEqual(asked, [f"{fetch.SOFTWARE_HERITAGE}/sha1:{SHA1}/raw/"]) def test_other_bytes_under_the_hash_are_refused(self): urlopen, _asked = _serving({fetch.SOFTWARE_HERITAGE: b"something else"}) - with mock.patch.object(auto_fetch.urllib.request, "urlopen", urlopen): + with mock.patch.object(fetch.urllib.request, "urlopen", urlopen): data = fetch.step3b_software_heritage({"name": "x", "sha1": SHA1}) self.assertIsNone(data) def test_an_absent_file_is_not_counted_as_a_source_that_did_not_answer(self): urlopen, _asked = _serving({}) fetch._UNANSWERED.clear() - with mock.patch.object(auto_fetch.urllib.request, "urlopen", urlopen): + with mock.patch.object(fetch.urllib.request, "urlopen", urlopen): self.assertIsNone(fetch.step3b_software_heritage({"name": "x", "sha1": SHA1})) self.assertEqual(fetch._UNANSWERED, []) @@ -76,7 +75,7 @@ class LookupBySha1(unittest.TestCase): def test_a_found_file_goes_to_tmp_acquire_never_to_bios(self): urlopen, _asked = _serving({f"{fetch.SOFTWARE_HERITAGE}/sha1:{SHA1}/raw/": PAYLOAD}) - with mock.patch.object(auto_fetch.urllib.request, "urlopen", urlopen), \ + with mock.patch.object(fetch.urllib.request, "urlopen", urlopen), \ contextlib.redirect_stdout(io.StringIO()) as out: code = fetch.lookup_sha1s([SHA1.upper()], {"files": {}}) self.assertEqual(code, 0) @@ -87,7 +86,7 @@ class LookupBySha1(unittest.TestCase): def test_a_held_file_is_named_and_not_fetched(self): urlopen, asked = _serving({}) db = {"files": {SHA1: {"path": "bios/Commodore/Amiga/x.rom"}}} - with mock.patch.object(auto_fetch.urllib.request, "urlopen", urlopen), \ + with mock.patch.object(fetch.urllib.request, "urlopen", urlopen), \ contextlib.redirect_stdout(io.StringIO()) as out: code = fetch.lookup_sha1s([SHA1], db) self.assertEqual((code, asked), (0, [])) @@ -95,7 +94,7 @@ class LookupBySha1(unittest.TestCase): def test_nothing_found_and_a_malformed_hash_end_with_one(self): urlopen, _asked = _serving({}) - with mock.patch.object(auto_fetch.urllib.request, "urlopen", urlopen), \ + with mock.patch.object(fetch.urllib.request, "urlopen", urlopen), \ contextlib.redirect_stdout(io.StringIO()) as out, \ contextlib.redirect_stderr(io.StringIO()) as err: code = fetch.lookup_sha1s([SHA1, "not-a-hash"], {"files": {}}) diff --git a/tests/test_layers.py b/tests/test_layers.py index 645cb48c4..d2c5855df 100644 --- a/tests/test_layers.py +++ b/tests/test_layers.py @@ -20,35 +20,37 @@ from pathlib import Path SCRIPTS = Path(__file__).resolve().parent.parent / "scripts" PACKAGE_LAYERS = {"core": 0, "model": 1, "resolve": 2, "packs": 3, "scraper": 3, "exporter": 3, - "profiling": 3, "sitegen": 3} + "profiling": 3, "sitegen": 3, "recipes": 3, "acquire": 3} LIBRARY_LAYER = 3 COMMAND_LAYER = 4 # The other domain packages a domain package may import. A package listed # here imports no sibling outside its set. SIBLING_IMPORTS: dict[str, frozenset[str]] = { + # A search starts from what verify finds missing. + "acquire": frozenset({"packs"}), "packs": frozenset(), "profiling": frozenset(), - "sitegen": frozenset({"packs"}), + "recipes": frozenset(), + # The site prints the recipe counts and the dump-catalog coverage report + # those two packages compute. + "sitegen": frozenset({"acquire", "packs", "recipes"}), } # Top-level modules other modules of scripts/ import: library code that a # later wave moves into a package. NOT_YET_PACKAGED = frozenset({ - "archive_build", "autorun", "build_port", "campaign", "campaign_profiles", "campaign_rules", - "check_buildbot_system", "check_release_assets", "commit_rules", "core_probe", "derivations", + "autorun", "build_port", "campaign", "campaign_profiles", "campaign_rules", + "check_buildbot_system", "check_release_assets", "commit_rules", "core_probe", "generate_event_schema", "measurement_records", "pin_installer", "pr_checks", - "provenance_report", "release_record", "romfolders", "romset_recipes", + "release_record", "romfolders", }) # Imports from a package module to a top-level module, left for the wave that # packages the top-level side. PENDING_TOP_LEVEL_IMPORTS = frozenset({ - ("sitegen.build", "archive_build"), - ("sitegen.build", "provenance_report"), ("sitegen.build", "release_record"), ("sitegen.config", "release_record"), - ("sitegen.dataexports", "provenance_report"), ("sitegen.decorate", "release_record"), ("sitegen.descriptions", "release_record"), ("sitegen.getpages", "release_record"), @@ -56,14 +58,11 @@ PENDING_TOP_LEVEL_IMPORTS = frozenset({ ("sitegen.handheld", "build_port"), ("sitegen.measurements", "measurement_records"), ("sitegen.platformindex", "release_record"), - ("sitegen.provenance", "provenance_report"), ("sitegen.publish", "release_record"), - ("sitegen.readme", "archive_build"), ("sitegen.readme", "pr_checks"), ("sitegen.readme", "release_record"), ("sitegen.validate", "release_record"), ("sitegen.versions", "release_record"), - ("sitegen.wanted", "archive_build"), ("sitegen.whichpack", "release_record"), })