From 2aa2da7d49ac69229c7616cff2524cd963734fd9 Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Thu, 18 Jul 2024 15:46:55 -0400 Subject: [PATCH] fix(pip): set `--no-binary=:all:` if possible (#1740) This reduces the chances of having pip install binary packages as indirect dependencies. Partial fix for #1473 --- charmcraft/utils/package.py | 20 ++++++++++++-------- tests/unit/utils/test_package.py | 2 ++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/charmcraft/utils/package.py b/charmcraft/utils/package.py index 1be541397..80d231606 100644 --- a/charmcraft/utils/package.py +++ b/charmcraft/utils/package.py @@ -103,10 +103,6 @@ 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), @@ -114,10 +110,18 @@ def get_pip_command( ) ) - 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, diff --git a/tests/unit/utils/test_package.py b/tests/unit/utils/test_package.py index dfcad6879..8bfe73afb 100644 --- a/tests/unit/utils/test_package.py +++ b/tests/unit/utils/test_package.py @@ -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"]])