From 2927e0060d61fde37fa571610e19fbc2281068b5 Mon Sep 17 00:00:00 2001 From: Cal Crosby Date: Tue, 23 Jul 2024 02:49:39 -0500 Subject: [PATCH] feat: added support for alias @ syntax (#101) - thanks to @Glizzus Signed-off-by: Cal Crosby --- requirements/requirement.py | 7 +- tests/reqfiles/vcs_package_alias.expected | 98 +++++++++++++++++++++++ tests/reqfiles/vcs_package_alias.txt | 7 ++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/reqfiles/vcs_package_alias.expected create mode 100644 tests/reqfiles/vcs_package_alias.txt diff --git a/requirements/requirement.py b/requirements/requirement.py index a6ababd..3d6093e 100644 --- a/requirements/requirement.py +++ b/requirements/requirement.py @@ -31,9 +31,10 @@ r'(#(?P\S+))?' ) +VCS_OPTIONAL_NAME_REGEX = r'(?:(?P\w+)\s*@)?\s*' VCS_SCHEMES_REGEX = r'|'.join([scheme.replace('+', r'\+') for scheme in VCS_SCHEMES]) VCS_REGEX = re.compile( - rf'^(?P{VCS_SCHEMES_REGEX})://((?P[^/@]+)@)?' + rf'^{VCS_OPTIONAL_NAME_REGEX}(?P{VCS_SCHEMES_REGEX})://((?P[^/@]+)@)?' r'(?P[^#@]+)(@(?P[^#]+))?(#(?P\S+))?' ) @@ -150,6 +151,8 @@ def parse_editable(cls, line: str) -> 'Requirement': req.name, req.extras = parse_extras_require(egg) # type: ignore req.hash_name, req.hash = get_hash_info(fragment) # type: ignore req.subdirectory = fragment.get('subdirectory') # type: ignore + if groups['name']: + req.name = groups['name'] # type: ignore for vcs in VCS: if str(req.uri).startswith(vcs): req.vcs = vcs # type: ignore @@ -203,6 +206,8 @@ def parse_line(cls, line: str) -> 'Requirement': for vcs in VCS: if str(req.uri).startswith(vcs): req.vcs = vcs # type: ignore + if groups['name']: + req.name = groups['name'] # type: ignore elif uri_match is not None: groups = uri_match.groupdict() req.uri = f'{groups["scheme"]}://{groups["path"]}' # type: ignore diff --git a/tests/reqfiles/vcs_package_alias.expected b/tests/reqfiles/vcs_package_alias.expected new file mode 100644 index 0000000..bd1b578 --- /dev/null +++ b/tests/reqfiles/vcs_package_alias.expected @@ -0,0 +1,98 @@ +[ + { + "line": "SomeProject@git+https://git.repo/some_pkg.git@1.3.1", + "editable": false, + "local_file": false, + "specifier": false, + "vcs": "git", + "revision": "1.3.1", + "name": "SomeProject", + "uri": "git+https://git.repo/some_pkg.git", + "subdirectory": null, + "path": null, + "hash_name": null, + "hash": null, + "extras": [], + "specs": [] + }, + { + "line": "SomeProject@git+https://git.repo/some_pkg.git@1.3.1#egg=EggName", + "editable": false, + "local_file": false, + "specifier": false, + "vcs": "git", + "revision": "1.3.1", + "name": "SomeProject", + "uri": "git+https://git.repo/some_pkg.git", + "subdirectory": null, + "path": null, + "hash_name": null, + "hash": null, + "extras": [], + "specs": [] + }, + { + "line": "SomeProject@ git+https://git.repo/some_pkg.git@1.3.1", + "editable": false, + "local_file": false, + "specifier": false, + "vcs": "git", + "revision": "1.3.1", + "name": "SomeProject", + "uri": "git+https://git.repo/some_pkg.git", + "subdirectory": null, + "path": null, + "hash_name": null, + "hash": null, + "extras": [], + "specs": [] + }, + { + "line": "SomeProject @git+https://git.repo/some_pkg.git@1.3.1", + "editable": false, + "local_file": false, + "specifier": false, + "vcs": "git", + "revision": "1.3.1", + "name": "SomeProject", + "uri": "git+https://git.repo/some_pkg.git", + "subdirectory": null, + "path": null, + "hash_name": null, + "hash": null, + "extras": [], + "specs": [] + }, + { + "line": "SomeProject @ git+https://git.repo/some_pkg.git@1.3.1", + "editable": false, + "local_file": false, + "specifier": false, + "vcs": "git", + "revision": "1.3.1", + "name": "SomeProject", + "uri": "git+https://git.repo/some_pkg.git", + "subdirectory": null, + "path": null, + "hash_name": null, + "hash": null, + "extras": [], + "specs": [] + }, + { + "line": "-e SomeProject@git+https://git.repo/some_pkg.git@1.3.1", + "editable": true, + "local_file": false, + "specifier": false, + "vcs": "git", + "revision": "1.3.1", + "name": "SomeProject", + "uri": "git+https://git.repo/some_pkg.git", + "subdirectory": null, + "path": null, + "hash_name": null, + "hash": null, + "extras": [], + "specs": [] + } +] \ No newline at end of file diff --git a/tests/reqfiles/vcs_package_alias.txt b/tests/reqfiles/vcs_package_alias.txt new file mode 100644 index 0000000..5a81817 --- /dev/null +++ b/tests/reqfiles/vcs_package_alias.txt @@ -0,0 +1,7 @@ +SomeProject@git+https://git.repo/some_pkg.git@1.3.1 +SomeProject@git+https://git.repo/some_pkg.git@1.3.1#egg=EggName +SomeProject@ git+https://git.repo/some_pkg.git@1.3.1 +SomeProject @git+https://git.repo/some_pkg.git@1.3.1 +SomeProject @ git+https://git.repo/some_pkg.git@1.3.1 + +-e SomeProject@git+https://git.repo/some_pkg.git@1.3.1 \ No newline at end of file