From f77caa1885bc7576dbaac03d13baff0e1caad5a1 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:03:20 +0100 Subject: [PATCH] fix: report only recorded names as mame clones --- scripts/collection/dedup.py | 29 +++++++++++++++++++++++------ tests/test_dedup.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/scripts/collection/dedup.py b/scripts/collection/dedup.py index a3de25676..a57ddd9fd 100644 --- a/scripts/collection/dedup.py +++ b/scripts/collection/dedup.py @@ -33,8 +33,16 @@ def _remove_copies(canonical: str, true_dupes_to_remove: list[str], dry_run: boo return removed -def _settle_group(paths: list[str], true_dupes_to_remove: list[str], dry_run: bool) -> dict: - """Keep the best copy of one content and remove the others.""" +def _settle_group( + paths: list[str], true_dupes_to_remove: list[str], clone_names: list[str], dry_run: bool +) -> dict: + """Keep the best copy of one content and remove the others. + + Only the names the clone map records are MAME clones. A removed variant + is one of the copies, and another name kept beside the canonical file + is one an emulator looks for: printing either as a MAME clone named a + file that stays, or a clone map entry that does not exist. + """ # Find the best canonical across all paths all_paths = [p for p in paths if p not in true_dupes_to_remove] if not all_paths: @@ -58,8 +66,14 @@ def _settle_group(paths: list[str], true_dupes_to_remove: list[str], dry_run: bo print(f" {dn} (keep: {canonical})") if true_dupes_to_remove: print(f" {action} {len(true_dupes_to_remove)} copies") - if alias_names: - print(f" MAME clones: {alias_names}") + if clone_names: + print(f" MAME clones: {clone_names}") + kept_names = sorted( + {os.path.basename(p) for p in paths if p not in true_dupes_to_remove} + - {canonical_name} + ) + if kept_names: + print(f" Kept under other names: {kept_names}") return { "canonical": canonical, "removed": removed, @@ -99,12 +113,15 @@ def deduplicate(bios_dir: str, dry_run: bool = False) -> dict: if len(paths) <= 1: continue - true_dupes_to_remove = plan_group_removals(sha1, paths, mame_clones) + group_clones: dict[str, dict] = {} + true_dupes_to_remove = plan_group_removals(sha1, paths, group_clones) + mame_clones.update(group_clones) if not true_dupes_to_remove: continue - settled = _settle_group(paths, true_dupes_to_remove, dry_run) + clone_names = [name for entry in group_clones.values() for name in entry["clones"]] + settled = _settle_group(paths, true_dupes_to_remove, clone_names, dry_run) total_removed += len(settled["removed"]) total_saved += settled.pop("saved") results[sha1] = settled diff --git a/tests/test_dedup.py b/tests/test_dedup.py index 5fde4eb7a..9dd99c840 100644 --- a/tests/test_dedup.py +++ b/tests/test_dedup.py @@ -125,6 +125,36 @@ class TrueDuplicates(_Bios): self.run_dedup() self.assertEqual(len(self.existing()), 2) + def report(self) -> str: + out = io.StringIO() + with contextlib.redirect_stdout(out): + dedup.deduplicate(str(self.bios), dry_run=True) + return out.getvalue() + + def test_a_removed_variant_is_not_reported_as_a_mame_clone(self): + # The run printed "MAME clones: ['x.bin.1234']" for a variant copy, + # a name the clone map never records. + self.write("Sony/PS/x.bin") + self.write("Sony/PS/.variants/x.bin.1234") + report = self.report() + self.assertNotIn("MAME clones", report) + self.assertIn("Would remove 1 copies", report) + + def test_another_name_kept_beside_the_canonical_is_named_as_kept(self): + self.write("Nintendo/N64DD/64DD_IPL_US.n64") + self.write("Nintendo/N64DD/x/64DD_IPL_US.n64") + self.write("Nintendo/N64DD/IPL_USA.n64") + report = self.report() + self.assertNotIn("MAME clones", report) + self.assertIn("Kept under other names: ['IPL_USA.n64']", report) + + def test_a_device_clone_is_still_reported_as_one(self): + self.write("Arcade/MAME/a.zip") + self.write("Arcade/MAME/abc.zip") + report = self.report() + self.assertIn("MAME clones: ['abc.zip']", report) + self.assertNotIn("Kept under other names", report) + def test_a_lone_file_is_left_alone(self): self.write("Sony/PS/only.bin") self.assertEqual(self.run_dedup(), {})