From 988d3904395e18a2ec646872dafb0795f6bdc94b Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 25 Aug 2022 22:43:59 -0400 Subject: [PATCH 1/5] Allow the pip.conf to override the default index source. --- pipenv/project.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index f9d53ba31d..27e917d395 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from __future__ import annotations import base64 @@ -23,6 +22,8 @@ from pipenv.environment import Environment from pipenv.environments import Setting, is_in_virtualenv, normalize_pipfile_path from pipenv.patched.pip._internal.commands.install import InstallCommand +from pipenv.patched.pip._internal.configuration import Configuration +from pipenv.patched.pip._internal.exceptions import ConfigurationError from pipenv.utils.constants import is_type_checking from pipenv.utils.dependencies import ( get_canonical_names, @@ -129,7 +130,22 @@ def __init__(self, python_version=None, chdir=True): self._build_system = {"requires": ["setuptools", "wheel"]} self.python_version = python_version self.s = Setting() - if self.s.PIPENV_TEST_INDEX: + # Load Pip configuration + self.configuration = Configuration(isolated=False, load_only=None) + self.configuration.load() + print(f"Configuration: {self.configuration}") + print(f"Configuration Dictionary: {self.configuration._dictionary}") + try: + pip_configured_index = self.configuration.get_value("global.index-url") + except ConfigurationError: + pip_configured_index = None + if pip_configured_index: + self.default_source = { + "url": pip_configured_index, + "verify_ssl": True, + "name": "pip_conf_index", + } + elif self.s.PIPENV_TEST_INDEX: self.default_source = { "url": self.s.PIPENV_TEST_INDEX, "verify_ssl": True, From a81728af3aa182037678df9b3933bceb1d45771f Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 25 Jan 2023 23:09:11 -0500 Subject: [PATCH 2/5] Remove prints. --- pipenv/project.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index 604c56f13b..8c73ecb3fe 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -136,8 +136,6 @@ def __init__(self, python_version=None, chdir=True): # Load Pip configuration self.configuration = Configuration(isolated=False, load_only=None) self.configuration.load() - print(f"Configuration: {self.configuration}") - print(f"Configuration Dictionary: {self.configuration._dictionary}") try: pip_configured_index = self.configuration.get_value("global.index-url") except ConfigurationError: From ee22183d4313754bb46e46cba0c9c32ed8dfb83b Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 10 Feb 2023 22:14:50 -0500 Subject: [PATCH 3/5] Refactor pip config sources to handle multiple sources defined. --- pipenv/environments.py | 2 +- pipenv/project.py | 46 ++++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/pipenv/environments.py b/pipenv/environments.py index 64f74f5a0b..3a0b1b6c93 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -362,7 +362,7 @@ def __init__(self) -> None: # Internal, support running in a different Python from sys.executable. self.PIPENV_PYTHON = get_from_env("PYTHON", check_for_negation=False) - # Internal, overwrite all index funcitonality. + # Internal, overwrite all index functionality. self.PIPENV_TEST_INDEX = get_from_env("TEST_INDEX", check_for_negation=False) # Internal, tells Pipenv about the surrounding environment. diff --git a/pipenv/project.py b/pipenv/project.py index 8c73ecb3fe..cfa82ccf8c 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -30,7 +30,7 @@ pep423_name, python_version, ) -from pipenv.utils.internet import get_url_name, is_valid_url, proper_case +from pipenv.utils.internet import get_url_name, is_valid_url, proper_case, is_pypi_url from pipenv.utils.shell import ( find_requirements, find_windows_executable, @@ -133,19 +133,34 @@ def __init__(self, python_version=None, chdir=True): self._build_system = {"requires": ["setuptools", "wheel"]} self.python_version = python_version self.s = Setting() - # Load Pip configuration + # Load Pip configuration and get items self.configuration = Configuration(isolated=False, load_only=None) self.configuration.load() - try: - pip_configured_index = self.configuration.get_value("global.index-url") - except ConfigurationError: - pip_configured_index = None - if pip_configured_index: - self.default_source = { - "url": pip_configured_index, - "verify_ssl": True, - "name": "pip_conf_index", - } + pip_conf_indexes = [] + for section_key, value in self.configuration.items(): + key_parts = section_key.split(".", 1) + if key_parts[1] == "index-url": + try: + trusted_hosts = self.configuration.get_value(f"{key_parts[0]}.trusted-host") + except ConfigurationError: + trusted_hosts = [] + pip_conf_indexes.append({ + "url": value, + "verify_ssl": not any( + trusted_host in value + for trusted_host in trusted_hosts + ) and "https://" in value, + "name": f"pip_conf_index_{key_parts[0]}" + }) + + if pip_conf_indexes: + self.default_source = None + for pip_conf_index in pip_conf_indexes: + if self.default_source is None: + self.default_source = pip_conf_index + if is_pypi_url(pip_conf_index["url"]): + self.default_source = pip_conf_index + pip_conf_indexes.remove(self.default_source) elif self.s.PIPENV_TEST_INDEX: self.default_source = { "url": self.s.PIPENV_TEST_INDEX, @@ -159,9 +174,10 @@ def __init__(self, python_version=None, chdir=True): "name": "pypi", } - plette.pipfiles.DEFAULT_SOURCE_TOML = ( - f"[[source]]\n{toml.dumps(self.default_source)}" - ) + default_sources_toml = f"[[source]]\n{toml.dumps(self.default_source)}" + for pip_conf_index in pip_conf_indexes: + default_sources_toml += f"\n\n[[source]]\n{toml.dumps(pip_conf_index)}" + plette.pipfiles.DEFAULT_SOURCE_TOML = default_sources_toml # Hack to skip this during pipenv run, or -r. if ("run" not in sys.argv) and chdir: From 955c822466c65f6dbc34f6271e254d3e45fe6209 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 10 Feb 2023 22:20:31 -0500 Subject: [PATCH 4/5] fix lint --- pipenv/project.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index cfa82ccf8c..1710abcfd8 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -30,7 +30,7 @@ pep423_name, python_version, ) -from pipenv.utils.internet import get_url_name, is_valid_url, proper_case, is_pypi_url +from pipenv.utils.internet import get_url_name, is_pypi_url, is_valid_url, proper_case from pipenv.utils.shell import ( find_requirements, find_windows_executable, @@ -141,17 +141,21 @@ def __init__(self, python_version=None, chdir=True): key_parts = section_key.split(".", 1) if key_parts[1] == "index-url": try: - trusted_hosts = self.configuration.get_value(f"{key_parts[0]}.trusted-host") + trusted_hosts = self.configuration.get_value( + f"{key_parts[0]}.trusted-host" + ) except ConfigurationError: trusted_hosts = [] - pip_conf_indexes.append({ - "url": value, - "verify_ssl": not any( - trusted_host in value - for trusted_host in trusted_hosts - ) and "https://" in value, - "name": f"pip_conf_index_{key_parts[0]}" - }) + pip_conf_indexes.append( + { + "url": value, + "verify_ssl": not any( + trusted_host in value for trusted_host in trusted_hosts + ) + and "https://" in value, + "name": f"pip_conf_index_{key_parts[0]}", + } + ) if pip_conf_indexes: self.default_source = None From 152877c5beb566dd0bb8bd8d4dbde47ff8f83511 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 17 Feb 2023 18:48:12 -0500 Subject: [PATCH 5/5] Adds news fragment. --- news/5297.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/5297.feature.rst diff --git a/news/5297.feature.rst b/news/5297.feature.rst new file mode 100644 index 0000000000..7a249a407a --- /dev/null +++ b/news/5297.feature.rst @@ -0,0 +1 @@ +``pipenv`` now reads the system ``pip.conf`` or ``pip.ini`` file in order to determine pre-defined indexes to use for package resolution and installation.