diff --git a/scripts/profiling/bump.py b/scripts/profiling/bump.py index d42d1f485..ead899ad1 100644 --- a/scripts/profiling/bump.py +++ b/scripts/profiling/bump.py @@ -17,7 +17,7 @@ from model.profiles import load_emulator_profiles from model.registry import list_registered_platforms from profiling import version_basis from profiling.anchoring import REVIEW_STATUSES -from profiling.rebase import pending_recale +from profiling.rebase import pending_recale, pin_blockers from profiling.reports import ProfileReport, SkipKind from profiling.yamledit import apply_edit, insert_after_line, replace_field_line, set_version_fields from resolve.cores import resolve_platform_cores @@ -127,6 +127,9 @@ def bump_commit( return Bump(False) text = path.read_text(encoding="utf-8") if pending_recale(report, accept_changed, text): + blockers = pin_blockers(report, accept_changed) + if blockers: + return Bump(False, f"pin not advanced, cannot move {'; '.join(blockers)}") return Bump(False) document = yaml.safe_load(text) if document.get("built_by") and not document.get("pin_follows"): diff --git a/scripts/profiling/rebase.py b/scripts/profiling/rebase.py index d1dd0efdc..d99aaf96c 100644 --- a/scripts/profiling/rebase.py +++ b/scripts/profiling/rebase.py @@ -2,7 +2,7 @@ from __future__ import annotations -import sys +from dataclasses import dataclass, field from pathlib import Path import yaml @@ -205,16 +205,28 @@ def _recaled_ref(entry: EntryReport, statuses: tuple[str, ...]) -> str | None: return ", ".join(rendered) if touched else None +@dataclass(frozen=True) +class Recale: + """The refs a recale wrote, and each citation that kept it from writing.""" + + applied: list[str] = field(default_factory=list) + refused: list[str] = field(default_factory=list) + + def _recale_statuses( - path: Path, report: ProfileReport, accept_changed: bool -) -> tuple[str, ...] | None: - """The statuses a recale moves, None when the profile must not move.""" + report: ProfileReport, accept_changed: bool +) -> tuple[tuple[str, ...] | None, list[str]]: + """The statuses a recale moves, None when the profile must not move. + + The list names what refuses the move. It stays empty when nothing is + refused but nothing may move either: a pinned tag, a ref to review. + """ if report.pinned_tag: - return None + return None, [] statuses = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) blocking = [s for s in REVIEW_STATUSES if s not in statuses] if any((report.counts or {}).get(s) for s in blocking): - return None + return None, [] blockers = pin_blockers(report, accept_changed) if blockers: # Something here will not move: an annotated ref, one under a mode @@ -222,14 +234,13 @@ def _recale_statuses( # bump_commit will refuse while it stands, and recaling the rest # would leave the profile describing two revisions at once. All or # nothing means the pin too, not only the refs. - _refuse_recale(path, blockers) - return None - return statuses + return None, blockers + return statuses, [] def rebase_refs( path: Path, report: ProfileReport, accept_changed: bool = False -) -> list[str]: +) -> Recale: """Recale the line ranges of parts whose content is unchanged. All or nothing per profile. `source_commit` names the revision the refs @@ -239,9 +250,9 @@ def rebase_refs( that only make sense at HEAD. Prose citations move with everything else: only their located tokens are rewritten, the sentence stays. """ - statuses = _recale_statuses(path, report, accept_changed) + statuses, blockers = _recale_statuses(report, accept_changed) if statuses is None: - return [] + return Recale(refused=blockers) text = path.read_text(encoding="utf-8") document = yaml.safe_load(text) citations = collect_citations(document, text) @@ -293,18 +304,11 @@ def rebase_refs( if refused: # A citation left on the pinned lines holds the pin, so writing the # others would leave the profile describing two revisions. - _refuse_recale(path, refused) - return [] + return Recale(refused=refused) applied.extend(prose_applied) if applied: apply_edit(path, text, document) - return applied - - -def _refuse_recale(path: Path, reasons: list[str]) -> None: - """Name each citation that keeps a recale from being written.""" - for reason in reasons: - print(f"{path.stem}: not recaled, cannot move {reason}", file=sys.stderr) + return Recale(applied=applied) def _pending_refs( diff --git a/scripts/profiling/run.py b/scripts/profiling/run.py index 1f15fdc0b..adeff6061 100644 --- a/scripts/profiling/run.py +++ b/scripts/profiling/run.py @@ -15,7 +15,7 @@ from core import upstream from profiling.bump import bump_commit, pins_changed from profiling.detection import detect_upstream_changes, print_extras from profiling.realign import realign_prose -from profiling.rebase import attribute_refs, rebase_refs +from profiling.rebase import Recale, attribute_refs, rebase_refs from profiling.reports import ( ProfileReport, SkipKind, @@ -174,9 +174,12 @@ def _apply_writes( target = Path(scratch) / path.name target.write_bytes(path.read_bytes()) recale, bumped = ("would recale ", "would set ") if args.dry_run else ("", "") - applied = [] + outcome = Recale() if args.rebase_refs and not report.skipped: - applied = rebase_refs(target, report, args.accept_changed) + outcome = rebase_refs(target, report, args.accept_changed) + for reason in outcome.refused: + print(f"{name}: not recaled, cannot move {reason}", file=sys.stderr) + applied = outcome.applied bump = bump_commit(target, report, args.accept_changed, args.cache_dir, args.offline) moved = bump.moved @@ -190,7 +193,8 @@ def _apply_writes( file=sys.stderr, ) return - if bump.refusal: + if bump.refusal and not outcome.refused: + # A refused recale has named the citations that hold the pin. print(f"{name}: {bump.refusal}", file=sys.stderr) messages = [f"{name}: {recale}{line}" for line in applied] if moved: diff --git a/tests/test_profile_sync.py b/tests/test_profile_sync.py index 57a5eb43a..2dfa9c230 100644 --- a/tests/test_profile_sync.py +++ b/tests/test_profile_sync.py @@ -72,7 +72,7 @@ from profiling.detection import ( watch_hashes, ) from profiling.triage import drift_score -from profiling.rebase import rebase_refs +from profiling.rebase import Recale, rebase_refs from profiling.repos import select_repo from model.sourceref import RefPart, parse_source_ref, split_source_ref from core import upstream @@ -2554,7 +2554,7 @@ class TestRebaseRefs(unittest.TestCase): applied = rebase_refs( self.path, self._report([EntryReport("a.bin", "a.c:10-12", "SHIFTED", [part])]), - ) + ).applied self.assertEqual(applied, ["a.c:10-12 -> a.c:20-22"]) self.assertIn('source_ref: "a.c:20-22"', self.path.read_text()) @@ -2572,7 +2572,7 @@ class TestRebaseRefs(unittest.TestCase): rebase_refs( self.path, self._report([EntryReport("b.bin", "b.c:5", "CHANGED", [part])]), - ), + ).applied, [], ) self.assertIn('source_ref: "b.c:5"', self.path.read_text()) @@ -2583,7 +2583,7 @@ class TestRebaseRefs(unittest.TestCase): rebase_refs( self.path, self._report([EntryReport("b.bin", "b.c:5", "AMBIGUOUS", [part])]), - ), + ).applied, [], ) @@ -2596,8 +2596,8 @@ class TestRebaseRefs(unittest.TestCase): ) report.counts = {"RENAMED": 1} report.pinned_tag = "v1.6.0" - self.assertEqual(rebase_refs(self.path, report), []) - self.assertEqual(rebase_refs(self.path, report, accept_changed=True), []) + self.assertEqual(rebase_refs(self.path, report), Recale()) + self.assertEqual(rebase_refs(self.path, report, accept_changed=True), Recale()) self.assertIn('source_ref: "a.c:10-12"', self.path.read_text()) def test_nothing_is_rebased_while_a_ref_needs_review(self): @@ -2606,7 +2606,7 @@ class TestRebaseRefs(unittest.TestCase): [EntryReport("a.bin", "a.c:10-12", "SHIFTED", [shifted])] ) report.counts = {"SHIFTED": 1, "CHANGED": 1} - self.assertEqual(rebase_refs(self.path, report), []) + self.assertEqual(rebase_refs(self.path, report), Recale()) self.assertIn('source_ref: "a.c:10-12"', self.path.read_text()) def test_accept_changed_recales_a_reviewed_diff(self): @@ -2615,8 +2615,8 @@ class TestRebaseRefs(unittest.TestCase): [EntryReport("a.bin", "a.c:10-12", "CHANGED", [part])] ) report.counts = {"CHANGED": 1} - self.assertEqual(rebase_refs(self.path, report), []) - applied = rebase_refs(self.path, report, accept_changed=True) + self.assertEqual(rebase_refs(self.path, report), Recale()) + applied = rebase_refs(self.path, report, accept_changed=True).applied self.assertEqual(applied, ["a.c:10-12 -> a.c:30-32"]) def test_accept_changed_does_not_cover_gone(self): @@ -2625,7 +2625,7 @@ class TestRebaseRefs(unittest.TestCase): [EntryReport("a.bin", "a.c:10-12", "CHANGED", [part])] ) report.counts = {"CHANGED": 1, "GONE": 1} - self.assertEqual(rebase_refs(self.path, report, accept_changed=True), []) + self.assertEqual(rebase_refs(self.path, report, accept_changed=True), Recale()) def test_accept_changed_is_refused_on_every_profile(self): args = profile_sync.build_parser().parse_args(["--all", "--accept-changed"]) @@ -2650,7 +2650,7 @@ class TestRebaseRefs(unittest.TestCase): ) ] ), - ) + ).applied self.assertEqual(len(applied), 1) text = self.path.read_text() self.assertIn('source_ref: "a.c:20-22 (loads the kernel)"', text) @@ -2666,7 +2666,7 @@ class TestRebaseRefs(unittest.TestCase): self._report( [EntryReport("a.bin [libretro]", "a.c:10-12", "SHIFTED", [part])] ), - ) + ).applied self.assertEqual(applied, []) def test_shifted_part_beside_changed_part_is_rebased(self): @@ -3180,7 +3180,7 @@ class TestDeliberatelyFrozenPin(TestBuildReport): report = build_report("test", self._frozen(), self.dir) path = Path(self.dir) / "p.yml" path.write_text(SAMPLE, encoding="utf-8") - self.assertEqual(rebase_refs(path, report), []) + self.assertEqual(rebase_refs(path, report).applied, []) self.assertFalse(bump_commit(path, report).moved) def test_the_builds_platforms_hold_say_so(self): @@ -3354,8 +3354,13 @@ class TestRebaseWaitsForThePin(unittest.TestCase): {"SHIFTED": 2}, ) self.assertTrue(rebase.pending_recale(report)) - self.assertEqual(rebase_refs(self.path, report), []) + blocker = "b.bin: b.c:5, (just a note) (annotated, a rewrite would drop the annotation)" + self.assertEqual(rebase_refs(self.path, report), Recale(refused=[blocker])) self.assertIn('source_ref: "a.c:10-12"', self.path.read_text()) + # --bump-commit alone names the same ref instead of staying silent. + bump = bump_commit(self.path, report) + self.assertFalse(bump.moved) + self.assertEqual(bump.refusal, f"pin not advanced, cannot move {blocker}") def test_asking_for_both_writes_nothing_when_the_pin_cannot_follow(self): """The pass is atomic: refs and pin move together or not at all.""" @@ -3385,7 +3390,7 @@ class TestRebaseWaitsForThePin(unittest.TestCase): {"SHIFTED": 1}, ) self.assertFalse(rebase.pending_recale(report)) - self.assertEqual(rebase_refs(self.path, report), ["a.c:10-12 -> a.c:30-32"]) + self.assertEqual(rebase_refs(self.path, report).applied, ["a.c:10-12 -> a.c:30-32"]) def test_chained_shifts_are_judged_at_their_own_entry(self): # tr1x: cut2 508->531, cut3 531->552, cut4 552->575. Once written, @@ -3408,7 +3413,7 @@ class TestRebaseWaitsForThePin(unittest.TestCase): for n, old, new in ((2, 508, 531), (3, 531, 552), (4, 552, 575)) ] report = self._report(entries, {"SHIFTED": 3}) - self.assertEqual(len(rebase_refs(self.path, report)), 3) + self.assertEqual(len(rebase_refs(self.path, report).applied), 3) written = self.path.read_text() self.assertIn('source_ref: "g.json5:575"', written) self.assertEqual( @@ -3608,7 +3613,7 @@ class TestBumpCommit(unittest.TestCase): ) self.assertFalse(bump_commit(self.path, report).moved) self.assertNotIn("newhead", self.path.read_text()) - self.assertEqual(rebase_refs(self.path, report), ["a.c:10-12 -> a.c:30-32"]) + self.assertEqual(rebase_refs(self.path, report).applied, ["a.c:10-12 -> a.c:30-32"]) self.assertTrue(bump_commit(self.path, report).moved) def test_an_upstream_on_its_release_tag_stays_there(self): @@ -4469,7 +4474,7 @@ class TestRebaseProse(unittest.TestCase): part = PartResult(RefPart("a.c", 10, 10, "10"), "SHIFTED", None, 14, 14, []) applied = rebase_refs( self.path, self._report([self._prose_entry("a.c:10", [part])]) - ) + ).applied self.assertEqual(applied, ["notes: a.c:10 -> a.c:14"]) text = self.path.read_text() self.assertIn("system directory (a.c:14) and reads", text) @@ -4484,7 +4489,7 @@ class TestRebaseProse(unittest.TestCase): ] applied = rebase_refs( self.path, self._report([self._prose_entry("b.c:2,8-9", parts)]) - ) + ).applied self.assertEqual(applied, ["notes: b.c:2,8-9 -> b.c:5,11-12"]) self.assertIn("both ranges (b.c:5,11-12) on boot", self.path.read_text()) @@ -4497,7 +4502,7 @@ class TestRebaseProse(unittest.TestCase): self._prose_entry("a.c:10", [prose]), EntryReport("a.bin", "a.c:10-12", "SHIFTED", [structured]), ]), - ) + ).applied self.assertEqual(len(applied), 2) text = self.path.read_text() self.assertIn('source_ref: "a.c:14-16"', text) @@ -4508,7 +4513,7 @@ class TestRebaseProse(unittest.TestCase): report = self._report( [self._prose_entry("a.c:10", [part])], counts={"GONE": 1} ) - self.assertEqual(rebase_refs(self.path, report), []) + self.assertEqual(rebase_refs(self.path, report).applied, []) self.assertIn("(a.c:10)", self.path.read_text()) def test_ambiguous_sentence_location_is_left_alone(self): @@ -4516,7 +4521,7 @@ class TestRebaseProse(unittest.TestCase): part = PartResult(RefPart("q.c", 7, 7, "7"), "SHIFTED", None, 9, 9, []) applied = rebase_refs( self.path, self._report([self._prose_entry("q.c:7", [part], "twice")]) - ) + ).applied self.assertEqual(applied, []) self.assertEqual(self.path.read_text(), FOLDED_TWICE) @@ -4536,7 +4541,7 @@ class TestRebaseProse(unittest.TestCase): self._prose_entry("a.c:10", [part], "quirks"), ]) self.assertEqual( - rebase_refs(self.path, report), + rebase_refs(self.path, report).applied, ["notes: a.c:10 -> a.c:14", "quirks: a.c:10 -> a.c:14"], ) written = self.path.read_text() @@ -4554,13 +4559,9 @@ class TestRebaseProse(unittest.TestCase): self._prose_entry("q.c:7", [prose], "twice"), EntryReport("a.bin", "a.c:10-12", "SHIFTED", [structured]), ]) - err = io.StringIO() - with contextlib.redirect_stderr(err): - self.assertEqual(rebase_refs(self.path, report), []) - self.assertIn( - "p: not recaled, cannot move twice: q.c:7 (0 lines carry its sentence)", - err.getvalue(), - ) + recale = rebase_refs(self.path, report) + self.assertEqual(recale.applied, []) + self.assertIn("twice: q.c:7 (0 lines carry its sentence)", recale.refused) self.assertEqual(self.path.read_text(), FOLDED_TWICE) def test_folded_scalar_moves_through_its_run_token(self): @@ -4574,7 +4575,7 @@ class TestRebaseProse(unittest.TestCase): applied = rebase_refs( self.path, self._report([self._prose_entry("q.c:7", [part], "quirk_note")]), - ) + ).applied self.assertEqual(applied, ["quirk_note: q.c:7 -> q.c:9"]) content = self.path.read_text() self.assertIn("(q.c:9). Checked", content) @@ -4599,7 +4600,7 @@ class TestRebaseProse(unittest.TestCase): applied = rebase_refs( self.path, self._report([self._prose_entry("q.c:10-12, 30-40", parts, "quirk_note")]), - ) + ).applied self.assertEqual( applied, ["quirk_note: q.c:10-12, 30-40 -> src/q.c:20-22, 50-60"] ) @@ -4615,7 +4616,7 @@ class TestRebaseProse(unittest.TestCase): ] applied = rebase_refs( self.path, self._report([self._prose_entry("b.c:2,8-9", parts)]) - ) + ).applied self.assertEqual(applied, []) def test_renamed_prose_path_is_rewritten(self): @@ -4624,7 +4625,7 @@ class TestRebaseProse(unittest.TestCase): ) applied = rebase_refs( self.path, self._report([self._prose_entry("a.c:10", [part])]) - ) + ).applied self.assertEqual(applied, ["notes: a.c:10 -> src/a.c:14"]) self.assertIn("(src/a.c:14)", self.path.read_text()) @@ -4680,7 +4681,7 @@ class TestPendingProse(unittest.TestCase): report.entries[0] = EntryReport( "twice", "q.c:7", "SHIFTED", [part], "prose", "twice" ) - self.assertEqual(rebase_refs(self.path, report), []) + self.assertEqual(rebase_refs(self.path, report).applied, []) self.assertFalse(bump_commit(self.path, report).moved) @@ -4745,7 +4746,7 @@ class TestWrappedProse(unittest.TestCase): "SHIFTED", None, 2438, 2442, []), ] report = self._report("libretro.cpp:2372-2373, 2429-2433", parts) - self.assertEqual(len(rebase_refs(self.path, report)), 1) + self.assertEqual(len(rebase_refs(self.path, report).applied), 1) text = self.path.read_text() self.assertIn("(libretro.cpp:\n 2381-2382, 2438-2442) and by", text) self.assertTrue(bump_commit(self.path, report).moved) @@ -4763,7 +4764,7 @@ class TestWrappedProse(unittest.TestCase): report = self._report("src/content.rs:111-199", [part]) report.counts = {"CHANGED": 1} self.assertEqual( - rebase_refs(self.path, report, accept_changed=True), + rebase_refs(self.path, report, accept_changed=True).applied, ["adam: src/content.rs:111-199 -> src/content.rs:111-204"], ) self.assertIn("(src/content.rs\n :111-204). No", self.path.read_text()) @@ -4836,7 +4837,7 @@ class TestCommentCitations(unittest.TestCase): "SHIFTED", None, 3268, 3507, []) report = self._report(0, part) self.assertEqual( - rebase_refs(self.path, report), + rebase_refs(self.path, report).applied, ["comment line 10: src/cpu.c:3212-3451 -> src/cpu.c:3268-3507"], ) self.assertEqual( @@ -4851,7 +4852,7 @@ class TestCommentCitations(unittest.TestCase): part = PartResult(RefPart("z.c", 9, 9, "9"), "RENAMED", "src/z.c", 9, 9, []) report = self._report(1, part) self.assertEqual( - rebase_refs(self.path, report), ["comment line 16: z.c:9 -> src/z.c:9"] + rebase_refs(self.path, report).applied, ["comment line 16: z.c:9 -> src/z.c:9"] ) self.assertIn('"a.c:10-12" # see src/z.c:9', self.path.read_text()) self.assertTrue(bump_commit(self.path, report).moved)