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

Support for url zip subdirectories #398

Merged
merged 3 commits into from
Sep 2, 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 src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def create_dependency(
dependency = URLDependency(
name,
constraint["url"],
directory=constraint.get("subdirectory", None),
groups=groups,
optional=optional,
extras=constraint.get("extras", []),
Expand Down
4 changes: 4 additions & 0 deletions src/poetry/core/json/schemas/poetry-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@
"type": "string",
"description": "The url to the file."
},
"subdirectory": {
"type": "string",
"description": "The relative path to the directory where the package is located."
},
"python": {
"type": "string",
"description": "The python versions for which the dependency should be installed."
Expand Down
7 changes: 6 additions & 1 deletion src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,12 @@ def create_from_pep_508(
name, "git", link.url_without_fragment, extras=req.extras
)
elif link.scheme in ["http", "https"]:
dep = URLDependency(name, link.url, extras=req.extras)
dep = URLDependency(
name,
link.url_without_fragment,
directory=link.subdirectory_fragment,
extras=req.extras,
)
elif is_file_uri:
# handle RFC 8089 references
path = url_to_path(req.url)
Expand Down
1 change: 1 addition & 0 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ def to_dependency(self) -> Dependency:
dep = URLDependency(
self._name,
cast(str, self._source_url),
directory=self.source_subdirectory,
radoering marked this conversation as resolved.
Show resolved Hide resolved
groups=list(self._dependency_groups.keys()),
optional=self.optional,
extras=self.features,
Expand Down
11 changes: 11 additions & 0 deletions src/poetry/core/packages/url_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ def __init__(
self,
name: str,
url: str,
*,
directory: str | None = None,
radoering marked this conversation as resolved.
Show resolved Hide resolved
groups: Iterable[str] | None = None,
optional: bool = False,
extras: Iterable[str] | None = None,
) -> None:
self._url = url
self._directory = directory

parsed = urlparse(url)
if not parsed.scheme or not parsed.netloc:
Expand All @@ -29,13 +32,18 @@ def __init__(
allows_prereleases=True,
source_type="url",
source_url=self._url,
source_subdirectory=directory,
extras=extras,
)

@property
def url(self) -> str:
return self._url

@property
def directory(self) -> str | None:
return self._directory

@property
def base_pep_508_name(self) -> str:
requirement = self.pretty_name
Expand All @@ -46,6 +54,9 @@ def base_pep_508_name(self) -> str:

requirement += f" @ {self._url}"

if self.directory:
requirement += f"#subdirectory={self.directory}"

return requirement

def is_url(self) -> bool:
Expand Down
15 changes: 15 additions & 0 deletions tests/packages/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ def test_dependency_from_pep_508_with_url() -> None:
assert dep.url == "https://example.com/django-utils-1.0.0.tar.gz"


def test_dependency_from_pep_508_with_url_and_subdirectory() -> None:
name = (
"django-utils @"
" https://example.com/django-utils-1.0.0.tar.gz#subdirectory=django"
)

dep = Dependency.create_from_pep_508(name)

assert dep.name == "django-utils"
assert dep.is_url()
dep = cast(URLDependency, dep)
assert dep.url == "https://example.com/django-utils-1.0.0.tar.gz"
assert dep.directory == "django"


def test_dependency_from_pep_508_with_wheel_url() -> None:
name = (
"example_wheel @ https://example.com/example_wheel-14.0.2-py2.py3-none-any.whl"
Expand Down
2 changes: 2 additions & 0 deletions tests/packages/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def test_to_dependency_for_url() -> None:
"1.2.3",
source_type="url",
source_url="https://example.com/path.tar.gz",
source_subdirectory="qux",
features=["baz", "bar"],
)
dep = package.to_dependency()
Expand All @@ -345,6 +346,7 @@ def test_to_dependency_for_url() -> None:
assert dep.url == "https://example.com/path.tar.gz"
assert dep.source_type == "url"
assert dep.source_url == "https://example.com/path.tar.gz"
assert dep.source_subdirectory == "qux"


def test_to_dependency_for_vcs() -> None:
Expand Down
11 changes: 11 additions & 0 deletions tests/packages/test_url_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def test_to_pep_508_with_extras() -> None:
assert expected == dependency.to_pep_508()


def test_to_pep_508_with_subdirectory() -> None:
dependency = URLDependency(
"demo",
"https://github.com/foo/bar/archive/0.1.0.zip",
directory="baz",
)

expected = "demo @ https://github.com/foo/bar/archive/0.1.0.zip#subdirectory=baz"
assert expected == dependency.to_pep_508()


def test_to_pep_508_with_marker() -> None:
dependency = URLDependency(
"pytorch",
Expand Down