Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode the requirments.txt from ireq #1037

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1036.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug of missing subdirectory fragment when importing from a `requirements.txt`.
23 changes: 11 additions & 12 deletions pdm/formats/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import urllib.parse
from argparse import Namespace
from os import PathLike
from typing import Any, Mapping, cast

from pip._vendor.packaging.requirements import Requirement as PRequirement
from typing import Any, Mapping

from pdm.formats.base import make_array
from pdm.models.candidates import Candidate
Expand All @@ -18,8 +16,9 @@
from pdm.utils import expand_env_vars_in_auth, get_finder


def _requirement_to_str_lowercase_name(requirement: PRequirement) -> str:
def _requirement_to_str_lowercase_name(requirement: InstallRequirement) -> str:
"""Formats a packaging.requirements.Requirement with a lowercase name."""
assert requirement.name
parts = [requirement.name.lower()]

if requirement.extras:
Expand All @@ -28,11 +27,12 @@ def _requirement_to_str_lowercase_name(requirement: PRequirement) -> str:
if requirement.specifier:
parts.append(str(requirement.specifier))

if requirement.url:
parts.append("@ {0}".format(requirement.url))

if requirement.marker:
parts.append("; {0}".format(requirement.marker))
if requirement.link:
parts.append("@ {0}".format(requirement.link.url_without_fragment))
if requirement.link.subdirectory_fragment:
parts.append(
"#subdirectory={0}".format(requirement.link.subdirectory_fragment)
)

return "".join(parts)

Expand All @@ -56,9 +56,8 @@ def ireq_as_line(ireq: InstallRequirement, environment: Environment) -> str:
req.name = Candidate(req).prepare(environment).metadata.metadata["Name"]
ireq.req = req # type: ignore

line = _requirement_to_str_lowercase_name(cast(PRequirement, ireq.req))
assert ireq.req
if not ireq.req.marker and ireq.markers:
line = _requirement_to_str_lowercase_name(ireq)
if ireq.markers:
line = f"{line}; {ireq.markers}"

return line
Expand Down
4 changes: 2 additions & 2 deletions pdm/models/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def from_req_dict(cls, name: str, req_dict: RequirementDict) -> "Requirement":
for vcs in VCS_SCHEMA:
if vcs in req_dict:
repo = cast(str, req_dict.pop(vcs, None))
url = vcs + "+" + repo
url = f"{vcs}+{repo}"
return VcsRequirement.create(name=name, vcs=vcs, url=url, **req_dict)
if "path" in req_dict or "url" in req_dict:
return FileRequirement.create(name=name, **req_dict)
Expand Down Expand Up @@ -396,7 +396,7 @@ def as_line(self) -> str:
project_name = f"{self.project_name}" if self.project_name else ""
extras = f"[{','.join(sorted(self.extras))}]" if self.extras else ""
marker = self._format_marker()
url = url_without_fragments(self.url)
url = self.url
if self.revision and not self.editable:
url += f"@{self.revision}"
elif self.ref:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ werkzeug==0.16.0
whoosh==2.7.4; sys_platform == "win32"
wtforms==2.2.1 --hash=sha256:0cdbac3e7f6878086c334aa25dc5a33869a3954e9d1e015130d65a69309b3b61 --hash=sha256:e3ee092c827582c50877cdbd49e9ce6d2c5c1f6561f849b3b068c1b8029626f1
-e git+https://github.com/pypa/pip.git@main#egg=pip
git+https://github.com/techalchemy/test-project.git@master#egg=pep508-package&subdirectory=parent_folder/pep508-package
4 changes: 4 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def test_convert_requirements_file(project, is_dev):
assert "webassets==2.0" in group
assert 'whoosh==2.7.4; sys_platform == "win32"' in group
assert "-e git+https://github.com/pypa/pip.git@main#egg=pip" in group
assert (
"pep508-package@ git+https://github.com/techalchemy/test-project.git"
"@master#subdirectory=parent_folder/pep508-package" in group
)


def test_convert_requirements_file_without_name(project, vcs):
Expand Down