Skip to content

Commit 2b0dae3

Browse files
authored
Allow the pip.conf to override the default index source. (#5297)
1 parent 5e2c0d6 commit 2b0dae3

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

news/5297.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

pipenv/environments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def __init__(self) -> None:
362362
# Internal, support running in a different Python from sys.executable.
363363
self.PIPENV_PYTHON = get_from_env("PYTHON", check_for_negation=False)
364364

365-
# Internal, overwrite all index funcitonality.
365+
# Internal, overwrite all index functionality.
366366
self.PIPENV_TEST_INDEX = get_from_env("TEST_INDEX", check_for_negation=False)
367367

368368
# Internal, tells Pipenv about the surrounding environment.

pipenv/project.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from pipenv.environment import Environment
2020
from pipenv.environments import Setting, is_in_virtualenv, normalize_pipfile_path
2121
from pipenv.patched.pip._internal.commands.install import InstallCommand
22+
from pipenv.patched.pip._internal.configuration import Configuration
23+
from pipenv.patched.pip._internal.exceptions import ConfigurationError
2224
from pipenv.patched.pip._vendor import pkg_resources
2325
from pipenv.utils.constants import is_type_checking
2426
from pipenv.utils.dependencies import (
@@ -28,7 +30,7 @@
2830
pep423_name,
2931
python_version,
3032
)
31-
from pipenv.utils.internet import get_url_name, is_valid_url, proper_case
33+
from pipenv.utils.internet import get_url_name, is_pypi_url, is_valid_url, proper_case
3234
from pipenv.utils.shell import (
3335
find_requirements,
3436
find_windows_executable,
@@ -131,7 +133,39 @@ def __init__(self, python_version=None, chdir=True):
131133
self._build_system = {"requires": ["setuptools", "wheel"]}
132134
self.python_version = python_version
133135
self.s = Setting()
134-
if self.s.PIPENV_TEST_INDEX:
136+
# Load Pip configuration and get items
137+
self.configuration = Configuration(isolated=False, load_only=None)
138+
self.configuration.load()
139+
pip_conf_indexes = []
140+
for section_key, value in self.configuration.items():
141+
key_parts = section_key.split(".", 1)
142+
if key_parts[1] == "index-url":
143+
try:
144+
trusted_hosts = self.configuration.get_value(
145+
f"{key_parts[0]}.trusted-host"
146+
)
147+
except ConfigurationError:
148+
trusted_hosts = []
149+
pip_conf_indexes.append(
150+
{
151+
"url": value,
152+
"verify_ssl": not any(
153+
trusted_host in value for trusted_host in trusted_hosts
154+
)
155+
and "https://" in value,
156+
"name": f"pip_conf_index_{key_parts[0]}",
157+
}
158+
)
159+
160+
if pip_conf_indexes:
161+
self.default_source = None
162+
for pip_conf_index in pip_conf_indexes:
163+
if self.default_source is None:
164+
self.default_source = pip_conf_index
165+
if is_pypi_url(pip_conf_index["url"]):
166+
self.default_source = pip_conf_index
167+
pip_conf_indexes.remove(self.default_source)
168+
elif self.s.PIPENV_TEST_INDEX:
135169
self.default_source = {
136170
"url": self.s.PIPENV_TEST_INDEX,
137171
"verify_ssl": True,
@@ -144,9 +178,10 @@ def __init__(self, python_version=None, chdir=True):
144178
"name": "pypi",
145179
}
146180

147-
plette.pipfiles.DEFAULT_SOURCE_TOML = (
148-
f"[[source]]\n{toml.dumps(self.default_source)}"
149-
)
181+
default_sources_toml = f"[[source]]\n{toml.dumps(self.default_source)}"
182+
for pip_conf_index in pip_conf_indexes:
183+
default_sources_toml += f"\n\n[[source]]\n{toml.dumps(pip_conf_index)}"
184+
plette.pipfiles.DEFAULT_SOURCE_TOML = default_sources_toml
150185

151186
# Hack to skip this during pipenv run, or -r.
152187
if ("run" not in sys.argv) and chdir:

0 commit comments

Comments
 (0)