Skip to content

Commit

Permalink
Fix - extra normalization of dist location (#4088)
Browse files Browse the repository at this point in the history
  • Loading branch information
spxd committed Oct 31, 2023
1 parent 8df6d6d commit 2d71a42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
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 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 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 self.paths and (
dist_location not in self.sitedirs
or
# account for '.' being in PYTHONPATH
dist.location == normalize_path(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 self.paths:
self.paths.remove(dist_location)
self.dirty = True
super().remove(dist)

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

def test_add_for_normalize_cwd_path(self):
def test_add_remove_for_normalized_path(self):
"""
In windows, path is not case sensitive,
This is test when the distiribution path is cwd and normalized.
"""
path_org = 'C:/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
pth = PthDistributions('test-package', [path_org])
dist = PRDistribution(path_norm)
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

0 comments on commit 2d71a42

Please sign in to comment.