Skip to content

Commit

Permalink
fix: write installer metadata (#3359)
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming authored Dec 26, 2024
1 parent 6ea9d55 commit e4eb940
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/3359.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write installer metadata like `INSTALLER` and `REQUESTED` to dist-info directory when installing packages.
8 changes: 8 additions & 0 deletions src/pdm/installers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dataclasses
from functools import cached_property
from itertools import chain
from typing import Collection, Iterable

from pdm import termui
Expand Down Expand Up @@ -90,6 +91,10 @@ def self_candidate(self) -> Candidate:
def candidates(self) -> dict[str, Candidate]:
"""Return the candidates to be installed"""
candidates = self.requested_candidates.copy()
requested = {
req.identify()
for req in (self.requirements or chain.from_iterable(self.environment.project.all_dependencies.values()))
}
if isinstance(self.no_editable, Collection):
keys = self.no_editable
elif self.no_editable:
Expand All @@ -107,6 +112,9 @@ def candidates(self) -> dict[str, Candidate]:
# Create a new candidate with editable=False
req = dataclasses.replace(candidate.req, editable=False)
candidates[key] = candidate.copy_with(req)
for key in requested:
if key in candidates:
candidates[key].requested = True
return candidates

def should_install_editables(self) -> bool:
Expand Down
5 changes: 4 additions & 1 deletion src/pdm/installers/installers.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def install_wheel(
direct_url: dict[str, Any] | None = None,
install_links: bool = False,
rename_pth: bool = False,
requested: bool = False,
) -> str:
"""Only create .pth files referring to the cached package.
If the cache doesn't exist, create one.
Expand All @@ -169,9 +170,11 @@ def install_wheel(
else:
link_method = _get_link_method(cache_method)

additional_metadata: dict[str, bytes] = {}
additional_metadata: dict[str, bytes] = {"INSTALLER": b"pdm"}
if direct_url is not None:
additional_metadata["direct_url.json"] = json.dumps(direct_url, indent=2).encode()
if requested:
additional_metadata["REQUESTED"] = b""

destination = InstallDestination(
scheme_dict=environment.get_paths(dist_name),
Expand Down
1 change: 1 addition & 0 deletions src/pdm/installers/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def install(self, candidate: Candidate) -> Distribution:
direct_url=prepared.direct_url(),
install_links=self.use_install_cache and not candidate.req.editable,
rename_pth=self.rename_pth,
requested=candidate.requested,
)
return Distribution.at(dist_info)

Expand Down
2 changes: 2 additions & 0 deletions src/pdm/models/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Candidate:
"link",
"name",
"req",
"requested",
"summary",
"version",
)
Expand All @@ -153,6 +154,7 @@ def __init__(
self.link = link
self.summary = ""
self.hashes: list[FileHash] = []
self.requested = False

self._requires_python: str | None = None
self._prepared: PreparedCandidate | None = None
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def build_env(build_env_wheels: Iterable[Path], tmp_path_factory: pytest.TempPat
p = Core().create_project(d)
env = PythonEnvironment(p, prefix=str(d), python=sys.executable)
for wheel in build_env_wheels:
install_wheel(wheel, env)
install_wheel(wheel, env, requested=True)
return d


Expand Down

0 comments on commit e4eb940

Please sign in to comment.