From 53eac3067e917a6e43d67ac664e7b9b0a0eb2783 Mon Sep 17 00:00:00 2001 From: Django Cass Date: Tue, 12 Sep 2023 13:18:01 +1000 Subject: [PATCH] added env override for default index URL --- README.rst | 9 +++++++++ micropipenv.py | 11 ++++++++++- tests/test_micropipenv.py | 25 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 1f08415..82e49a2 100644 --- a/README.rst +++ b/README.rst @@ -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`` ================================ diff --git a/micropipenv.py b/micropipenv.py index a41066d..c23e276 100755 --- a/micropipenv.py +++ b/micropipenv.py @@ -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) diff --git a/tests/test_micropipenv.py b/tests/test_micropipenv.py index 35e4c32..2583ca3 100644 --- a/tests/test_micropipenv.py +++ b/tests/test_micropipenv.py @@ -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."""