Skip to content

Commit abf5494

Browse files
michal-simecekfridexfrenzymadness
authored
Add resolving enviroment variables in Pipfile URL. (#178)
* Add resolving enviroment variables in Pipfile URL. * Update micropipenv.py Co-authored-by: Fridolín Pokorný <[email protected]> * squash! Add resolving enviroment variables in Pipfile URL. * Added tests. * test env vars not working * Test for env variables changed. * Update tests/test_micropipenv.py Co-authored-by: Lumír 'Frenzy' Balhar <[email protected]> * Resolve env variables also in _get_index_entry_str * Fix tests * Fixed format * Fix end of files Co-authored-by: Fridolín Pokorný <[email protected]> Co-authored-by: Lumír 'Frenzy' Balhar <[email protected]> Co-authored-by: Lumir Balhar <[email protected]>
1 parent 1469101 commit abf5494

File tree

6 files changed

+133
-5
lines changed

6 files changed

+133
-5
lines changed

micropipenv.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
("requirements.txt", "requirements"),
110110
]
111111
)
112+
__re_nested_vars = re.compile(r"\$\{(?P<name>[^\}:]*)(?::-(?P<default>[^\}]*))?\}")
113+
__re_sub_vars = re.compile(r"\$\{[^}]*\}")
112114

113115

114116
class MicropipenvException(Exception):
@@ -949,20 +951,21 @@ def _get_index_entry_str(sections, package_info=None): # type: (Dict[str, Any],
949951

950952
result = ""
951953
for idx, source in enumerate(sections.get("sources", [])):
954+
url = _resolve_nested_variables(source["url"])
952955
if index_name is None:
953956
if idx == 0:
954-
result += "--index-url {}\n".format(source["url"])
957+
result += "--index-url {}\n".format(url)
955958
else:
956-
result += "--extra-index-url {}\n".format(source["url"])
959+
result += "--extra-index-url {}\n".format(url)
957960

958961
if not source["verify_ssl"]:
959-
result += "--trusted-host {}\n".format(urlparse(source["url"]).netloc)
962+
result += "--trusted-host {}\n".format(urlparse(url).netloc)
960963
else:
961964
if index_name == source["name"]:
962-
result += "--index-url {}\n".format(source["url"])
965+
result += "--index-url {}\n".format(url)
963966

964967
if not source["verify_ssl"]:
965-
result += "--trusted-host {}\n".format(urlparse(source["url"]).netloc)
968+
result += "--trusted-host {}\n".format(urlparse(url).netloc)
966969

967970
break
968971

@@ -974,6 +977,19 @@ def _get_index_entry_str(sections, package_info=None): # type: (Dict[str, Any],
974977
return result
975978

976979

980+
def _resolve_nested_variables(url):
981+
# type: (str) -> str
982+
while True:
983+
variable = __re_nested_vars.search(url)
984+
if not variable:
985+
break
986+
value = os.getenv(variable["name"])
987+
if not value:
988+
value = variable["default"] if variable["default"] else ""
989+
url = __re_sub_vars.sub(value, url, count=1)
990+
return url
991+
992+
977993
def _iter_index_entry_str(
978994
sections, package_info
979995
): # type: (Dict[str, Any], Optional[Dict[str, Any]]) -> Generator[str, None, None]
@@ -987,6 +1003,7 @@ def _iter_index_entry_str(
9871003
return None
9881004

9891005
for source in sections["sources"]:
1006+
source["url"] = _resolve_nested_variables(source["url"])
9901007
result = "--index-url {}\n".format(source["url"])
9911008
if not source["verify_ssl"]:
9921009
result += "--trusted-host {}\n".format(urlparse(source["url"]).netloc)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "${URL}"
4+
verify_ssl = true
5+
6+
[packages]
7+
daiquiri = "==2.0.0"

tests/data/install/pipenv_env_vars/Pipfile.lock

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "${URL:-https://pypi.org/simple}"
4+
verify_ssl = true
5+
6+
[packages]
7+
daiquiri = "==2.0.0"

tests/data/install/pipenv_env_vars_default/Pipfile.lock

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_micropipenv.py

+29
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,35 @@ def test_install_poetry_vcs(venv):
196196
check_generated_pipfile_lock(os.path.join(work_dir, "Pipfile.lock"), os.path.join(work_dir, "_Pipfile.lock"))
197197

198198

199+
@pytest.mark.online
200+
def test_install_pipenv_env_vars(venv):
201+
"""Test installation using enviroment variables in source URL."""
202+
cmd = [os.path.join(venv.path, BIN_DIR, "python"), micropipenv.__file__, "install", "--method", "pipenv"]
203+
with cwd(os.path.join(_DATA_DIR, "install", "pipenv_env_vars")):
204+
subprocess.run(cmd, check=True, env={**get_updated_env(venv), **{"URL": "https://pypi.org/simple"}})
205+
assert str(venv.get_version("daiquiri")) == "2.0.0"
206+
assert str(venv.get_version("python-json-logger")) == "0.1.11"
207+
208+
209+
@pytest.mark.online
210+
def test_install_pipenv_env_vars_undefined(venv):
211+
"""Test installation using enviroment variables without setting them."""
212+
cmd = [os.path.join(venv.path, BIN_DIR, "python"), micropipenv.__file__, "install", "--method", "pipenv"]
213+
with cwd(os.path.join(_DATA_DIR, "install", "pipenv_env_vars")):
214+
with pytest.raises(subprocess.CalledProcessError):
215+
subprocess.check_call(cmd, env=get_updated_env(venv))
216+
217+
218+
@pytest.mark.online
219+
def test_install_pipenv_env_vars_default(venv):
220+
"""Test installation using default values of environment variables."""
221+
cmd = [os.path.join(venv.path, BIN_DIR, "python"), micropipenv.__file__, "install", "--method", "pipenv"]
222+
with cwd(os.path.join(_DATA_DIR, "install", "pipenv_env_vars_default")):
223+
subprocess.run(cmd, check=True, env=get_updated_env(venv))
224+
assert str(venv.get_version("daiquiri")) == "2.0.0"
225+
assert str(venv.get_version("python-json-logger")) == "0.1.11"
226+
227+
199228
@pytest.mark.online
200229
def test_install_pip_tools_print_lock(venv):
201230
"""Test invoking installation when pip-tools style requirements.txt are used.

0 commit comments

Comments
 (0)