diff --git a/craft_parts/parts.py b/craft_parts/parts.py index c54bf366..fbb6e2b6 100644 --- a/craft_parts/parts.py +++ b/craft_parts/parts.py @@ -105,9 +105,6 @@ def validate_root(cls, values: dict[str, Any]) -> dict[str, Any]: # This check is only relevant in deb systems. return values - def is_slice(name: str) -> bool: - return "_" in name - # Detect a mixture of .deb packages and chisel slices. stage_packages = values.get("stage-packages", []) has_slices = any(name for name in stage_packages if is_slice(name)) @@ -175,6 +172,16 @@ def has_overlay(self) -> bool: or self.overlay_files != ["*"] ) + @property + def has_slices(self) -> bool: + """Whether the part has slices in its stage-packages. + + :param data: The part data to query. + """ + if not self.stage_packages: + return False + return any(is_slice(name) for name in self.stage_packages) + # pylint: disable=too-many-public-methods class Part: @@ -658,6 +665,14 @@ def part_has_overlay(data: dict[str, Any]) -> bool: return spec.has_overlay +def is_slice(name: str) -> bool: + """Whether the stage-package is a slice or a package. + + :param name: name of the package. + """ + return "_" in name + + def _get_part_spec(data: dict[str, Any]) -> PartSpec: if not isinstance(data, dict): raise TypeError("value must be a dictionary") diff --git a/tests/unit/test_parts.py b/tests/unit/test_parts.py index 046733fc..35b453fb 100644 --- a/tests/unit/test_parts.py +++ b/tests/unit/test_parts.py @@ -335,6 +335,18 @@ def test_part_has_overlay(self, partitions): p = Part("foo", {}, partitions=partitions) assert p.has_overlay is False + @pytest.mark.parametrize( + ("tc_spec", "tc_result"), + [ + ({}, False), + ({"stage-packages": ["libc6_libs"]}, True), + ({"stage-packages": ["python3"]}, False), + ], + ) + def test_part_has_slices(self, partitions, tc_spec, tc_result): + p = Part("foo", tc_spec, partitions=partitions) + assert p.spec.has_slices == tc_result + class TestPartOrdering: """Test part ordering.