Skip to content

Commit

Permalink
fix(pip): set --no-binary=:all: if possible (#1740)
Browse files Browse the repository at this point in the history
This reduces the chances of having pip install binary packages as
indirect dependencies.

Partial fix for #1473
  • Loading branch information
lengau committed Jul 19, 2024
1 parent 18236ea commit 2aa2da7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 12 additions & 8 deletions charmcraft/utils/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,25 @@ def get_pip_command(
binary_packages = get_pypi_packages(binary_deps)
requirements_packages = get_requirements_file_package_names(*requirements_files)
all_packages = charm_packages | binary_packages | requirements_packages
source_only_packages = sorted(
get_package_names(all_packages) - get_package_names(binary_packages)
)

non_requirements_packages = sorted(
exclude_packages(
set(source_deps) | set(binary_deps),
excluded=get_package_names(requirements_packages),
)
)

if source_only_packages:
no_binary = [f"--no-binary={','.join(source_only_packages)}"]
else:
no_binary = []
if not binary_packages:
return [
*prefix,
"--no-binary=:all:",
*(f"--requirement={path}" for path in requirements_files),
*non_requirements_packages,
]

source_only_packages = sorted(
get_package_names(all_packages) - get_package_names(binary_packages)
)
no_binary = [f"--no-binary={','.join(source_only_packages)}"] if source_only_packages else ()

return [
*prefix,
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/utils/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def test_get_requirements_file_package_names(tmp_path, file_contents, expected):
"--no-binary=abc,ghi",
["ghi", "jkl"],
),
(["abc==1.0.0", "def>=1.2.3"], [], [], "--no-binary=:all:", []),
([], ["abc==1.0.0", "def>=1.2.3"], [], "--no-binary=:all:", ["abc==1.0.0", "def>=1.2.3"]),
],
)
@pytest.mark.parametrize("prefix", [["/bin/pip"], ["/some/path/to/pip3"], ["pip", "--some-param"]])
Expand Down

0 comments on commit 2aa2da7

Please sign in to comment.