Skip to content

Commit

Permalink
lock: use relativate paths for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Feb 9, 2025
1 parent 89a029f commit d124afc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/pip/_internal/commands/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from optparse import Values
from pathlib import Path
from typing import List

from pip._internal.cache import WheelCache
Expand Down Expand Up @@ -131,7 +132,7 @@ def run(self, options: Values, args: List[str]) -> int:
requirement_set = resolver.resolve(reqs, check_supported_wheels=True)

pyproject_lock = Pylock.from_install_requirements(
requirement_set.requirements.values()
requirement_set.requirements.values(), base_dir=Path.cwd()
)
sys.stdout.write(pyproject_lock.as_toml())

Expand Down
13 changes: 9 additions & 4 deletions src/pip/_internal/models/pylock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, Iterable, List, Literal, Optional, Tuple

import tomli_w
Expand Down Expand Up @@ -80,7 +81,7 @@ class Package:
# (not supported) tool

@classmethod
def from_install_requirement(cls, ireq: InstallRequirement) -> Self:
def from_install_requirement(cls, ireq: InstallRequirement, base_dir: Path) -> Self:
assert ireq.name
dist = ireq.get_dist()
download_info = ireq.download_info
Expand All @@ -98,7 +99,11 @@ def from_install_requirement(cls, ireq: InstallRequirement) -> Self:
)
elif isinstance(download_info.info, DirInfo):
package.directory = PackageDirectory(
path=url_to_path(download_info.url),
path=(
Path(url_to_path(download_info.url))
.relative_to(base_dir, walk_up=True)
.as_posix()
),
editable=(
download_info.info.editable
if download_info.info.editable
Expand Down Expand Up @@ -157,12 +162,12 @@ def as_toml(self) -> str:

@classmethod
def from_install_requirements(
cls, install_requirements: Iterable[InstallRequirement]
cls, install_requirements: Iterable[InstallRequirement], base_dir: Path
) -> Self:
return cls(
packages=sorted(
(
Package.from_install_requirement(ireq)
Package.from_install_requirement(ireq, base_dir)
for ireq in install_requirements
),
key=lambda p: p.name,
Expand Down

0 comments on commit d124afc

Please sign in to comment.