Skip to content

Commit

Permalink
fix(python,poetry): remove duplicate files in the charm
Browse files Browse the repository at this point in the history
Fixes #1966

The lib64 directory exists as a symbolic link in virtual environments
for historic reasons, but is not necessary.

This prevents the charm from packing it twice (due to Python's inability
to put symlinks into zip files).
  • Loading branch information
lengau committed Oct 17, 2024
1 parent 408650c commit 6997eb3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
11 changes: 7 additions & 4 deletions charmcraft/parts/plugins/_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import pathlib
from pathlib import Path
import textwrap

from craft_parts.plugins import poetry_plugin
from overrides import override
Expand Down Expand Up @@ -91,7 +92,9 @@ def _get_rewrite_shebangs_commands(self) -> list[str]:
@override
def get_build_commands(self) -> list[str]:
"""Get the build commands for the Python plugin."""
if self._options.poetry_keep_bins:
return super().get_build_commands()
venv_bin = self._get_venv_directory() / "bin"
return [*super().get_build_commands(), f"rm -rf {venv_bin}"]
return [
*super().get_build_commands(),
*utils.get_venv_cleanup_commands(
self._get_venv_directory(), keep_bins=self._options.poetry_keep_bins
),
]
10 changes: 6 additions & 4 deletions charmcraft/parts/plugins/_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def _get_rewrite_shebangs_commands(self) -> list[str]:
@override
def get_build_commands(self) -> list[str]:
"""Get the build commands for the Python plugin."""
if self._options.python_keep_bins:
return super().get_build_commands()
venv_bin = self._get_venv_directory() / "bin"
return [*super().get_build_commands(), f"rm -rf {venv_bin}"]
return [
*super().get_build_commands(),
*utils.get_venv_cleanup_commands(
self._get_venv_directory(), keep_bins=self._options.python_keep_bins
),
]
2 changes: 2 additions & 0 deletions charmcraft/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from charmcraft.utils.parts import (
extend_python_build_environment,
get_charm_copy_commands,
get_venv_cleanup_commands,
)
from charmcraft.utils.project import (
find_charm_sources,
Expand Down Expand Up @@ -116,6 +117,7 @@
"humanize_list",
"extend_python_build_environment",
"get_charm_copy_commands",
"get_venv_cleanup_commands",
"find_charm_sources",
"get_charm_name_from_path",
"get_templates_environment",
Expand Down
20 changes: 20 additions & 0 deletions charmcraft/utils/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pathlib
import shlex
from collections.abc import Collection
import textwrap


def extend_python_build_environment(environment: dict[str, str]) -> dict[str, str]:
Expand Down Expand Up @@ -54,3 +55,22 @@ def get_charm_copy_commands(
)

return commands


def get_venv_cleanup_commands(venv_path: pathlib.Path, *, keep_bins: bool) -> list[str]:
"""Get a script do Charmcraft-specific venv cleanup.
:param venv_path: The path to the venv.
:param keep_bins: Whether to keep the bin directory of the venv.
:returns: A shell script to do this, as a string.
"""
venv_bin = venv_path / "bin"
venv_lib64 = venv_path / "lib64"
delete_bins = [] if keep_bins else [f"rm -rf {venv_bin}"]
delete_lib64 = textwrap.dedent(f"""
if [ -L '{venv_lib64}' ]; then
rm -f '{venv_lib64}'
fi
""")

return [*delete_bins, delete_lib64]
1 change: 1 addition & 0 deletions tests/integration/parts/plugins/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ def test_poetry_plugin(
# Check that the stage directory looks correct.
assert (stage_path / "src" / "charm.py").read_text() == "# Charm file"
assert (stage_path / "venv" / "lib").is_dir()
assert not (stage_path / "venv" / "lib64").is_symlink()
1 change: 1 addition & 0 deletions tests/integration/parts/plugins/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ def test_python_plugin(
# Check that the stage directory looks correct.
assert (stage_path / "src" / "charm.py").read_text() == "# Charm file"
assert (stage_path / "venv" / "lib").is_dir()
assert not (stage_path / "venv" / "lib64").is_symlink()

0 comments on commit 6997eb3

Please sign in to comment.