From f379df5e1e51b47255cf5092d62a26dd1db89c41 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:19:29 +0100 Subject: [PATCH] refactor: one list of files the pipeline rewrites diskcleanup kept its own regex of generated paths and missed derivable.json, retired.json and wiki/installer.md, so a worktree holding only a pipeline run's output was kept as uncommitted code. core.generated holds the globs, the cleanup and the nightly's commit rules match with them, and a test ties the regenerate rule to the list. The rule gains install/standalone/*.json and wiki/reading-reports.md, which the pipeline writes and no rule committed. --- automation/jobs.yml | 7 ++--- scripts/core/generated.py | 48 +++++++++++++++++++++++++++++++++ scripts/devtools/diskcleanup.py | 8 ++---- scripts/unattended/worktree.py | 12 ++------- tests/test_autorun.py | 31 ++++++++++++++++----- tests/test_doc_pages.py | 4 +-- 6 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 scripts/core/generated.py diff --git a/automation/jobs.yml b/automation/jobs.yml index b3eb6b01f..47ce23a98 100644 --- a/automation/jobs.yml +++ b/automation/jobs.yml @@ -87,9 +87,10 @@ jobs: body: >- Regenerated by the offline pipeline, database.json and the manifests computed twice with the same result. - paths: [database.json, derivable.json, retired.json, install/*.json, install/targets/*.json, - install/games/*.json, install/games/*/*.json, README.md, CONTRIBUTING.md, - wiki/installer.md, website/src/data/*.json] + paths: [database.json, derivable.json, retired.json, install/*.json, + install/standalone/*.json, install/targets/*.json, install/games/*.json, + install/games/*/*.json, README.md, CONTRIBUTING.md, wiki/installer.md, + wiki/reading-reports.md, website/src/data/*.json] scratch: [dist, tmp/autorun] bios-integrity: diff --git a/scripts/core/generated.py b/scripts/core/generated.py new file mode 100644 index 000000000..61f7722ea --- /dev/null +++ b/scripts/core/generated.py @@ -0,0 +1,48 @@ +"""The tracked paths a pipeline run rewrites, as globs whose `*` stays inside one path segment.""" + +from __future__ import annotations + +import fnmatch +from collections.abc import Iterable + +# What `pipeline.py --offline` writes and the nightly job commits under its +# "regenerate collection records" rule (automation/jobs.yml). +COLLECTION_RECORDS = ( + "database.json", + "derivable.json", + "retired.json", + "install/*.json", + "install/standalone/*.json", + "install/targets/*.json", + "install/games/*.json", + "install/games/*/*.json", + "README.md", + "CONTRIBUTING.md", + "wiki/installer.md", + "wiki/reading-reports.md", + "website/src/data/*.json", +) +# The archive recipes of step 1c, committed by the recipes rule, and the +# reports of `--with-export`. +OTHER_PIPELINE_OUTPUTS = ( + "recipes/archives.json", + "exports/*.json", + "exports/*/*.json", +) +GENERATED = COLLECTION_RECORDS + OTHER_PIPELINE_OUTPUTS + + +def path_matches(pattern: str, path: str) -> bool: + """A glob whose `*` stays inside one path segment.""" + parts, segments = pattern.split("/"), path.split("/") + return len(parts) == len(segments) and all( + fnmatch.fnmatchcase(s, p) for p, s in zip(parts, segments) + ) + + +def matches_any(path: str, patterns: Iterable[str]) -> bool: + return any(path_matches(p, path) for p in patterns) + + +def is_generated(path: str) -> bool: + return matches_any(path, GENERATED) diff --git a/scripts/devtools/diskcleanup.py b/scripts/devtools/diskcleanup.py index 3ed633d78..e2c369d2a 100644 --- a/scripts/devtools/diskcleanup.py +++ b/scripts/devtools/diskcleanup.py @@ -4,19 +4,15 @@ from __future__ import annotations import contextlib import os -import re import shutil import subprocess from collections.abc import Iterable from dataclasses import dataclass from pathlib import Path +from core.generated import is_generated from core.paths import AUTORUN_WORKTREE -GENERATED = re.compile( - r"^(database\.json|install/.*\.json|exports/.*\.json|README\.md|CONTRIBUTING\.md" - r"|website/src/data/[^/]+\.json)$" -) KEPT_TMP = ("acquire",) ALERT_GB = 100 @@ -80,7 +76,7 @@ def real_changes(worktree: Path) -> list[str]: if len(line) < 4 or line[:2].strip() == "D": continue path = line[3:].split(" -> ")[-1].strip('"') - if not GENERATED.match(path): + if not is_generated(path): paths.append(path) return paths diff --git a/scripts/unattended/worktree.py b/scripts/unattended/worktree.py index 2179c8981..aebbeb2c6 100644 --- a/scripts/unattended/worktree.py +++ b/scripts/unattended/worktree.py @@ -4,13 +4,13 @@ may carry, and the push. from __future__ import annotations -import fnmatch import os import shutil import subprocess from collections.abc import Callable, Sequence from pathlib import Path +from core.generated import matches_any from core.gitclone import GitRefused from core.gitclone import main_checkout as gitclone_main_checkout from core.paths import REPO_ROOT @@ -54,16 +54,8 @@ def changed_files(worktree: Path) -> list[str]: return sorted(set(paths)) -def path_matches(pattern: str, path: str) -> bool: - """A glob whose `*` stays inside one path segment.""" - parts, segments = pattern.split("/"), path.split("/") - return len(parts) == len(segments) and all( - fnmatch.fnmatchcase(s, p) for p, s in zip(parts, segments) - ) - - def plan_commit(changed: Sequence[str], patterns: Sequence[str]) -> list[str]: - return sorted(p for p in changed if any(path_matches(g, p) for g in patterns)) + return sorted(p for p in changed if matches_any(p, patterns)) def commit_paths(worktree: Path, files: Sequence[str], rule: CommitRule) -> str: diff --git a/tests/test_autorun.py b/tests/test_autorun.py index 4f05f5d64..60ebd10a9 100644 --- a/tests/test_autorun.py +++ b/tests/test_autorun.py @@ -27,6 +27,7 @@ from unattended import runner from unattended import timers as unattended_timers from unattended import worktree as unattended_worktree from core import childenv +from core import generated from core.hashing import file_digest from core import hostguard from core import statefile @@ -522,9 +523,9 @@ class Origin(Scratch): class Commits(Origin): def test_globs_stay_inside_one_segment(self): - self.assertTrue(unattended_worktree.path_matches("install/*.json", "install/a.json")) - self.assertFalse(unattended_worktree.path_matches("install/*.json", "install/targets/a.json")) - self.assertFalse(unattended_worktree.path_matches("install/*.json", "x/install/a.json")) + self.assertTrue(generated.path_matches("install/*.json", "install/a.json")) + self.assertFalse(generated.path_matches("install/*.json", "install/targets/a.json")) + self.assertFalse(generated.path_matches("install/*.json", "x/install/a.json")) def test_the_dry_run_names_only_matching_files_and_stages_nothing(self): wt = self.worktree() @@ -723,12 +724,28 @@ class Cleanup(Origin): def test_generated_files_are_the_ones_a_run_rewrites(self): """mkdocs.yml stayed in the list after the site moved to Starlight, - while CONTRIBUTING.md and website/src/data/*.json were left out.""" + while CONTRIBUTING.md and website/src/data/*.json were left out, and + derivable.json and wiki/installer.md kept a worktree holding nothing + but a pipeline run's output.""" for path in ("README.md", "CONTRIBUTING.md", "database.json", "install/retroarch.json", - "exports/batocera.json", "website/src/data/source-paths.json"): - self.assertTrue(cd.GENERATED.match(path), path) + "exports/batocera.json", "website/src/data/source-paths.json", + "derivable.json", "retired.json", "wiki/installer.md", + "install/standalone/batocera.json", "install/games/systems/x.json"): + self.assertTrue(generated.is_generated(path), path) for path in ("mkdocs.yml", "website/src/data/x/y.json", "scripts/verify.py"): - self.assertFalse(cd.GENERATED.match(path), path) + self.assertFalse(generated.is_generated(path), path) + + def test_the_nightly_commits_every_collection_record(self): + """The nightly's regenerate rule is the list the cleanup trusts: a + path one names and the other omits keeps a worktree or fails a run.""" + rules = [rule for job in unattended_jobs.load_jobs().values() for rule in job.commits + if rule.subject == "chore: regenerate collection records"] + self.assertEqual(len(rules), 1) + self.assertEqual(rules[0].paths, generated.COLLECTION_RECORDS) + nightly = next(job for job in unattended_jobs.load_jobs().values() + if rules[0] in job.commits) + self.assertTrue(unattended_worktree.plan_commit( + ["recipes/archives.json"], [p for r in nightly.commits for p in r.paths])) def test_worktree_and_tmp_rules(self): old_clean = self._worktree("old-clean", 100) diff --git a/tests/test_doc_pages.py b/tests/test_doc_pages.py index 0222a1a33..3dd4c02c7 100644 --- a/tests/test_doc_pages.py +++ b/tests/test_doc_pages.py @@ -1946,8 +1946,8 @@ class TheInstallerSamplesTravelWithTheManifests(unittest.TestCase): place for the samples the gate's pipeline rewrites.""" def test_the_pages_and_the_nightly_carry_them(self): + from core.generated import matches_any from unattended import jobs - from unattended import worktree guide = (WIKI / "testing-guide.md").read_text(encoding="utf-8") for name in ("wiki/installer.md", "derivable.json", "generate_installer_doc.py"): @@ -1959,7 +1959,7 @@ class TheInstallerSamplesTravelWithTheManifests(unittest.TestCase): patterns = [p for rule in nightly.commits for p in rule.paths] for path in ("wiki/installer.md", "derivable.json", "install/retroarch.json"): with self.subTest(path=path): - self.assertTrue(any(worktree.path_matches(p, path) for p in patterns)) + self.assertTrue(matches_any(path, patterns)) class TheRemovalProofsAreTheExportersOwn(unittest.TestCase):