diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml index d39bff899..27bd194c9 100644 --- a/.github/workflows/deploy-site.yml +++ b/.github/workflows/deploy-site.yml @@ -145,6 +145,7 @@ on: - "scripts/sitegen/agents.py" - "scripts/sitegen/apiitems.py" - "scripts/sitegen/arcadeboards.py" + - "scripts/sitegen/archivecounts.py" - "scripts/sitegen/bootstrap.py" - "scripts/sitegen/build.py" - "scripts/sitegen/cards.py" diff --git a/scripts/sitegen/archivecounts.py b/scripts/sitegen/archivecounts.py new file mode 100644 index 000000000..120978cf5 --- /dev/null +++ b/scripts/sitegen/archivecounts.py @@ -0,0 +1,31 @@ +"""The generated block of wiki/archive-recipes.md: how many archives rebuild exact and canonical.""" + +from __future__ import annotations + +import re +from pathlib import Path + +from sitegen.render import WIKI_SRC_DIR + +ARCHIVE_RECIPES_PAGE = Path(WIKI_SRC_DIR) / "archive-recipes.md" +_ARCHIVE_BLOCK = re.compile( + r"(\n)(.*?)()", re.S) + + +def archive_recipe_lines(counts: dict[str, int]) -> list[str]: + """What an exact and a canonical rebuild are, each with its count.""" + total = f"{counts['total']:,}" + return [ + f"- **Exact**: the same bytes as the archive in the collection." + f" {counts['exact']:,} of {total} archives.", + f"- **Canonical**: the same files inside, packed another way." + f" {counts['canonical']:,} of {total} archives.", + ] + + +def with_archive_counts(page: str, counts: dict[str, int]) -> str: + """The archive recipes page with its counts read from recipes/archives.json.""" + block = "\n".join(archive_recipe_lines(counts)) + "\n" + if not _ARCHIVE_BLOCK.search(page): + raise ValueError(f"{ARCHIVE_RECIPES_PAGE}: no archive-recipes block to write the counts in") + return _ARCHIVE_BLOCK.sub(lambda m: f"{m.group(1)}{block}{m.group(3)}", page, count=1) diff --git a/scripts/sitegen/build.py b/scripts/sitegen/build.py index d2269b6f4..506149f61 100644 --- a/scripts/sitegen/build.py +++ b/scripts/sitegen/build.py @@ -44,6 +44,7 @@ from sitegen import ( from sitegen.cards import wanted_link, system_install_choices, tables_out_of_search from sitegen.config import GENERATED_DIRS, PUBLIC_DIRS, REPO_URL, yaml from core.paths import REPO_ROOT +from sitegen.archivecounts import ARCHIVE_RECIPES_PAGE, with_archive_counts from sitegen.crossref import generate_cross_reference from sitegen.dataexports import ( source_commit, @@ -57,10 +58,8 @@ from sitegen.pageregistry import PageRegistry from sitegen.decorate import front_matter, decorate_markdown_pages from sitegen.descriptions import title_noun from sitegen.emulatorcards import ( - ARCHIVE_RECIPES_PAGE, ROMSET_FAQ_PAGE, emulator_setups, - with_archive_counts, with_romset_table, ) from sitegen.emulatorpage import ( diff --git a/scripts/sitegen/emulatorcards.py b/scripts/sitegen/emulatorcards.py index 4aab05cac..9f703fe4b 100644 --- a/scripts/sitegen/emulatorcards.py +++ b/scripts/sitegen/emulatorcards.py @@ -981,30 +981,6 @@ def with_romset_table(page: str, profiles: dict) -> str: return _ROMSET_BLOCK.sub(lambda m: f"{m.group(1)}{table}{m.group(3)}", page, count=1) -ARCHIVE_RECIPES_PAGE = Path(WIKI_SRC_DIR) / "archive-recipes.md" -_ARCHIVE_BLOCK = re.compile( - r"(\n)(.*?)()", re.S) - - -def archive_recipe_lines(counts: dict[str, int]) -> list[str]: - """What an exact and a canonical rebuild are, each with its count.""" - total = f"{counts['total']:,}" - return [ - f"- **Exact**: the same bytes as the archive in the collection." - f" {counts['exact']:,} of {total} archives.", - f"- **Canonical**: the same files inside, packed another way." - f" {counts['canonical']:,} of {total} archives.", - ] - - -def with_archive_counts(page: str, counts: dict[str, int]) -> str: - """The archive recipes page with its counts read from recipes/archives.json.""" - block = "\n".join(archive_recipe_lines(counts)) + "\n" - if not _ARCHIVE_BLOCK.search(page): - raise ValueError(f"{ARCHIVE_RECIPES_PAGE}: no archive-recipes block to write the counts in") - return _ARCHIVE_BLOCK.sub(lambda m: f"{m.group(1)}{block}{m.group(3)}", page, count=1) - - def render_metadata_rows(profile: dict) -> list[str]: """Scalar profile fields, one table row each. diff --git a/tests/sources.py b/tests/sources.py index 32f2e9dc9..1e8e1e6b0 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -84,7 +84,7 @@ COMMANDS = { "sitegen/startwith.py", "sitegen/systempages.py", "sitegen/emulatorcards.py", "sitegen/emulatorpage.py", "sitegen/gaps.py", "sitegen/crossref.py", "sitegen/reference.py", "sitegen/whichpack.py", "sitegen/nav.py", "sitegen/build.py", "sitegen/coverage.py", - "sitegen/extraction.py", "sitegen/releasepacks.py", + "sitegen/extraction.py", "sitegen/releasepacks.py", "sitegen/archivecounts.py", "packs/crossref.py", ), "sitediagrams.py": ("sitegen/diagrams.py",), diff --git a/tests/test_doc_surfaces.py b/tests/test_doc_surfaces.py index 7126febe6..9e32a8ac2 100644 --- a/tests/test_doc_surfaces.py +++ b/tests/test_doc_surfaces.py @@ -663,12 +663,12 @@ class ArchiveRecipesAreCountedFromTheRecipes(unittest.TestCase): self.assertIn(f"({SITE_URL}wiki/archive-recipes/)", line) def test_the_page_counts(self) -> None: - from sitegen import emulatorcards + from sitegen import archivecounts page = _read("wiki/archive-recipes.md") - self.assertEqual(emulatorcards.with_archive_counts(page, self.counts), page, + self.assertEqual(archivecounts.with_archive_counts(page, self.counts), page, "wiki/archive-recipes.md counts are stale: run scripts/generate_site.py") - for line in emulatorcards.archive_recipe_lines(self.counts): + for line in archivecounts.archive_recipe_lines(self.counts): self.assertIn(line, page) def test_the_page_command_is_the_tools(self) -> None: