Coverage for src/romfarmer/planner/passes/dat_filter.py: 50%

14 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-05 02:19 -0700

1"""dat_filter pass — keep only units that matched an entry in the platform DAT. 

2 

3The DAT (typically a Retool 1G1R export) *defines* the collection: anything 

4in the source directory that did not match a DAT entry — pirates, unlicensed 

5dumps, regions the DAT excluded, stray files — is dropped here. This is the 

6semantic the legacy ``FilterDATStage`` provided and the compiler initially 

7lost; without it a "1G1R English" build emitted every file in the source. 

8 

9Gated by ``manifest.dat_filter``. When the platform has no DAT (digital 

10stores, arcade sets driven by their own metadata) the manifest leaves it 

11``False`` and this pass is a no-op. 

12""" 

13 

14from __future__ import annotations 

15 

16from typing import TYPE_CHECKING 

17 

18from romfarmer.ir.catalog import Catalog, PassResult, PassTrace, UnitId 

19 

20if TYPE_CHECKING: 

21 from romfarmer.analysis.knowledge import KnowledgeBase 

22 from romfarmer.ir.manifest import BuildManifest 

23 from romfarmer.planner.costmodel import CostModel 

24 

25PASS_NAME = "dat_filter" 

26 

27 

28def run( 

29 catalog: Catalog, 

30 manifest: BuildManifest, 

31 kb: KnowledgeBase, 

32 cost_model: CostModel, 

33) -> PassResult: 

34 """Remove units with no DAT match on any disc.""" 

35 if not manifest.dat_filter: 

36 return PassResult(catalog=catalog, trace=PassTrace(pass_name=PASS_NAME, removed=())) 

37 

38 keep: set[UnitId] = set() 

39 removed: list[tuple[UnitId, str]] = [] 

40 for unit in catalog.units: 

41 if any(d.dat_name is not None for d in unit.discs): 

42 keep.add(unit.unit_id) 

43 else: 

44 removed.append((unit.unit_id, "dat_filter: no DAT entry matched this file")) 

45 

46 return PassResult( 

47 catalog=catalog.keep(keep), 

48 trace=PassTrace(pass_name=PASS_NAME, removed=tuple(removed)), 

49 )