Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a utility function that determines whether a part has slices #637

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
20 changes: 17 additions & 3 deletions craft_parts/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -175,6 +172,15 @@ 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.
"""
stage_packages = self.stage_packages or []
linostar marked this conversation as resolved.
Show resolved Hide resolved
return any(is_slice(name) for name in stage_packages)


# pylint: disable=too-many-public-methods
class Part:
Expand Down Expand Up @@ -658,6 +664,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")
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading