Skip to content

Commit

Permalink
Parse hash data before passing to MetadataFile
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmoore committed Jun 8, 2023
1 parent 93b274e commit 232cc9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 16 additions & 8 deletions src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ class MetadataFile:

hashes: Optional[Dict[str, str]]

# TODO: Do we care about stripping out unsupported hash methods?
def __init__(self, hashes: Optional[Dict[str, str]]):
if hashes:
hashes = {n: v for n, v in hashes.items() if n in _SUPPORTED_HASHES}
# We need to use this as this is a frozen dataclass
object.__setattr__(self, "hashes", hashes)
def __post_init__(self) -> None:
if self.hashes is not None:
assert all(name in _SUPPORTED_HASHES for name in self.hashes)


def supported_hashes(hashes: Optional[Dict[str, str]]) -> Optional[Dict[str, str]]:
# Remove any unsupported hash types from the mapping. If this leaves no
# supported hashes, return None
if hashes is None:
return None
hashes = {n: v for n, v in hashes.items() if n in _SUPPORTED_HASHES}
if len(hashes) > 0:
return hashes
return None


def _clean_url_path_part(part: str) -> str:
Expand Down Expand Up @@ -273,7 +281,7 @@ def from_json(
metadata_info = file_data.get("dist-info-metadata", False)
if isinstance(metadata_info, dict):
# The file exists, and hashes have been supplied
metadata_file_data = MetadataFile(metadata_info)
metadata_file_data = MetadataFile(supported_hashes(metadata_info))
elif metadata_info:
# The file exists, but there are no hashes
metadata_file_data = MetadataFile(None)
Expand Down Expand Up @@ -328,7 +336,7 @@ def from_element(
# The file exists, and hashes have been supplied
hashname, sep, hashval = metadata_info.partition("=")
if sep == "=":
metadata_file_data = MetadataFile({hashname: hashval})
metadata_file_data = MetadataFile(supported_hashes({hashname: hashval}))
else:
# Error - data is wrong. Treat as no hashes supplied.
logger.debug(
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,10 @@ def test_link_hash_parsing(url: str, result: Optional[LinkHash]) -> None:
[
("sha256=aa113592bbe", MetadataFile({"sha256": "aa113592bbe"})),
("sha256=", MetadataFile({"sha256": ""})),
("sha500=aa113592bbe", MetadataFile({})),
("sha500=aa113592bbe", MetadataFile(None)),
("true", MetadataFile(None)),
(None, None),
# TODO: Are these correct?
# Attribute is present but invalid
("", MetadataFile(None)),
("aa113592bbe", MetadataFile(None)),
],
Expand All @@ -1104,4 +1104,3 @@ def test_metadata_file_info_parsing_html(
base_url = "https://index.url/simple"
link = Link.from_element(attribs, page_url, base_url)
assert link is not None and link.metadata_file_data == expected
# TODO: Do we need to do something for the JSON data?

0 comments on commit 232cc9d

Please sign in to comment.