diff --git a/scripts/profile_sync.py b/scripts/profile_sync.py index a6076b46c..dc4d3303e 100644 --- a/scripts/profile_sync.py +++ b/scripts/profile_sync.py @@ -647,23 +647,23 @@ def _refresh_source_paths(args: argparse.Namespace) -> None: return from model.profiles import emulator_profiles_cache from sitegen import sources - from core.artifacts import write_if_changed + from core.artifacts import ArtifactLockBusy # The run loaded the profiles before writing them, and the loader caches # by directory: read again, the record described the pins just replaced # (linapple, then ten profiles in one advance). emulator_profiles_cache.clear() profiles = load_emulator_profiles(args.emulators_dir, skip_aliases=False) - held, unread = sources.build_source_paths( - profiles, [sources.TREE_CACHE], fetch=True - ) - held = sources.keep_unread( - held, unread, sources.read_record(sources.SOURCE_PATHS) - ) - sources.SOURCE_PATHS.parent.mkdir(parents=True, exist_ok=True) - write_if_changed( - str(sources.SOURCE_PATHS), json.dumps(held, indent=1, sort_keys=True) + "\n" - ) + try: + held, unread = sources.refresh_record(profiles, [sources.TREE_CACHE], fetch=True) + except ArtifactLockBusy as exc: + # The profiles are written: only the record waits for the other run. + print( + f"source paths not refreshed ({exc}): run python scripts/sitesources.py " + "--fetch once it finishes", + file=sys.stderr, + ) + return print( f"source paths: {sum(map(len, held.values()))} in {len(held)} trees, " f"{len(unread)} trees not cached: {sources.SOURCE_PATHS}", diff --git a/scripts/sitegen/sources.py b/scripts/sitegen/sources.py index 1808b3771..b753d95f7 100644 --- a/scripts/sitegen/sources.py +++ b/scripts/sitegen/sources.py @@ -12,6 +12,7 @@ from dataclasses import dataclass from pathlib import Path from core import upstream +from core.artifacts import repository_output_lock, write_if_changed from core.paths import REPO_ROOT from model.sourceref import ( HEX_ADDRESS_RE, @@ -370,6 +371,19 @@ def read_record(path: Path) -> dict[str, list[str]]: return {} +def refresh_record( + profiles: dict, cache_dirs: list[Path], fetch: bool, output: Path = SOURCE_PATHS, +) -> tuple[dict[str, list[str]], list[str]]: + """Rebuild and write the record of cited source paths, under the + repository hold when *output* is one of its outputs (website/).""" + with repository_output_lock(str(output)): + held, unread = build_source_paths(profiles, cache_dirs, fetch) + held = keep_unread(held, unread, read_record(output)) + output.parent.mkdir(parents=True, exist_ok=True) + write_if_changed(str(output), json.dumps(held, indent=1, sort_keys=True) + "\n") + return held, unread + + def _commit_url(repo: upstream.Repo, pin: str) -> str: """Forge-specific browser URL for one commit.""" base = f"https://{repo.host}/{repo.owner}/{repo.name}" diff --git a/scripts/sitesources.py b/scripts/sitesources.py index cc350e9c7..54a65bd7b 100644 --- a/scripts/sitesources.py +++ b/scripts/sitesources.py @@ -6,11 +6,10 @@ was written against, so a permalink is pinned, never guessed.""" from __future__ import annotations import argparse -import json import sys from pathlib import Path -from sitegen.sources import SOURCE_PATHS, TREE_CACHE, build_source_paths, keep_unread, read_record +from sitegen.sources import SOURCE_PATHS, TREE_CACHE, refresh_record def main() -> int: @@ -40,14 +39,9 @@ def main() -> int: print(f"Error: no tree cache at {', '.join(map(str, cache_dirs))}", file=sys.stderr) return 1 from model.profiles import load_emulator_profiles - from core.artifacts import repository_output_lock, write_if_changed profiles = load_emulator_profiles(args.emulators_dir, skip_aliases=False) - with repository_output_lock(str(args.output)): - held, unread = build_source_paths(profiles, cache_dirs, args.fetch) - held = keep_unread(held, unread, read_record(args.output)) - args.output.parent.mkdir(parents=True, exist_ok=True) - write_if_changed(str(args.output), json.dumps(held, indent=1, sort_keys=True) + "\n") + held, unread = refresh_record(profiles, cache_dirs, args.fetch, args.output) print(f"{sum(map(len, held.values()))} paths held in {len(held)} trees, " f"{len(unread)} trees not cached: {args.output}") return 0 diff --git a/tests/test_profile_sync.py b/tests/test_profile_sync.py index 6f7f55e81..aa153612d 100644 --- a/tests/test_profile_sync.py +++ b/tests/test_profile_sync.py @@ -5158,7 +5158,7 @@ class AWriteRefreshesTheSourcePaths(unittest.TestCase): with mock.patch.object(sources, "build_source_paths", return_value=({}, [])) as built, \ mock.patch.dict(os.environ, {"RETROBIOS_OFFLINE": ""}), \ mock.patch.object(profile_sync, "load_emulator_profiles", return_value={}), \ - mock.patch("core.artifacts.write_if_changed") as written, \ + mock.patch.object(sources, "write_if_changed") as written, \ contextlib.redirect_stderr(io.StringIO()): profiling_run.WRITTEN_PROFILES.clear() profile_sync._refresh_source_paths(self._args()) @@ -5189,7 +5189,7 @@ class AWriteRefreshesTheSourcePaths(unittest.TestCase): profile.write_text('emulator: P\nsource_commit: "new"\n', encoding="utf-8") with mock.patch.object(sources, "build_source_paths", return_value=({}, [])) as built, \ mock.patch.dict(os.environ, {"RETROBIOS_OFFLINE": ""}), \ - mock.patch("core.artifacts.write_if_changed"), \ + mock.patch.object(sources, "write_if_changed"), \ contextlib.redirect_stderr(io.StringIO()), \ contextlib.redirect_stdout(io.StringIO()): profiling_run.WRITTEN_PROFILES.add("p") @@ -5197,6 +5197,29 @@ class AWriteRefreshesTheSourcePaths(unittest.TestCase): profile_sync._refresh_source_paths(self._args(emulators_dir=tmp)) self.assertEqual(built.call_args[0][0]["p"]["source_commit"], "new") + def test_the_record_waits_for_a_run_holding_the_repository(self): + """profile_sync rewrote source-paths.json without the repository + hold sitesources takes, in the middle of a pipeline run.""" + from unittest import mock + + from core.artifacts import ArtifactLockBusy + from sitegen import sources + + busy = mock.MagicMock() + busy.__enter__.side_effect = ArtifactLockBusy("another run is writing") + with mock.patch.object(sources, "repository_output_lock", return_value=busy) as lock, \ + mock.patch.object(sources, "build_source_paths") as built, \ + mock.patch.dict(os.environ, {"RETROBIOS_OFFLINE": ""}), \ + mock.patch.object(profile_sync, "load_emulator_profiles", return_value={}), \ + contextlib.redirect_stderr(io.StringIO()) as err: + profiling_run.WRITTEN_PROFILES.add("x") + self.addCleanup(profiling_run.WRITTEN_PROFILES.clear) + profile_sync._refresh_source_paths(self._args()) + lock.assert_called_once_with(str(sources.SOURCE_PATHS)) + self.assertFalse(built.called) + self.assertIn("sitesources.py --fetch", err.getvalue()) + + class AnUnreadTreeKeepsItsRecord(unittest.TestCase): """sitesources --fetch, rate-limited by GitLab, dropped about twenty trees it never read from source-paths.json."""