Skip to content

Issue 180: Fix not parsing pip.conf index urls #181

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
42 changes: 42 additions & 0 deletions src/python_inspector/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#

import os
import subprocess
from netrc import netrc
from typing import Dict
from typing import List
Expand Down Expand Up @@ -72,6 +73,42 @@ def to_dict(self, generic_paths=False):
}


def pip_conf_get_index_urls() -> list:
"""
Returns a list of index_urls as provided by `pip config get`.
If none, it returns an empty list.
"""

# Get index URLS from pip and split them into lists.
# Index URLs are split by whitespace
base_pip = ["python", "-m", "pip"]
config = ["config", "get"]
pip_index_url_cmd = base_pip + config + ["global.index-url"]
pip_extra_index_url_cmd = base_pip + config + ["global.extra-index-url"]
index_urls = subprocess.run(pip_index_url_cmd, capture_output=True)
if index_urls.returncode != 0:
index_urls = []
else:
index_urls = index_urls.stdout.decode("utf-8").split()
extra_index_urls = subprocess.run(pip_extra_index_url_cmd,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would assume pip is installed and available. But we cannot make this blanket assumption. We could instead call its code, extracting and vendoring the pip subset that reads the config file(s). See:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm fine with just abandoning this one since I haven't worked on it in a bit and this one solves most of the issues I had without dealing with pip.conf at the moment.

capture_output=True)
if extra_index_urls.returncode != 0:
extra_index_urls = []
else:
extra_index_urls = extra_index_urls.stdout.decode("utf-8").split()

# Extract index urls from environment variables
pip_index_url_env = [] if os.getenv("PIP_INDEX_URL") is None \
else os.getenv("PIP_INDEX_URL").split()
pip_extra_index_url_env = [] if os.getenv("PIP_EXTRA_INDEX_URL") is None \
else os.getenv("PIP_EXTRA_INDEX_URL").split()
pip_env_urls = pip_index_url_env + pip_extra_index_url_env

all_index_urls = [url for url in index_urls + extra_index_urls +
pip_env_urls if url != ""]
return all_index_urls


def resolve_dependencies(
requirement_files=tuple(),
setup_py_file=None,
Expand Down Expand Up @@ -146,6 +183,11 @@ def resolve_dependencies(

files = []

pip_conf_index_urls = pip_conf_get_index_urls()

if pip_conf_index_urls != []:
index_urls = tuple(pip_conf_index_urls) + tuple(index_urls)

if PYPI_SIMPLE_URL not in index_urls:
index_urls = tuple([PYPI_SIMPLE_URL]) + tuple(index_urls)

Expand Down
Loading