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

1"""SourceCopyTransform — copy a source file from disk into the scratch dir. 

2 

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. 

5 

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. 

10 

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. 

15 

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""" 

19 

20from __future__ import annotations 

21 

22import shutil 

23from collections.abc import Mapping 

24from pathlib import Path 

25 

26 

27class SourceCopyTransform: 

28 """Copy ``params["path"]`` into the scratch dir. 

29 

30 Satisfies the :class:`~romfarmer.engine.transforms.base.Transform` protocol. 

31 """ 

32 

33 name = "source-copy" 

34 

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] 

49 

50 

51class PassthroughTransform: 

52 """Copy input[0] → output unchanged. 

53 

54 Used by the passthrough lowering rules to relabel an intermediate 

55 artifact as terminal without any actual transformation. 

56 """ 

57 

58 name = "passthrough" 

59 

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]