Coverage for src/romfarmer/engine/transforms/source.py: 64%
25 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"""SourceCopyTransform — copy a source file from disk into the scratch dir.
3This is the bridge between filesystem source files and the CAS-aware executor.
4It handles the special ``source-copy`` tool declared by the lowering rules.
6Unlike all other transforms, SourceCopyTransform reads from a filesystem path
7specified in ``params["path"]`` rather than from CAS. Its output is a copy
8of the source file in scratch; the executor then ingests it into CAS and
9records the sha256.
11Caching semantics: the ActionKey for a source-copy action is derived from
12the file path (in params) and tool_version="1" (constant). If the source
13file is immutable (ROM archives are), this is safe. If the file changes,
14the cache must be invalidated manually.
16``passthrough`` tool: a no-op transform that copies input → output unchanged.
17Used when a lowering rule wants to relabel an intermediate as terminal.
18"""
20from __future__ import annotations
22import shutil
23from collections.abc import Mapping
24from pathlib import Path
27class SourceCopyTransform:
28 """Copy ``params["path"]`` into the scratch dir.
30 Satisfies the :class:`~romfarmer.engine.transforms.base.Transform` protocol.
31 """
33 name = "source-copy"
35 def run(
36 self,
37 inputs: list[Path],
38 params: Mapping[str, str],
39 scratch: Path,
40 ) -> list[Path]:
41 """Copy the source file to *scratch* and return the destination path."""
42 src = Path(params["path"])
43 if not src.exists():
44 raise FileNotFoundError(f"SourceCopyTransform: source not found: {src}")
45 dest = scratch / src.name
46 dest.parent.mkdir(parents=True, exist_ok=True)
47 shutil.copy2(src, dest)
48 return [dest]
51class PassthroughTransform:
52 """Copy input[0] → output unchanged.
54 Used by the passthrough lowering rules to relabel an intermediate
55 artifact as terminal without any actual transformation.
56 """
58 name = "passthrough"
60 def run(
61 self,
62 inputs: list[Path],
63 params: Mapping[str, str],
64 scratch: Path,
65 ) -> list[Path]:
66 if not inputs:
67 return []
68 src = inputs[0]
69 dest = scratch / src.name
70 dest.parent.mkdir(parents=True, exist_ok=True)
71 if src != dest:
72 shutil.copy2(src, dest)
73 return [dest]