Skip to content

Commit

Permalink
fix: Fix & always include bin/activate in python & poetry plugin
Browse files Browse the repository at this point in the history
Including `bin/activate` in the virtual environment will make debugging much easier (charm developers can easily run Python code using the charm's virtual environment to debug issues)
  • Loading branch information
carlcsaposs-canonical authored and lengau committed Dec 17, 2024
1 parent af59055 commit 7fa2064
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 16 additions & 2 deletions charmcraft/utils/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,25 @@ def get_venv_cleanup_commands(venv_path: pathlib.Path, *, keep_bins: bool) -> li
"""
venv_bin = venv_path / "bin"
venv_lib64 = venv_path / "lib64"
delete_bins = [] if keep_bins else [f"rm -rf {venv_bin}"]
if keep_bins:
delete_bins = []
else:
delete_bins = [
# Remove all files in venv_bin except `activate`
"shopt -s extglob",
f"rm -rf {venv_bin}/!(activate)",
"shopt -u extglob",
]
update_activate = [
# Replace hard-coded path in `activate` with portable path
# "\&" is escape for sed
'sed -i \'s#^VIRTUAL_ENV=.*$#VIRTUAL_ENV="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." \\&> /dev/null \\&\\& pwd )"#\' '
+ str(venv_bin / "activate"),
]
delete_lib64 = textwrap.dedent(f"""
if [ -L '{venv_lib64}' ]; then
rm -f '{venv_lib64}'
fi
""")

return [*delete_bins, delete_lib64]
return [*delete_bins, *update_activate, delete_lib64]
8 changes: 6 additions & 2 deletions tests/unit/parts/plugins/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def test_get_package_install_commands(
def test_get_rm_command(
poetry_plugin: plugins.PoetryPlugin, install_path: pathlib.Path
):
assert f"rm -rf {install_path / 'venv/bin'}" in poetry_plugin.get_build_commands()
assert (
f"rm -rf {install_path / 'venv/bin'}/!(activate)"
in poetry_plugin.get_build_commands()
)


def test_no_get_rm_command(
Expand All @@ -110,5 +113,6 @@ def test_no_get_rm_command(
}
poetry_plugin._options = plugins.PoetryPluginProperties.unmarshal(spec)
assert (
f"rm -rf {install_path / 'venv/bin'}" not in poetry_plugin.get_build_commands()
f"rm -rf {install_path / 'venv/bin'}/!(activate)"
not in poetry_plugin.get_build_commands()
)
8 changes: 6 additions & 2 deletions tests/unit/parts/plugins/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def test_get_package_install_commands(
def test_get_rm_command(
python_plugin: plugins.PythonPlugin, install_path: pathlib.Path
):
assert f"rm -rf {install_path / 'venv/bin'}" in python_plugin.get_build_commands()
assert (
f"rm -rf {install_path / 'venv/bin'}/!(activate)"
in python_plugin.get_build_commands()
)


def test_no_get_rm_command(
Expand All @@ -119,5 +122,6 @@ def test_no_get_rm_command(
}
python_plugin._options = plugins.PythonPluginProperties.unmarshal(spec)
assert (
f"rm -rf {install_path / 'venv/bin'}" not in python_plugin.get_build_commands()
f"rm -rf {install_path / 'venv/bin'}/!(activate)"
not in python_plugin.get_build_commands()
)

0 comments on commit 7fa2064

Please sign in to comment.