Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - normalize cwd path on dist path check (#4088) #4098

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions newsfragments/4088.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed path comparison in ``easy_install`` via normalization.
(This affects only deprecated calls to ``python setup.py install/develop``,
users are still encouraged to use ``python -m build`` or ``pip install -e .``
with build isolation).
31 changes: 18 additions & 13 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,26 +1247,29 @@ def update_pth(self, dist): # noqa: C901 # is too complex (11) # FIXME
if self.pth_file is None:
return

dist_location = normalize_path(dist.location)

for d in self.pth_file[dist.key]: # drop old entries
if not self.multi_version and d.location == dist.location:
d_location = normalize_path(d.location)
if not self.multi_version and d_location == dist_location:
continue

log.info("Removing %s from easy-install.pth file", d)
self.pth_file.remove(d)
if d.location in self.shadow_path:
self.shadow_path.remove(d.location)
if d_location in map(normalize_path, self.shadow_path):
self.shadow_path.remove(d_location)

if not self.multi_version:
if dist.location in self.pth_file.paths:
if dist_location in self.pth_file.paths:
log.info(
"%s is already the active version in easy-install.pth",
dist,
)
else:
log.info("Adding %s to easy-install.pth file", dist)
self.pth_file.add(dist) # add new entry
if dist.location not in self.shadow_path:
self.shadow_path.append(dist.location)
if dist_location not in map(normalize_path, self.shadow_path):
self.shadow_path.append(dist_location)

if self.dry_run:
return
Expand All @@ -1282,7 +1285,7 @@ def update_pth(self, dist): # noqa: C901 # is too complex (11) # FIXME
if os.path.islink(filename):
os.unlink(filename)
with open(filename, 'wt') as f:
f.write(self.pth_file.make_relative(dist.location) + '\n')
f.write(self.pth_file.make_relative(dist_location) + '\n')

def unpack_progress(self, src, dst):
# Progress filter for unpacking
Expand Down Expand Up @@ -1713,21 +1716,23 @@ def _wrap_lines(lines):

def add(self, dist):
"""Add `dist` to the distribution map"""
new_path = dist.location not in self.paths and (
dist.location not in self.sitedirs
dist_location = normalize_path(dist.location)
new_path = dist_location not in map(normalize_path, self.paths) and (
dist_location not in map(normalize_path, self.sitedirs)
or
# account for '.' being in PYTHONPATH
dist.location == os.getcwd()
dist_location == normalize_path(os.getcwd())
)
if new_path:
self.paths.append(dist.location)
self.paths.append(dist_location)
self.dirty = True
super().add(dist)

def remove(self, dist):
"""Remove `dist` from the distribution map"""
while dist.location in self.paths:
self.paths.remove(dist.location)
dist_location = normalize_path(dist.location)
while dist_location in map(normalize_path, self.paths):
self.paths.remove(dist_location)
self.dirty = True
super().remove(dist)

Expand Down
24 changes: 24 additions & 0 deletions setuptools/tests/test_easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,30 @@ def test_add_from_site_is_ignored(self):
pth.add(PRDistribution(location))
assert not pth.dirty

def test_add_remove_for_normalized_path(self):
"""
In windows, path is not case sensitive,
This is test when the distribution path is cwd and normalized.
"""
if sys.platform.startswith('win'): # pragma: no cover
path_org = 'C:/Location/package'
else:
path_org = '/Location/package'
path_norm = pkg_resources.normalize_path(path_org)

pth = PthDistributions('test-package', [path_org])
dist = PRDistribution(location=path_norm, version='1.0')

with mock.patch('os.getcwd') as mock_getcwd:
mock_getcwd.return_value = path_org
assert not pth.dirty
pth.add(dist)
assert pth.dirty

assert len(pth.paths) == 1
pth.remove(dist)
assert len(pth.paths) == 0

def test_many_pth_distributions_merge_together(self, tmpdir):
"""
If the pth file is modified under the hood, then PthDistribution
Expand Down
Loading