Skip to content

Commit 2aa1c2f

Browse files
rename from_metadata_file{,_contents}() to avoid ambiguity pointed out by reviewer
1 parent 96bd60e commit 2aa1c2f

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

src/pip/_internal/metadata/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,22 @@ def get_wheel_distribution(wheel: Wheel, canonical_name: str) -> BaseDistributio
106106

107107

108108
def get_metadata_distribution(
109-
metadata_path: str,
109+
metadata_contents: bytes,
110110
filename: str,
111111
canonical_name: str,
112112
) -> BaseDistribution:
113-
"""Get the representation of the specified METADATA file.
113+
"""Get the dist representation of the specified METADATA file contents.
114114
115-
This returns a Distribution instance from the chosen backend based on the contents
116-
of the file at ``metadata_path``.
115+
This returns a Distribution instance from the chosen backend sourced from the data
116+
in `metadata_contents`.
117117
118-
:param metadata_path: Path to the METADATA file.
118+
:param metadata_contents: Contents of a METADATA file within a dist, or one served
119+
via PEP 658.
119120
:param filename: Filename for the dist this metadata represents.
120121
:param canonical_name: Normalized project name of the given dist.
121122
"""
122-
return select_backend().Distribution.from_metadata_file(
123-
metadata_path,
123+
return select_backend().Distribution.from_metadata_file_contents(
124+
metadata_contents,
124125
filename,
125126
canonical_name,
126127
)

src/pip/_internal/metadata/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,18 @@ def from_directory(cls, directory: str) -> "BaseDistribution":
114114
raise NotImplementedError()
115115

116116
@classmethod
117-
def from_metadata_file(
117+
def from_metadata_file_contents(
118118
cls,
119-
metadata_path: str,
119+
metadata_contents: bytes,
120120
filename: str,
121121
project_name: str,
122122
) -> "BaseDistribution":
123123
"""Load the distribution from the contents of a METADATA file.
124124
125-
:param metadata: The path to a METADATA file.
125+
This is used to implement PEP 658 by generating a "shallow" dist object that can
126+
be used for resolution without downloading or building the actual dist yet.
127+
128+
:param metadata_contents: The contents of a METADATA file.
126129
:param filename: File name for the dist with this metadata.
127130
:param project_name: Name of the project this dist represents.
128131
"""

src/pip/_internal/metadata/importlib/_dists.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from pip._internal.utils.misc import normalize_path
3030
from pip._internal.utils.packaging import safe_extra
31+
from pip._internal.utils.temp_dir import TempDirectory
3132
from pip._internal.utils.wheel import parse_wheel, read_wheel_metadata_file
3233

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

112113
@classmethod
113-
def from_metadata_file(
114+
def from_metadata_file_contents(
114115
cls,
115-
metadata_path: str,
116+
metadata_contents: bytes,
116117
filename: str,
117118
project_name: str,
118119
) -> BaseDistribution:
119-
metadata_location = pathlib.Path(metadata_path)
120-
dist = importlib.metadata.Distribution.at(metadata_location.parent)
121-
return cls(dist, metadata_location.parent, None)
120+
# Generate temp dir to contain the metadata file, and write the file contents.
121+
temp_dir = pathlib.Path(
122+
TempDirectory(kind="metadata", globally_managed=True).path
123+
)
124+
metadata_path = temp_dir / "METADATA"
125+
with open(metadata_path, "wb") as f:
126+
f.write(metadata_contents)
127+
# Construct dist pointing to the newly created directory.
128+
dist = importlib.metadata.Distribution.at(metadata_path.parent)
129+
return cls(dist, metadata_path.parent, None)
122130

123131
@classmethod
124132
def from_wheel(cls, wheel: Wheel, name: str) -> BaseDistribution:

src/pip/_internal/metadata/pkg_resources.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,18 @@ def from_directory(cls, directory: str) -> BaseDistribution:
9393
return cls(dist)
9494

9595
@classmethod
96-
def from_metadata_file(
96+
def from_metadata_file_contents(
9797
cls,
98-
metadata_path: str,
98+
metadata_contents: bytes,
9999
filename: str,
100100
project_name: str,
101101
) -> BaseDistribution:
102-
with open(metadata_path, "rb") as f:
103-
metadata = f.read()
104-
metadata_text = {
105-
"METADATA": metadata,
102+
metadata_dict = {
103+
"METADATA": metadata_contents,
106104
}
107105
dist = pkg_resources.DistInfoDistribution(
108106
location=filename,
109-
metadata=InMemoryMetadata(metadata_text, filename),
107+
metadata=InMemoryMetadata(metadata_dict, filename),
110108
project_name=project_name,
111109
)
112110
return cls(dist)

src/pip/_internal/operations/prepare.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,19 +374,17 @@ def _fetch_metadata_using_link_data_attr(
374374
req.req,
375375
metadata_link,
376376
)
377+
# Download the contents of the METADATA file, separate from the dist itself.
377378
metadata_file = get_http_url(
378379
metadata_link,
379380
self._download,
380381
hashes=metadata_link.as_hashes(),
381382
)
382-
# The file will be downloaded under the same name as it's listed in the index,
383-
# which will end with .metadata. To make importlib.metadata.PathDistribution
384-
# work, we need to keep it in the same directory, but rename it to METADATA.
385-
containing_dir = os.path.dirname(metadata_file.path)
386-
new_metadata_path = os.path.join(containing_dir, "METADATA")
387-
os.rename(metadata_file.path, new_metadata_path)
383+
with open(metadata_file.path, "rb") as f:
384+
metadata_contents = f.read()
385+
# Generate a dist just from those file contents.
388386
return get_metadata_distribution(
389-
new_metadata_path,
387+
metadata_contents,
390388
req.link.filename,
391389
req.req.name,
392390
)

0 commit comments

Comments
 (0)