diff --git a/scripts/scraper/base_scraper.py b/scripts/scraper/base_scraper.py index ccbbb8f78..8b82f3a0f 100644 --- a/scripts/scraper/base_scraper.py +++ b/scripts/scraper/base_scraper.py @@ -535,7 +535,7 @@ def fetch_branch_head(repo: str, branch: str | None = None) -> str: return sha -class _PlatformDumper(yaml.SafeDumper): +class PlatformDumper(yaml.SafeDumper): """Quote scalars that a YAML reader could misparse. Names such as "7800 BIOS (E).rom" or "Bandai - WonderSwan: x" are plain @@ -557,7 +557,7 @@ def _quoted_str(dumper: yaml.SafeDumper, data: str) -> yaml.ScalarNode: return dumper.represent_scalar("tag:yaml.org,2002:str", data, style=style) -_PlatformDumper.add_representer(str, _quoted_str) +PlatformDumper.add_representer(str, _quoted_str) def exit_on_scrape_error(error: Exception) -> NoReturn: @@ -655,7 +655,7 @@ def _write_output( # Written whole or not at all: a reader in the middle of a streamed # dump parsed a valid platform with a fraction of its systems. write_text_atomic(output_path, yaml.dump( - config, Dumper=_PlatformDumper, + config, Dumper=PlatformDumper, default_flow_style=False, allow_unicode=True, sort_keys=False, )) print(f"Written {len(reqs)} entries to {output}") diff --git a/scripts/scraper/romfolders/__init__.py b/scripts/scraper/romfolders/__init__.py index 12dcc4da1..3c33656d2 100644 --- a/scripts/scraper/romfolders/__init__.py +++ b/scripts/scraper/romfolders/__init__.py @@ -20,12 +20,16 @@ from collections.abc import Callable from dataclasses import dataclass, field from pathlib import Path +import yaml + if (_SCRIPTS_DIR := str(Path(__file__).resolve().parents[2])) not in sys.path: sys.path.insert(0, _SCRIPTS_DIR) from core import upstream from model.sources import source_yaml_file +from ..base_scraper import PlatformDumper + CACHE_DIR = upstream.CACHE_DIR # Keys a setup file carries, in the order they are written. @@ -201,3 +205,10 @@ def scrape_setup(setup: str, spec: dict, fetcher: Fetcher) -> dict: doc = {"setup": setup, **{k: spec[k] for k in SETUP_KEYS[1:-1] if k in spec}} doc["folders"] = [folders[k] for k in sorted(folders, key=lambda f: (f.lower(), f))] return doc + + +def dump_setup(doc: dict) -> str: + """The bytes of platforms/romfolders/.yml for a setup document.""" + return yaml.dump( + doc, Dumper=PlatformDumper, sort_keys=False, allow_unicode=True, width=100 + ) diff --git a/scripts/scraper/romfolders/__main__.py b/scripts/scraper/romfolders/__main__.py index 1aefd2fd1..db29450f5 100644 --- a/scripts/scraper/romfolders/__main__.py +++ b/scripts/scraper/romfolders/__main__.py @@ -11,22 +11,13 @@ import argparse import sys from pathlib import Path -import yaml - -from . import Fetcher, SourceError, load_sources, scrape_setup +from . import Fetcher, SourceError, dump_setup, load_sources, scrape_setup from core.upstream import UpstreamError -from core.artifacts import write_text_atomic -from ..base_scraper import _PlatformDumper +from core.artifacts import write_if_changed DEFAULT_DIR = Path("platforms") / "romfolders" -def dump_setup(doc: dict) -> str: - return yaml.dump( - doc, Dumper=_PlatformDumper, sort_keys=False, allow_unicode=True, width=100 - ) - - def main() -> int: parser = argparse.ArgumentParser(allow_abbrev=False, description=__doc__.splitlines()[0]) which = parser.add_mutually_exclusive_group(required=True) @@ -64,10 +55,7 @@ def main() -> int: print(f"{setup}: {len(doc['folders'])} folders") if not args.dry_run: args.output_dir.mkdir(parents=True, exist_ok=True) - target = args.output_dir / f"{setup}.yml" - text = dump_setup(doc) - if not target.exists() or target.read_text(encoding="utf-8") != text: - write_text_atomic(str(target), text) + write_if_changed(str(args.output_dir / f"{setup}.yml"), dump_setup(doc)) return 1 if failed else 0 diff --git a/tests/test_romfolders_card.py b/tests/test_romfolders_card.py index 3faaed320..0932dd479 100644 --- a/tests/test_romfolders_card.py +++ b/tests/test_romfolders_card.py @@ -15,8 +15,7 @@ from pathlib import Path from model.romfolders import source_refs, split_refs from model.safeparse import yaml_load -from scraper.romfolders import SourceError, load_sources, scrape_setup -from scraper.romfolders.__main__ import dump_setup +from scraper.romfolders import SourceError, dump_setup, load_sources, scrape_setup from scraper.romfolders.card import ( libretro_core, parse_allium, diff --git a/tests/test_romfolders_es.py b/tests/test_romfolders_es.py index e9589c337..9cb2e2529 100644 --- a/tests/test_romfolders_es.py +++ b/tests/test_romfolders_es.py @@ -19,11 +19,11 @@ from model.romfolders import setup_paths, source_refs, split_refs from scraper.romfolders import ( Fetcher, SourceError, + dump_setup, load_formats, load_sources, scrape_setup, ) -from scraper.romfolders.__main__ import dump_setup from scraper.retropie_modules import module_enabled ROOT = Path(__file__).resolve().parent.parent diff --git a/tests/test_scraper_contract.py b/tests/test_scraper_contract.py index b4a773249..088980cc1 100644 --- a/tests/test_scraper_contract.py +++ b/tests/test_scraper_contract.py @@ -1274,10 +1274,10 @@ class PlatformFileHasNoAnchors(unittest.TestCase): the platform file read back the same, but held &id001 and *id001.""" import yaml - from scraper.base_scraper import _PlatformDumper + from scraper.base_scraper import PlatformDumper names = ["kick34005.A500", "amiga-os-130.rom"] - text = yaml.dump([{"names": names}, {"names": names}], Dumper=_PlatformDumper) + text = yaml.dump([{"names": names}, {"names": names}], Dumper=PlatformDumper) self.assertNotIn("&id", text) self.assertNotIn("*id", text) self.assertEqual(yaml.safe_load(text), [{"names": names}, {"names": names}])