From fc67bb9292fe01ffc16da6cc2f4dce3c87ba2b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Tue, 8 Mar 2022 14:08:25 +0100 Subject: [PATCH 1/5] Fix an issue for projects with an at sign (``@``) in the path It seems `requirementslib` is causing a crash in `pipenv` in some rare circumstances. If you define a package like this in a `Pipfile`: ``` myproject = {editable = true, path = "."} ``` and this project path contains an at sign (`@`), then requirementslib will parse the path wrong. The second clause in the added test case will fail without this fix. Fixes #309 --- news/309.bugfix.rst | 1 + src/requirementslib/models/utils.py | 2 +- tests/unit/test_utils.py | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 news/309.bugfix.rst diff --git a/news/309.bugfix.rst b/news/309.bugfix.rst new file mode 100644 index 00000000..edf93a1b --- /dev/null +++ b/news/309.bugfix.rst @@ -0,0 +1 @@ +Fix an issue for projects with an at sign (``@``) in the path diff --git a/src/requirementslib/models/utils.py b/src/requirementslib/models/utils.py index c0f4b239..95050be4 100644 --- a/src/requirementslib/models/utils.py +++ b/src/requirementslib/models/utils.py @@ -565,7 +565,7 @@ def split_ref_from_uri(uri): raise TypeError("Expected a string, received {0!r}".format(uri)) parsed = _get_parsed_url(uri) path = parsed.path if parsed.path else "" - scheme = parsed.scheme if parsed.scheme else "" + scheme = parsed.scheme if parsed.scheme else "file" ref = None if scheme != "file" and "@" in path: path, _, ref = path.rpartition("@") diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 9fb4ce78..3f739a5c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -100,6 +100,29 @@ def test_split_vcs_method_from_uri(): ) +def test_split_ref_from_uri(): + url = "https://github.com/sarugaku/plette.git" + assert utils.split_ref_from_uri(url) == ( + "https://github.com/sarugaku/plette.git", + None + ) + url = "/Users/some.user@acme.com/dev/myproject" + assert utils.split_ref_from_uri(url) == ( + "/Users/some.user@acme.com/dev/myproject", + None + ) + url = "https://user:password@github.com/sarugaku/plette.git" + assert utils.split_ref_from_uri(url) == ( + "https://user:password@github.com/sarugaku/plette.git", + None + ) + url = "git+https://github.com/pypa/pipenv.git@master#egg=pipenv" + assert utils.split_ref_from_uri(url) == ( + "git+https://github.com/pypa/pipenv.git#egg=pipenv", + "master" + ) + + # tests from pip-tools From d6c23ee8a3a8e24a7c75c3ae03181f88069b9e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Wed, 23 Mar 2022 09:08:56 +0100 Subject: [PATCH 2/5] fixup! Fix an issue for projects with an at sign (``@``) in the path --- tests/unit/test_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 3f739a5c..be272475 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -104,22 +104,22 @@ def test_split_ref_from_uri(): url = "https://github.com/sarugaku/plette.git" assert utils.split_ref_from_uri(url) == ( "https://github.com/sarugaku/plette.git", - None + None, ) url = "/Users/some.user@acme.com/dev/myproject" assert utils.split_ref_from_uri(url) == ( "/Users/some.user@acme.com/dev/myproject", - None + None, ) url = "https://user:password@github.com/sarugaku/plette.git" assert utils.split_ref_from_uri(url) == ( "https://user:password@github.com/sarugaku/plette.git", - None + None, ) url = "git+https://github.com/pypa/pipenv.git@master#egg=pipenv" assert utils.split_ref_from_uri(url) == ( "git+https://github.com/pypa/pipenv.git#egg=pipenv", - "master" + "master", ) From 3c4172ee385f1cf6702c891f7713fe4480d019b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Tue, 26 Apr 2022 22:31:14 +0200 Subject: [PATCH 3/5] Detect @ character using a regex instead The motivation is to only care about @ that is not part of the path but rather used for git refs --- src/requirementslib/models/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/requirementslib/models/utils.py b/src/requirementslib/models/utils.py index 95050be4..9b0ec618 100644 --- a/src/requirementslib/models/utils.py +++ b/src/requirementslib/models/utils.py @@ -565,9 +565,9 @@ def split_ref_from_uri(uri): raise TypeError("Expected a string, received {0!r}".format(uri)) parsed = _get_parsed_url(uri) path = parsed.path if parsed.path else "" - scheme = parsed.scheme if parsed.scheme else "file" + scheme = parsed.scheme if parsed.scheme else "" ref = None - if scheme != "file" and "@" in path: + if scheme != "file" and re.match("^.*@[^/@]*$", path): path, _, ref = path.rpartition("@") parsed = parsed._replace(path=path) return (parsed.url, ref) From 9629660bf4e6c24351e0cf19e27980783ffb6dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Tue, 26 Apr 2022 23:29:07 +0200 Subject: [PATCH 4/5] Support paths with @ and refs with slashes --- src/requirementslib/models/utils.py | 2 +- tests/unit/test_utils.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/requirementslib/models/utils.py b/src/requirementslib/models/utils.py index 9b0ec618..14e3a742 100644 --- a/src/requirementslib/models/utils.py +++ b/src/requirementslib/models/utils.py @@ -567,7 +567,7 @@ def split_ref_from_uri(uri): path = parsed.path if parsed.path else "" scheme = parsed.scheme if parsed.scheme else "" ref = None - if scheme != "file" and re.match("^.*@[^/@]*$", path): + if scheme != "file" and (re.match("^.*@[^/@]*$", path) or path.count('@') >= 2): path, _, ref = path.rpartition("@") parsed = parsed._replace(path=path) return (parsed.url, ref) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index be272475..80ee6c0c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -101,26 +101,26 @@ def test_split_vcs_method_from_uri(): def test_split_ref_from_uri(): - url = "https://github.com/sarugaku/plette.git" - assert utils.split_ref_from_uri(url) == ( + assert utils.split_ref_from_uri("https://github.com/sarugaku/plette.git") == ( "https://github.com/sarugaku/plette.git", None, ) - url = "/Users/some.user@acme.com/dev/myproject" - assert utils.split_ref_from_uri(url) == ( + assert utils.split_ref_from_uri("/Users/some.user@acme.com/dev/myproject") == ( "/Users/some.user@acme.com/dev/myproject", None, ) - url = "https://user:password@github.com/sarugaku/plette.git" - assert utils.split_ref_from_uri(url) == ( + assert utils.split_ref_from_uri("https://user:password@github.com/sarugaku/plette.git") == ( "https://user:password@github.com/sarugaku/plette.git", None, ) - url = "git+https://github.com/pypa/pipenv.git@master#egg=pipenv" - assert utils.split_ref_from_uri(url) == ( + assert utils.split_ref_from_uri("git+https://github.com/pypa/pipenv.git@master#egg=pipenv") == ( "git+https://github.com/pypa/pipenv.git#egg=pipenv", "master", ) + assert utils.split_ref_from_uri("/Users/some.user@acme.com/dev/myproject@bugfix/309") == ( + "/Users/some.user@acme.com/dev/myproject", + "bugfix/309", + ) # tests from pip-tools From 934b24d11acc554f25f95423cf24a6796d0a495a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Wed, 27 Apr 2022 08:51:26 +0200 Subject: [PATCH 5/5] Make the pre-commit hook happy --- src/requirementslib/models/utils.py | 2 +- tests/unit/test_utils.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/requirementslib/models/utils.py b/src/requirementslib/models/utils.py index 14e3a742..5b11f261 100644 --- a/src/requirementslib/models/utils.py +++ b/src/requirementslib/models/utils.py @@ -567,7 +567,7 @@ def split_ref_from_uri(uri): path = parsed.path if parsed.path else "" scheme = parsed.scheme if parsed.scheme else "" ref = None - if scheme != "file" and (re.match("^.*@[^/@]*$", path) or path.count('@') >= 2): + if scheme != "file" and (re.match("^.*@[^/@]*$", path) or path.count("@") >= 2): path, _, ref = path.rpartition("@") parsed = parsed._replace(path=path) return (parsed.url, ref) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 80ee6c0c..fb16961e 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -109,15 +109,21 @@ def test_split_ref_from_uri(): "/Users/some.user@acme.com/dev/myproject", None, ) - assert utils.split_ref_from_uri("https://user:password@github.com/sarugaku/plette.git") == ( + assert utils.split_ref_from_uri( + "https://user:password@github.com/sarugaku/plette.git" + ) == ( "https://user:password@github.com/sarugaku/plette.git", None, ) - assert utils.split_ref_from_uri("git+https://github.com/pypa/pipenv.git@master#egg=pipenv") == ( + assert utils.split_ref_from_uri( + "git+https://github.com/pypa/pipenv.git@master#egg=pipenv" + ) == ( "git+https://github.com/pypa/pipenv.git#egg=pipenv", "master", ) - assert utils.split_ref_from_uri("/Users/some.user@acme.com/dev/myproject@bugfix/309") == ( + assert utils.split_ref_from_uri( + "/Users/some.user@acme.com/dev/myproject@bugfix/309" + ) == ( "/Users/some.user@acme.com/dev/myproject", "bugfix/309", )