Coverage for src/romfarmer/engine/transforms/xiso.py: 43%

37 statements  

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

1"""XISO transform — extract-xiso rewrite mode. 

2 

3Extracted from ``romfarmer.stages.convert_xiso.ConvertXISOStage``. 

4Converts a Redump ISO to XISO format (game partition only) using 

5``extract-xiso -r``. The source file is modified in-place; the 

6original is renamed to ``.iso.old`` by extract-xiso and removed here. 

7""" 

8 

9from __future__ import annotations 

10 

11import shutil 

12import subprocess 

13from collections.abc import Mapping 

14from pathlib import Path 

15 

16from .base import Transform, TransformError, pinned_env 

17 

18_DEFAULT_TOOL = Path("tools/bin/extract-xiso") 

19 

20 

21class XisoTransform: 

22 """Rewrite a Redump ISO to XISO format via ``extract-xiso -r``. 

23 

24 Recognised params: 

25 extract_xiso_path: override path to the extract-xiso binary 

26 """ 

27 

28 name = "extract-xiso" 

29 

30 def run( 

31 self, 

32 inputs: list[Path], 

33 params: Mapping[str, str], 

34 scratch: Path, 

35 ) -> list[Path]: 

36 if not inputs: 

37 raise TransformError("XisoTransform: no input files provided") 

38 

39 source = inputs[0] 

40 # extract-xiso -r modifies in place; copy to scratch first so the 

41 # original CAS blob is not touched. 

42 work_iso = scratch / source.name 

43 shutil.copy2(source, work_iso) 

44 

45 tool = self._find_tool(params.get("extract_xiso_path")) 

46 if tool is None: 

47 raise TransformError( 

48 "extract-xiso not found; install it or set extract_xiso_path param" 

49 ) 

50 

51 result = subprocess.run( 

52 [str(tool), "-r", work_iso.name], 

53 capture_output=True, 

54 text=True, 

55 check=False, 

56 cwd=scratch, 

57 timeout=600, 

58 env=pinned_env(), 

59 ) 

60 if result.returncode != 0: 

61 raise TransformError(f"extract-xiso failed for {source.name}: {result.stderr[:500]}") 

62 

63 # extract-xiso renames original to .iso.old; clean up 

64 old = work_iso.with_suffix(".iso.old") 

65 if old.exists(): 

66 old.unlink() 

67 

68 if not work_iso.exists(): 

69 raise TransformError(f"extract-xiso did not produce expected output {work_iso}") 

70 return [work_iso] 

71 

72 @staticmethod 

73 def _find_tool(override: str | None) -> Path | None: 

74 # Absolute: the tool is invoked with cwd=scratch. 

75 if override: 

76 p = Path(override).resolve() 

77 return p if p.exists() else None 

78 if _DEFAULT_TOOL.exists(): 

79 return _DEFAULT_TOOL.resolve() 

80 found = shutil.which("extract-xiso") 

81 return Path(found).resolve() if found else None 

82 

83 

84_: Transform = XisoTransform()