Skip to content

Commit

Permalink
added env override for default index URL
Browse files Browse the repository at this point in the history
  • Loading branch information
djcass44 authored and frenzymadness committed Nov 20, 2023
1 parent ee82f23 commit 53eac30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ To create a virtual environment to be used by ``micropipenv``:
python3 -m venv venv/ && . venv/bin/activate
To set the default Python Package Index to something other than ``https://pypi.org/simple``, set the ``MICROPIPENV_DEFAULT_INDEX_URLS`` to one or more comma-separated URLs.

Note: if the package manager file contains a package index URL, it will be used over this value.

.. code-block:: console
export MICROPIPENV_DEFAULT_INDEX_URLS=https://pypi.example.com/simple,https://pypi.org/simple
micropipenv install
``micropipenv install --deploy``
================================
Expand Down
11 changes: 10 additions & 1 deletion micropipenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@
from typing import Union
from pip._internal.req.req_file import ParsedRequirement

_DEFAULT_INDEX_URLS = ("https://pypi.org/simple",)

def get_index_urls(): # type: () -> Tuple[str, ...]
"""Return parsed MICROPIPENV_DEFAULT_INDEX_URLS env variable or the default value."""
urls = os.getenv("MICROPIPENV_DEFAULT_INDEX_URLS")
if urls and urls.strip() != "":
return tuple([url.strip() for url in urls.split(",")])
return ("https://pypi.org/simple",)


_DEFAULT_INDEX_URLS = get_index_urls()
_MAX_DIR_TRAVERSAL = 42 # Avoid any symlinks that would loop.
_PIP_BIN = os.getenv("MICROPIPENV_PIP_BIN", "pip")
_SUPPORTED_PIP = SpecifierSet(_SUPPORTED_PIP_STR)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_micropipenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ def check_generated_pipfile_lock(pipfile_lock_path, pipfile_lock_path_expected):
os.remove(pipfile_lock_path)


@pytest.mark.parametrize(
"input,expected",
[
[
"https://pypi.org/simple, https://example.org/simple",
("https://pypi.org/simple", "https://example.org/simple"),
],
["https://example.org/simple", ("https://example.org/simple",)],
["", ("https://pypi.org/simple",)],
[" ", ("https://pypi.org/simple",)],
["\t", ("https://pypi.org/simple",)],
[None, ("https://pypi.org/simple",)],
],
)
def test_get_index_urls(input, expected):
"""Test index url parsing"""
if input is not None:
os.environ["MICROPIPENV_DEFAULT_INDEX_URLS"] = input
else:
del os.environ["MICROPIPENV_DEFAULT_INDEX_URLS"]

result = micropipenv.get_index_urls()
assert result == expected


@pytest.mark.online
def test_install_pipenv(venv):
"""Test invoking installation using information in Pipfile.lock."""
Expand Down

0 comments on commit 53eac30

Please sign in to comment.