Coverage for src/romfarmer/planner/lowering/cartridge.py: 94%

33 statements  

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

1"""Cartridge lowering rule — No-Intro ZIP/7z extraction + optional recompression. 

2 

3Stage sequence this replaces (``stages/builder.py::_cartridge_stages``): 

4 [CachePreCheckStage →] ExtractArchiveStage [→ CompressArchiveStage] 

5 

6IR action sequence: 

7 source-copy (INTERMEDIATE) — ingest ZIP into CAS 

8 unzip (INTERMEDIATE) — extract ROM from ZIP 

9 [compress] (TERMINAL) — optional recompression to zip or 7z 

10 

11 If no recompression (passthrough format): 

12 source-copy + unzip, output ROM as TERMINAL 

13""" 

14 

15from __future__ import annotations 

16 

17from pathlib import Path 

18 

19from romfarmer.ir.actions import ( 

20 Action, 

21 ArtifactDecl, 

22 InputRef, 

23 PendingRef, 

24 Retention, 

25 UnitPlan, 

26) 

27from romfarmer.ir.catalog import GameUnit 

28from romfarmer.ir.manifest import BuildManifest 

29from romfarmer.planner.lowering.base import ( 

30 FormatChain, 

31 make_action_id, 

32 probe_tool_version, 

33 source_action, 

34 static_tool_version, 

35 zero_prediction, 

36) 

37 

38 

39class CartridgeLoweringRule: 

40 """Lower a cartridge/ROM game unit to an action sequence.""" 

41 

42 def __init__( 

43 self, 

44 action_cache: object | None = None, 

45 fmt: str = "zip", 

46 ) -> None: 

47 self._cache = action_cache 

48 self._fmt = fmt # "zip" | "7z" | "passthrough" 

49 

50 def lower( 

51 self, 

52 unit: GameUnit, 

53 chain: FormatChain, 

54 manifest: BuildManifest, 

55 ) -> UnitPlan: 

56 disc = unit.discs[0] 

57 src_path = disc.source.path 

58 stem = src_path.stem 

59 step = 0 

60 # The extracted ROM keeps its in-archive name (CATALOG read it from the 

61 # zip central directory); emulators key on that extension inside the 

62 # recompressed archive. 

63 zid = disc.identity.zip_identity 

64 rom_name = Path(zid.member_name).name if zid is not None else f"{stem}.rom" 

65 

66 actions: list[Action] = [] 

67 

68 # ── source-copy ──────────────────────────────────────────────── 

69 src_act = source_action(unit, step, src_path, src_path.name, "zip") 

70 actions.append(src_act) 

71 step += 1 

72 src_out_ref: InputRef = PendingRef(src_act.action_id, 0) 

73 

74 # ── unzip ────────────────────────────────────────────────────── 

75 unzip_act = Action( 

76 action_id=make_action_id(str(unit.unit_id), step, "unzip"), 

77 tool="unzip", 

78 tool_version=static_tool_version("unzip"), 

79 params={"format": "cartridge"}, 

80 inputs=(src_out_ref,), 

81 outputs=( 

82 ArtifactDecl( 

83 logical_name=rom_name, 

84 kind="rom", 

85 retention=Retention.INTERMEDIATE, 

86 ), 

87 ), 

88 ) 

89 actions.append(unzip_act) 

90 step += 1 

91 extracted_ref: InputRef = PendingRef(unzip_act.action_id, 0) 

92 

93 fmt = chain[0] if chain else self._fmt 

94 if fmt in ("zip", "7z"): 

95 # ── recompress ───────────────────────────────────────────── 

96 compress_act = Action( 

97 action_id=make_action_id(str(unit.unit_id), step, f"compress-{fmt}"), 

98 tool=f"compress-{fmt}", 

99 tool_version=probe_tool_version("7z"), 

100 params={"format": fmt}, 

101 inputs=(extracted_ref,), 

102 outputs=( 

103 ArtifactDecl( 

104 logical_name=f"{stem}.{fmt}", 

105 kind=fmt, 

106 retention=Retention.TERMINAL, 

107 ), 

108 ), 

109 ) 

110 actions.append(compress_act) 

111 else: 

112 # passthrough — just mark extracted ROM as terminal 

113 pt_act = Action( 

114 action_id=make_action_id(str(unit.unit_id), step, "passthrough"), 

115 tool="passthrough", 

116 tool_version="1", 

117 params={}, 

118 inputs=(extracted_ref,), 

119 outputs=( 

120 ArtifactDecl( 

121 logical_name=f"{stem}.rom", 

122 kind="rom", 

123 retention=Retention.TERMINAL, 

124 ), 

125 ), 

126 ) 

127 actions.append(pt_act) 

128 

129 return UnitPlan( 

130 unit=unit, 

131 actions=tuple(actions), 

132 predicted_output_bytes=unit.source_size, 

133 prediction=zero_prediction(), 

134 )