Coverage for src/romfarmer/engine/transforms/squashfs.py: 41%
32 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"""SquashFS transform — mksquashfs compression.
3Used as the optional second step of the Xbox chain
4(``("xiso", "squashfs")``) for Xbox-on-Linux targets.
5"""
7from __future__ import annotations
9import shutil
10import subprocess
11from collections.abc import Mapping
12from pathlib import Path
14from .base import Transform, TransformError, pinned_env
16# Determinism pins (IMPL_VERSIONS["mksquashfs"] == 2). Changing these requires a bump.
17# Timestamps (mkfs time + every inode's mtime) are pinned by SOURCE_DATE_EPOCH=0
18# in pinned_env() — mksquashfs ≥ 4.6 refuses -mkfs-time/-all-time alongside it.
19# uid/gid, xattrs, and the processor count (block layout) are pinned here.
20_DETERMINISM_FLAGS = (
21 "-all-root",
22 "-no-xattrs",
23 "-processors",
24 "4",
25)
28class SquashFSTransform:
29 """Compress ``inputs[0]`` into a ``.squashfs`` image via mksquashfs.
31 Recognised params:
32 compression: mksquashfs codec (default ``"lz4"``)
33 mksquashfs_path: override path to the mksquashfs binary
34 """
36 name = "mksquashfs"
38 def run(
39 self,
40 inputs: list[Path],
41 params: Mapping[str, str],
42 scratch: Path,
43 ) -> list[Path]:
44 if not inputs:
45 raise TransformError("SquashFSTransform: no input files provided")
47 input_file = inputs[0]
48 tool = self._find_tool(params.get("mksquashfs_path"))
49 if tool is None:
50 raise TransformError(
51 "mksquashfs not found; install squashfs-tools or set mksquashfs_path param"
52 )
54 output_path = scratch / (input_file.stem + ".squashfs")
55 cmd = [
56 str(tool),
57 str(input_file),
58 str(output_path),
59 "-comp",
60 params.get("compression", "lz4"),
61 "-noappend",
62 "-quiet",
63 *_DETERMINISM_FLAGS,
64 ]
65 result = subprocess.run(cmd, capture_output=True, text=True, check=False, env=pinned_env())
66 if result.returncode != 0:
67 raise TransformError(f"mksquashfs failed for {input_file.name}: {result.stderr[:500]}")
68 if not output_path.exists():
69 raise TransformError(f"mksquashfs did not produce expected output {output_path}")
70 return [output_path]
72 @staticmethod
73 def _find_tool(override: str | None) -> Path | None:
74 if override:
75 p = Path(override)
76 return p if p.exists() else None
77 found = shutil.which("mksquashfs")
78 return Path(found) if found else None
81# Satisfy the Transform protocol at type-check time
82_: Transform = SquashFSTransform()