Skip to content

Commit

Permalink
fix: expose part deps in PartInfo
Browse files Browse the repository at this point in the history
This lets the plugin inspect the part's dependencies when determining the
build-packages.

Fixes #911
  • Loading branch information
tigarmo committed Dec 4, 2024
1 parent 12d08a2 commit 50b75f8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 2.1.3
current_version = 2.1.4
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion craft_parts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""Craft a project from several parts."""

__version__ = "2.1.3"
__version__ = "2.1.4"

from . import plugins
from .actions import Action, ActionProperties, ActionType
Expand Down
7 changes: 7 additions & 0 deletions craft_parts/infos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
import platform
import re
from collections.abc import Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -375,6 +376,7 @@ def __init__(
self._part_install_dir = part.part_install_dir
self._part_state_dir = part.part_state_dir
self._part_cache_dir = part.part_cache_dir
self._part_dependencies = part.dependencies
self.build_attributes = part.spec.build_attributes.copy()

def __getattr__(self, name: str) -> Any: # noqa: ANN401
Expand Down Expand Up @@ -430,6 +432,11 @@ def part_cache_dir(self) -> Path:
"""Return the subdirectory containing this part's cache directory."""
return self._part_cache_dir

@property
def part_dependencies(self) -> Sequence[str]:
"""Return the names of the parts that this part depends on."""
return self._part_dependencies

def set_project_var(
self, name: str, value: str, *, raw_write: bool = False
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion craft_parts/plugins/poetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_build_packages(self) -> set[str]:
build_packages = super().get_build_packages()
if (
not self._system_has_poetry()
and "poetry-deps" not in self._part_info.dependencies
and "poetry-deps" not in self._part_info.part_dependencies
):
build_packages |= {"python3-poetry"}
return build_packages
Expand Down
11 changes: 11 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
Changelog
*********

2.1.4 (2024-12-04)
------------------

Bug fixes:

- Fix a regression where trying to use the poetry plugin without poetry
installed on the system would give an error.

For a complete list of commits, check out the `2.1.4`_ release on GitHub.

2.1.3 (2024-11-20)
------------------

Expand Down Expand Up @@ -603,3 +613,4 @@ Bug fixes:
.. _Poetry: https://python-poetry.org

.. _2.1.3: https://github.com/canonical/craft-parts/releases/tag/2.1.3
.. _2.1.4: https://github.com/canonical/craft-parts/releases/tag/2.1.4
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from setuptools import find_packages, setup

VERSION = "2.1.3"
VERSION = "2.1.4"

with open("README.md") as readme_file:
readme = readme_file.read()
Expand Down
24 changes: 16 additions & 8 deletions tests/unit/plugins/test_poetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,29 @@ def plugin(new_dir):


@pytest.mark.parametrize(
("has_poetry", "part_deps", "expected_packages"),
("has_poetry", "part_deps", "expected_added_poetry"),
[
(False, set(), {"python3-poetry"}),
(False, {"poetry-deps"}, set()),
(True, {"poetry-deps"}, set()),
(True, set(), set()),
(False, set(), True),
(False, {"poetry-deps"}, False),
(True, {"poetry-deps"}, False),
(True, set(), False),
],
)
def test_get_build_packages(
monkeypatch, plugin: PoetryPlugin, has_poetry, part_deps, expected_packages: set
monkeypatch,
plugin: PoetryPlugin,
has_poetry,
part_deps,
expected_added_poetry,
):
monkeypatch.setattr(plugin, "_system_has_poetry", lambda: has_poetry)
plugin._part_info.dependencies = part_deps # type: ignore[attr-defined]
plugin._part_info._part_dependencies = part_deps
assert plugin._part_info.part_dependencies == part_deps

assert plugin.get_build_packages().issuperset(expected_packages)
obtained = plugin.get_build_packages()
added_poetry = "python3-poetry" in obtained

assert added_poetry == expected_added_poetry


def test_get_build_environment(plugin, new_dir):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_infos.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ def test_part_info_get_project_var():
)


def test_part_info_part_dependencies():
info = ProjectInfo(application_name="test", cache_dir=Path())
part = Part("foo", {"after": ["part1", "part2"]})
x = PartInfo(project_info=info, part=part)
assert x.part_dependencies == ["part1", "part2"]


def test_step_info(new_dir):
info = ProjectInfo(
application_name="test",
Expand Down

0 comments on commit 50b75f8

Please sign in to comment.