From c132ddede1355e3496ba2c63333bb2e0212c6b87 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Fri, 20 Oct 2023 14:33:30 -0400 Subject: [PATCH] Account for PEP668 in pip invocations Set the PIP_BREAK_SYSTEM_PACKAGES environment variable anywhere pip is in use to account for PEP668 which would change pip to not allow us to install in the system environment for newer versions of pip. --- src/ansible_builder/_target_scripts/assemble | 3 ++ .../_target_scripts/install-from-bindep | 4 ++ src/ansible_builder/containerfile.py | 4 ++ test/unit/test_containerfile.py | 47 +++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/src/ansible_builder/_target_scripts/assemble b/src/ansible_builder/_target_scripts/assemble index d82e5c46..0d111a4c 100755 --- a/src/ansible_builder/_target_scripts/assemble +++ b/src/ansible_builder/_target_scripts/assemble @@ -20,6 +20,9 @@ # at build time. set -ex +# Account for PEP668 +export PIP_BREAK_SYSTEM_PACKAGES=1 + RELEASE=$(source /etc/os-release; echo $ID) # NOTE(pabelanger): Allow users to force either microdnf or dnf as a package diff --git a/src/ansible_builder/_target_scripts/install-from-bindep b/src/ansible_builder/_target_scripts/install-from-bindep index c5807071..f2ec42c9 100755 --- a/src/ansible_builder/_target_scripts/install-from-bindep +++ b/src/ansible_builder/_target_scripts/install-from-bindep @@ -15,6 +15,10 @@ # limitations under the License. set -ex + +# Account for PEP668 +export PIP_BREAK_SYSTEM_PACKAGES=1 + # NOTE(pabelanger): Allow users to force either microdnf or dnf as a package # manager. PKGMGR="${PKGMGR:-}" diff --git a/src/ansible_builder/containerfile.py b/src/ansible_builder/containerfile.py index 426f40ae..fe2b1574 100644 --- a/src/ansible_builder/containerfile.py +++ b/src/ansible_builder/containerfile.py @@ -235,6 +235,10 @@ def _insert_global_args(self, include_values: bool = False) -> None: self.steps.append(f'ARG {arg}="{value}"') else: self.steps.append(f"ARG {arg}") + + # To account for PEP668 (https://peps.python.org/pep-0668/), always set this + # pip environment variable for every EE version in every stage. + self.steps.append("ENV PIP_BREAK_SYSTEM_PACKAGES=1") self.steps.append("") def _create_folder_copy_files(self) -> None: diff --git a/test/unit/test_containerfile.py b/test/unit/test_containerfile.py index b78be4a2..2ea1e8a5 100644 --- a/test/unit/test_containerfile.py +++ b/test/unit/test_containerfile.py @@ -207,3 +207,50 @@ def test__handle_additional_build_files(build_dir_and_ee_yml): config_dir = tmpdir / '_build' / 'configs' assert config_dir.exists() assert (config_dir / 'ansible.cfg').exists() + + +def test_pep668_env_var_v1(build_dir_and_ee_yml): + """ + Test that we add the pip env var to handle PEP668. + """ + ee_data = """ + version: 1 + """ + tmpdir, ee_path = build_dir_and_ee_yml(ee_data) + c = make_containerfile(tmpdir, ee_path, run_validate=True) + c.prepare() + assert "ENV PIP_BREAK_SYSTEM_PACKAGES=1" in c.steps + + +def test_pep668_env_var_v2(build_dir_and_ee_yml): + """ + Test that we add the pip env var to handle PEP668. + """ + ee_data = """ + version: 2 + images: + base_image: + name: quay.io/user/mycustombaseimage:latest + builder_image: + name: quay.io/user/mycustombuilderimage:latest + """ + tmpdir, ee_path = build_dir_and_ee_yml(ee_data) + c = make_containerfile(tmpdir, ee_path, run_validate=True) + c.prepare() + assert "ENV PIP_BREAK_SYSTEM_PACKAGES=1" in c.steps + + +def test_pep668_env_var_v3(build_dir_and_ee_yml): + """ + Test that we add the pip env var to handle PEP668. + """ + ee_data = """ + version: 3 + images: + base_image: + name: quay.io/user/mycustombaseimage:latest + """ + tmpdir, ee_path = build_dir_and_ee_yml(ee_data) + c = make_containerfile(tmpdir, ee_path, run_validate=True) + c.prepare() + assert "ENV PIP_BREAK_SYSTEM_PACKAGES=1" in c.steps