Skip to content

Commit

Permalink
rename from_metadata_file{,_contents}() to avoid ambiguity pointed ou…
Browse files Browse the repository at this point in the history
…t by reviewer
  • Loading branch information
cosmicexplorer committed Sep 5, 2022
1 parent 96bd60e commit 2aa1c2f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
15 changes: 8 additions & 7 deletions src/pip/_internal/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,22 @@ def get_wheel_distribution(wheel: Wheel, canonical_name: str) -> BaseDistributio


def get_metadata_distribution(
metadata_path: str,
metadata_contents: bytes,
filename: str,
canonical_name: str,
) -> BaseDistribution:
"""Get the representation of the specified METADATA file.
"""Get the dist representation of the specified METADATA file contents.
This returns a Distribution instance from the chosen backend based on the contents
of the file at ``metadata_path``.
This returns a Distribution instance from the chosen backend sourced from the data
in `metadata_contents`.
:param metadata_path: Path to the METADATA file.
:param metadata_contents: Contents of a METADATA file within a dist, or one served
via PEP 658.
:param filename: Filename for the dist this metadata represents.
:param canonical_name: Normalized project name of the given dist.
"""
return select_backend().Distribution.from_metadata_file(
metadata_path,
return select_backend().Distribution.from_metadata_file_contents(
metadata_contents,
filename,
canonical_name,
)
9 changes: 6 additions & 3 deletions src/pip/_internal/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,18 @@ def from_directory(cls, directory: str) -> "BaseDistribution":
raise NotImplementedError()

@classmethod
def from_metadata_file(
def from_metadata_file_contents(
cls,
metadata_path: str,
metadata_contents: bytes,
filename: str,
project_name: str,
) -> "BaseDistribution":
"""Load the distribution from the contents of a METADATA file.
:param metadata: The path to a METADATA file.
This is used to implement PEP 658 by generating a "shallow" dist object that can
be used for resolution without downloading or building the actual dist yet.
:param metadata_contents: The contents of a METADATA file.
:param filename: File name for the dist with this metadata.
:param project_name: Name of the project this dist represents.
"""
Expand Down
18 changes: 13 additions & 5 deletions src/pip/_internal/metadata/importlib/_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from pip._internal.utils.misc import normalize_path
from pip._internal.utils.packaging import safe_extra
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.wheel import parse_wheel, read_wheel_metadata_file

from ._compat import BasePath, get_dist_name
Expand Down Expand Up @@ -110,15 +111,22 @@ def from_directory(cls, directory: str) -> BaseDistribution:
return cls(dist, info_location, info_location.parent)

@classmethod
def from_metadata_file(
def from_metadata_file_contents(
cls,
metadata_path: str,
metadata_contents: bytes,
filename: str,
project_name: str,
) -> BaseDistribution:
metadata_location = pathlib.Path(metadata_path)
dist = importlib.metadata.Distribution.at(metadata_location.parent)
return cls(dist, metadata_location.parent, None)
# Generate temp dir to contain the metadata file, and write the file contents.
temp_dir = pathlib.Path(
TempDirectory(kind="metadata", globally_managed=True).path
)
metadata_path = temp_dir / "METADATA"
with open(metadata_path, "wb") as f:
f.write(metadata_contents)
# Construct dist pointing to the newly created directory.
dist = importlib.metadata.Distribution.at(metadata_path.parent)
return cls(dist, metadata_path.parent, None)

@classmethod
def from_wheel(cls, wheel: Wheel, name: str) -> BaseDistribution:
Expand Down
12 changes: 5 additions & 7 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,18 @@ def from_directory(cls, directory: str) -> BaseDistribution:
return cls(dist)

@classmethod
def from_metadata_file(
def from_metadata_file_contents(
cls,
metadata_path: str,
metadata_contents: bytes,
filename: str,
project_name: str,
) -> BaseDistribution:
with open(metadata_path, "rb") as f:
metadata = f.read()
metadata_text = {
"METADATA": metadata,
metadata_dict = {
"METADATA": metadata_contents,
}
dist = pkg_resources.DistInfoDistribution(
location=filename,
metadata=InMemoryMetadata(metadata_text, filename),
metadata=InMemoryMetadata(metadata_dict, filename),
project_name=project_name,
)
return cls(dist)
Expand Down
12 changes: 5 additions & 7 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,17 @@ def _fetch_metadata_using_link_data_attr(
req.req,
metadata_link,
)
# Download the contents of the METADATA file, separate from the dist itself.
metadata_file = get_http_url(
metadata_link,
self._download,
hashes=metadata_link.as_hashes(),
)
# The file will be downloaded under the same name as it's listed in the index,
# which will end with .metadata. To make importlib.metadata.PathDistribution
# work, we need to keep it in the same directory, but rename it to METADATA.
containing_dir = os.path.dirname(metadata_file.path)
new_metadata_path = os.path.join(containing_dir, "METADATA")
os.rename(metadata_file.path, new_metadata_path)
with open(metadata_file.path, "rb") as f:
metadata_contents = f.read()
# Generate a dist just from those file contents.
return get_metadata_distribution(
new_metadata_path,
metadata_contents,
req.link.filename,
req.req.name,
)
Expand Down

0 comments on commit 2aa1c2f

Please sign in to comment.