Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the pip.conf to override the default index source. #5297

Merged
merged 7 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/5297.feature.rst
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion pipenv/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 40 additions & 5 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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.patched.pip._vendor import pkg_resources
from pipenv.utils.constants import is_type_checking
from pipenv.utils.dependencies import (
Expand All @@ -28,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_pypi_url, is_valid_url, proper_case
from pipenv.utils.shell import (
find_requirements,
find_windows_executable,
Expand Down Expand Up @@ -131,7 +133,39 @@ 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 and get items
self.configuration = Configuration(isolated=False, load_only=None)
self.configuration.load()
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,
"verify_ssl": True,
Expand All @@ -144,9 +178,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:
Expand Down