Coverage for src/romfarmer/planner/lowering/disc.py: 72%
40 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
1"""Disc lowering rule — Redump CUE/BIN → CHD (or passthrough).
3Stage sequence this replaces (from ``stages/builder.py::_disc_stages``):
4 CachePreCheckStage → ExtractArchiveStage → CompressCHDStage → CreateM3UStage
6IR action sequence:
7 For each disc:
8 source-copy (INTERMEDIATE) — ingest ZIP into CAS
9 unzip (INTERMEDIATE) — extract CUE/BIN from ZIP
10 chdman (TERMINAL) — compress to CHD
12 If multi-disc:
13 m3u-create (TERMINAL) — synthesise M3U playlist
15 If no CHD (passthrough):
16 source-copy + unzip, outputs CUE/BIN as TERMINAL
17"""
19from __future__ import annotations
21from romfarmer.ir.actions import (
22 Action,
23 ActionId,
24 ArtifactDecl,
25 InputRef,
26 PendingRef,
27 Retention,
28 UnitPlan,
29)
30from romfarmer.ir.catalog import GameUnit
31from romfarmer.ir.manifest import BuildManifest
32from romfarmer.planner.lowering.base import (
33 FormatChain,
34 make_action_id,
35 probe_tool_version,
36 source_action,
37 source_ref,
38 static_tool_version,
39 zero_prediction,
40)
43class DiscLoweringRule:
44 """Lower a disc-based game unit to an action sequence."""
46 def __init__(
47 self,
48 action_cache: object | None = None,
49 no_chd: bool = False,
50 ) -> None:
51 self._cache = action_cache
52 self._no_chd = no_chd
54 def lower(
55 self,
56 unit: GameUnit,
57 chain: FormatChain,
58 manifest: BuildManifest,
59 ) -> UnitPlan:
60 actions: list[Action] = []
61 chd_action_ids: list[ActionId] = []
62 step = 0
64 for disc in unit.discs:
65 src_path = disc.source.path
66 zip_name = src_path.name
67 stem = src_path.stem
69 # ── Step A: source-copy ────────────────────────────────────
70 cref, sha256 = source_ref(unit, self._cache) if disc.index == 1 else (None, None)
71 src_act = source_action(unit, step, disc.source.path, zip_name, "zip")
72 # If sha256 is already known, we can skip source-copy and use ContentRef directly
73 # For now always emit source-copy (executor will cache-hit if already ingested)
74 actions.append(src_act)
75 step += 1
76 src_out_ref: InputRef = PendingRef(src_act.action_id, 0)
78 if self._no_chd:
79 # ── unzip + passthrough — CUE+BIN as terminal ───────────────
80 unzip_act = Action(
81 action_id=make_action_id(str(unit.unit_id), step, "unzip"),
82 tool="unzip",
83 tool_version=static_tool_version("unzip"),
84 params={"format": "disc"},
85 inputs=(src_out_ref,),
86 outputs=(
87 ArtifactDecl(
88 logical_name=f"{stem}.cue",
89 kind="cue",
90 retention=Retention.INTERMEDIATE,
91 ),
92 ),
93 )
94 actions.append(unzip_act)
95 step += 1
96 extracted_ref: InputRef = PendingRef(unzip_act.action_id, 0)
98 passthrough_act = Action(
99 action_id=make_action_id(str(unit.unit_id), step, "source-copy-out"),
100 tool="source-copy",
101 tool_version=static_tool_version("source-copy"),
102 params={"path": str(src_path)},
103 inputs=(extracted_ref,),
104 outputs=(
105 ArtifactDecl(
106 logical_name=f"{stem}.cue",
107 kind="cue_bin",
108 retention=Retention.TERMINAL,
109 ),
110 ),
111 )
112 actions.append(passthrough_act)
113 step += 1
114 else:
115 # ── Step B: chdman on the source ZIP ──────────────────────
116 chd_act = Action(
117 action_id=make_action_id(str(unit.unit_id), step, "chdman"),
118 tool="chdman",
119 tool_version=probe_tool_version("chdman"),
120 params={"compression": "dvd"},
121 inputs=(src_out_ref,),
122 outputs=(
123 ArtifactDecl(
124 logical_name=f"{stem}.chd",
125 kind="chd",
126 retention=Retention.TERMINAL,
127 ),
128 ),
129 )
130 actions.append(chd_act)
131 chd_action_ids.append(chd_act.action_id)
132 step += 1
134 # ── M3U for multi-disc CHD sets ────────────────────────────────
135 if unit.is_multi_disc and not self._no_chd and chd_action_ids:
136 m3u_inputs = tuple(PendingRef(aid, 0) for aid in chd_action_ids)
137 m3u_entries = ",".join(f"{d.source.path.stem}.chd" for d in unit.discs)
138 m3u_act = Action(
139 action_id=make_action_id(str(unit.unit_id), step, "m3u-create"),
140 tool="m3u-create",
141 tool_version=static_tool_version("m3u-create"),
142 params={
143 "entries": m3u_entries,
144 "name": f"{unit.canonical_name}.m3u",
145 },
146 inputs=m3u_inputs,
147 outputs=(
148 ArtifactDecl(
149 logical_name=f"{unit.canonical_name}.m3u",
150 kind="m3u",
151 retention=Retention.TERMINAL,
152 ),
153 ),
154 )
155 actions.append(m3u_act)
157 return UnitPlan(
158 unit=unit,
159 actions=tuple(actions),
160 predicted_output_bytes=unit.source_size,
161 prediction=zero_prediction(),
162 )