Skip to content

Commit

Permalink
Support space separated markers in URL reqs. (pex-tool#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois authored Jan 18, 2023
1 parent e8844ec commit f433ad6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pex/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import os
import re
import sys
from contextlib import contextmanager

from pex import attrs, dist_metadata, pex_warnings
Expand Down Expand Up @@ -396,6 +397,8 @@ def _try_parse_pip_local_formats(
# + Environment markers denoted by `;...`
# See: https://www.python.org/dev/peps/pep-0508/#environment-markers
r";",
# + A trailing whitespace character separating any of the above
r"\s",
)
# N.B.: The basename of the current directory (.) is '' and we allow this.
match = re.match(
Expand Down Expand Up @@ -481,7 +484,9 @@ def _parse_requirement_line(
specifier = None # type: Optional[SpecifierSet]
if not project_name:
project_name_and_specifier = _try_parse_project_name_and_specifier_from_path(
unquote(parsed_url.path)
# There may be whitespace separated markers; so we strip the trailing whitespace
# used to support those.
unquote(parsed_url.path).rstrip()
)
if project_name_and_specifier is not None:
project_name = project_name_and_specifier.project_name
Expand All @@ -499,7 +504,9 @@ def _parse_requirement_line(
"#egg=<project name>."
),
)
url = parsed_url._replace(params="", fragment="").geturl()
# There may be whitespace separated markers; so we strip the trailing whitespace
# used to support those.
url = parsed_url._replace(params="", fragment="").geturl().rstrip()
requirement = parse_requirement_from_project_name_and_specifier(
project_name,
extras=extras,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def test_parse_requirements_stress(chroot):
a/local/project[foo]; python_full_version == "2.7.8"
./another/local/project;python_version == "2.7.*"
./another/local/project ; python_version == "2.7.*"
./another/local/project
./
# Local projects with basenames that are invalid Python project names (trailing _):
Expand All @@ -274,18 +275,23 @@ def test_parse_requirements_stress(chroot):
hg+http://hg.example.com/MyProject@da39a3ee5e6b\\
#egg=AnotherProject[extra,more];python_version=="3.9.*"&subdirectory=foo/bar
hg+http://hg.example.com/MyProject@da39a3ee5e6b\\
#egg=AnotherProject[extra,more] ; python_version=="3.9.*"&subdirectory=foo/bar
ftp://a/${PROJECT_NAME}-1.0.tar.gz
http://a/${PROJECT_NAME}-1.0.zip
https://a/numpy-1.9.2-cp34-none-win32.whl
https://a/numpy-1.9.2-cp34-none-win32.whl;\\
python_version=="3.4.*" and sys_platform=='win32'
https://a/numpy-1.9.2-cp34-none-win32.whl ; \\
python_version=="3.4.*" and sys_platform=='win32'
Django@ git+https://github.com/django/django.git
Django@git+https://github.com/django/django.git@stable/2.1.x
Django@ git+https://github.com/django/django.git\\
@fd209f62f1d83233cc634443cfac5ee4328d98b8
Django @ file:projects/django-2.3.zip; python_version >= "3.10"
Django @ file:projects/django-2.3.zip ;python_version >= "3.10"
# Wheel with local version
http://download.pytorch.org/whl/cpu/torch-1.12.1%2Bcpu-cp310-cp310-linux_x86_64.whl
Expand Down Expand Up @@ -390,6 +396,10 @@ def test_parse_requirements_stress(chroot):
path=os.path.realpath("extra/another/local/project"),
marker="python_version == '2.7.*'",
),
local_req(
path=os.path.realpath("extra/another/local/project"),
marker="python_version == '2.7.*'",
),
local_req(path=os.path.realpath("extra/another/local/project")),
local_req(path=os.path.realpath("extra")),
local_req(path=os.path.realpath("extra/tmp/tmpW8tdb_")),
Expand All @@ -406,6 +416,13 @@ def test_parse_requirements_stress(chroot):
extras=["more", "extra"],
marker="python_version == '3.9.*'",
),
vcs_req(
vcs=VCS.Mercurial,
project_name="AnotherProject",
url="http://hg.example.com/MyProject@da39a3ee5e6b",
extras=["more", "extra"],
marker="python_version == '3.9.*'",
),
url_req(project_name="Project", url="ftp://a/Project-1.0.tar.gz", specifier="==1.0"),
url_req(project_name="Project", url="http://a/Project-1.0.zip", specifier="==1.0"),
url_req(
Expand All @@ -419,6 +436,12 @@ def test_parse_requirements_stress(chroot):
specifier="==1.9.2",
marker="python_version == '3.4.*' and sys_platform == 'win32'",
),
url_req(
project_name="numpy",
url="https://a/numpy-1.9.2-cp34-none-win32.whl",
specifier="==1.9.2",
marker="python_version == '3.4.*' and sys_platform == 'win32'",
),
vcs_req(vcs=VCS.Git, project_name="Django", url="https://github.com/django/django.git"),
vcs_req(
vcs=VCS.Git,
Expand All @@ -436,6 +459,12 @@ def test_parse_requirements_stress(chroot):
specifier="==2.3",
marker="python_version>='3.10'",
),
url_req(
project_name="Django",
url=os.path.realpath("extra/projects/django-2.3.zip"),
specifier="==2.3",
marker="python_version>='3.10'",
),
url_req(
project_name="torch",
url="http://download.pytorch.org/whl/cpu/torch-1.12.1%2Bcpu-cp310-cp310-linux_x86_64.whl",
Expand Down

0 comments on commit f433ad6

Please sign in to comment.