Skip to content

Commit

Permalink
fix: refactor poetry export and pip install commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Aug 23, 2024
1 parent 13cd175 commit 8e34c56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
51 changes: 38 additions & 13 deletions craft_parts/plugins/poetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

"""The poetry plugin."""

import pathlib
import shlex
import shutil
import subprocess
from typing import Literal

import pydantic
from overrides import override

from craft_parts import errors
from craft_parts.plugins import validator

from .base import BasePythonPlugin
Expand Down Expand Up @@ -93,30 +92,56 @@ def get_build_packages(self) -> set[str]:
build_packages |= {"python3-poetry"}
return build_packages

def _get_pip_command(self) -> str:
"""Get the pip command for installing the package and its dependencies."""
requirements_path = self._part_info.part_build_dir / "requirements.txt"
return f"{self._get_pip()} install --requirement={requirements_path} ."
def _get_poetry_export_commands(self, requirements_path: pathlib.Path) -> list[str]:
"""Get the commands for exporting from poetry.
@override
def _get_package_install_commands(self) -> list[str]:
"""Return a list of commands to run during the build step."""
requirements_path = self._part_info.part_build_dir / "requirements.txt"
Application-specific classes may override this if they need to export from
poetry differently.
:param requirements_path: The path of the requirements.txt file to write to.
:returns: A list of strings forming the export script.
"""
export_command = [
"poetry",
"export",
"--format=requirements.txt",
f"--output={requirements_path}",
"--with-credentials",
"--without-hashes",
]
if self._options.poetry_with:
export_command.append(
f"--with={','.join(sorted(self._options.poetry_with))}",
)

return [shlex.join(export_command)]

def _get_pip_install_commands(self, requirements_path: pathlib.Path) -> list[str]:
"""Get the commands for installing with pip.
Application-specific classes my override this if they need to install
differently.
:param requirements_path: The path of the requirements.txt file to write to.
:returns: A list of strings forming the install script.
"""
pip = self._get_pip()
return [
# These steps need to be separate because poetry export defaults to including
# hashes, which don't work with installing from a directory.
f"{pip} install --requirement={requirements_path}",
# All dependencies should be installed through the requirements file made by
# poetry.
f"{pip} install --no-deps .",
# Check that the virtualenv is consistent.
f"{pip} check",
]

@override
def _get_package_install_commands(self) -> list[str]:
"""Return a list of commands to run during the build step."""
requirements_path = self._part_info.part_build_dir / "requirements.txt"

return [
shlex.join(export_command),
self._get_pip_command(),
*self._get_poetry_export_commands(requirements_path),
*self._get_pip_install_commands(requirements_path),
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import distro


def main() -> int:
# Check that we installed the correct version of `distro`.
assert distro.__version__ == "1.8.0"
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/plugins/test_poetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ def test_get_build_commands(new_dir, optional_groups, export_addendum):
f'PARTS_PYTHON_VENV_INTERP_PATH="{new_dir}/parts/p1/install/bin/${{PARTS_PYTHON_INTERPRETER}}"',
f"poetry export --format=requirements.txt --output={new_dir}/parts/p1/build/requirements.txt --with-credentials"
+ export_addendum,
f"{new_dir}/parts/p1/install/bin/pip install --requirement={new_dir}/parts/p1/build/requirements.txt .",
f"{new_dir}/parts/p1/install/bin/pip install --requirement={new_dir}/parts/p1/build/requirements.txt",
f"{new_dir}/parts/p1/install/bin/pip install --no-deps .",
f"{new_dir}/parts/p1/install/bin/pip check",
*get_build_commands(new_dir),
]

Expand Down

0 comments on commit 8e34c56

Please sign in to comment.