Skip to content

Commit

Permalink
remove unused code from Link
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Jun 2, 2022
1 parent 8379c76 commit 351519a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 136 deletions.
67 changes: 2 additions & 65 deletions src/poetry/core/packages/utils/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,27 @@
import re
import urllib.parse as urlparse

from typing import Any

from poetry.core.packages.utils.utils import path_to_url
from poetry.core.packages.utils.utils import splitext


class Link:
def __init__(
self,
url: str,
comes_from: Any | None = None,
requires_python: str | None = None,
metadata: str | bool | None = None,
) -> None:
def __init__(self, url: str) -> None:
"""
Object representing a parsed link from https://pypi.python.org/simple/*
url:
url of the resource pointed to (href of the link)
comes_from:
instance of HTMLPage where the link was found, or string.
requires_python:
String containing the `Requires-Python` metadata field, specified
in PEP 345. This may be specified by a data-requires-python
attribute in the HTML link tag, as described in PEP 503.
metadata:
String of the syntax `<hashname>=<hashvalue>` representing the hash
of the Core Metadata file. This may be specified by a
data-dist-info-metadata attribute in the HTML link tag, as described
in PEP 658.
"""

# url can be a UNC windows share
if url.startswith("\\\\"):
url = path_to_url(url)

self.url = url
self.comes_from = comes_from
self.requires_python = requires_python if requires_python else None

if isinstance(metadata, str):
metadata = {"true": True, "": False, "false": False}.get(
metadata.strip().lower(), metadata
)

self._metadata = metadata

def __str__(self) -> str:
if self.requires_python:
rp = f" (requires-python:{self.requires_python})"
else:
rp = ""
if self.comes_from:
return f"{self.url} (from {self.comes_from}){rp}"
else:
return str(self.url)
return str(self.url)

def __repr__(self) -> str:
return f"<Link {self!s}>"
Expand Down Expand Up @@ -149,34 +114,6 @@ def subdirectory_fragment(self) -> str | None:

_hash_re = re.compile(r"(sha1|sha224|sha384|sha256|sha512|md5)=([a-f0-9]+)")

@property
def has_metadata(self) -> bool:
if self._metadata is None:
return False
return bool(self._metadata) and (self.is_wheel or self.is_sdist)

@property
def metadata_url(self) -> str | None:
if self.has_metadata:
return f"{self.url_without_fragment.split('?', 1)[0]}.metadata"
return None

@property
def metadata_hash(self) -> str | None:
if self.has_metadata and isinstance(self._metadata, str):
match = self._hash_re.search(self._metadata)
if match:
return match.group(2)
return None

@property
def metadata_hash_name(self) -> str | None:
if self.has_metadata and isinstance(self._metadata, str):
match = self._hash_re.search(self._metadata)
if match:
return match.group(1)
return None

@property
def hash(self) -> str | None:
match = self._hash_re.search(self.url)
Expand Down
72 changes: 1 addition & 71 deletions tests/packages/utils/test_utils_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ def file_checksum() -> str:
return make_checksum()


@pytest.fixture()
def metadata_checksum() -> str:
return make_checksum()


def make_url(
ext: str, file_checksum: str | None = None, metadata_checksum: str | None = None
ext: str, file_checksum: str | None = None
) -> Link:
file_checksum = file_checksum or make_checksum()
return Link(
"https://files.pythonhosted.org/packages/16/52/dead/"
f"demo-1.0.0.{ext}#sha256={file_checksum}",
metadata=f"sha256={metadata_checksum}" if metadata_checksum else None,
)


Expand All @@ -40,12 +34,6 @@ def test_package_link_hash(file_checksum: str) -> None:
assert link.hash == file_checksum
assert link.show_url == "demo-1.0.0.whl"

# this is legacy PEP 503, no metadata hash is present
assert not link.has_metadata
assert not link.metadata_url
assert not link.metadata_hash
assert not link.metadata_hash_name


@pytest.mark.parametrize(
("ext", "check"),
Expand All @@ -60,61 +48,3 @@ def test_package_link_hash(file_checksum: str) -> None:
def test_package_link_is_checks(ext: str, check: str) -> None:
link = make_url(ext=ext)
assert getattr(link, f"is_{check}")


@pytest.mark.parametrize(
("ext", "has_metadata"),
[("whl", True), ("egg", False), ("tar.gz", True), ("zip", True)],
)
def test_package_link_pep658(
ext: str, has_metadata: bool, metadata_checksum: str
) -> None:
link = make_url(ext=ext, metadata_checksum=metadata_checksum)

if has_metadata:
assert link.has_metadata
assert link.metadata_url == f"{link.url_without_fragment}.metadata"
assert link.metadata_hash == metadata_checksum
assert link.metadata_hash_name == "sha256"
else:
assert not link.has_metadata
assert not link.metadata_url
assert not link.metadata_hash
assert not link.metadata_hash_name


def test_package_link_pep658_no_default_metadata() -> None:
link = make_url(ext="whl")

assert not link.has_metadata
assert not link.metadata_url
assert not link.metadata_hash
assert not link.metadata_hash_name


@pytest.mark.parametrize(
("metadata", "has_metadata"),
[
("true", True),
("false", False),
("", False),
],
)
def test_package_link_pep653_non_hash_metadata_value(
file_checksum: str, metadata: str | bool, has_metadata: bool
) -> None:
link = Link(
"https://files.pythonhosted.org/packages/16/52/dead/"
f"demo-1.0.0.whl#sha256={file_checksum}",
metadata=metadata,
)

if has_metadata:
assert link.has_metadata
assert link.metadata_url == f"{link.url_without_fragment}.metadata"
else:
assert not link.has_metadata
assert not link.metadata_url

assert not link.metadata_hash
assert not link.metadata_hash_name

0 comments on commit 351519a

Please sign in to comment.