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

Make failed uninstalls roll back more reliably and better at avoiding naming conflicts #6225

Merged
merged 6 commits into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions news/6194.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make failed uninstalls roll back more reliably and better at avoiding naming conflicts.
18 changes: 16 additions & 2 deletions src/pip/_internal/req/req_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import (
FakeFile, ask, dist_in_usersite, dist_is_local, egg_link_path, is_local,
normalize_path, renames,
normalize_path, renames, rmtree,
)
from pip._internal.utils.temp_dir import AdjacentTempDirectory

Expand Down Expand Up @@ -241,7 +241,10 @@ def _stash(self, path):
best = AdjacentTempDirectory(os.path.dirname(path))
best.create()
self._save_dirs.append(best)
return os.path.join(best.path, os.path.relpath(path, best.original))
relpath = os.path.relpath(path, best.original)
if not relpath or relpath == os.path.curdir:
return best.path
return os.path.join(best.path, relpath)

def remove(self, auto_confirm=False, verbose=False):
"""Remove paths in ``self.paths`` with confirmation (unless
Expand All @@ -265,6 +268,13 @@ def remove(self, auto_confirm=False, verbose=False):
new_path = self._stash(path)
logger.debug('Removing file or directory %s', path)
self._moved_paths.append((path, new_path))
if os.path.isdir(path) and os.path.isdir(new_path):
# If we're moving a directory, we need to
# remove the destination first or else it will be
# moved to inside the existing directory.
# We just created new_path ourselves, so it will
# be removable.
os.rmdir(new_path)
renames(path, new_path)
for pth in self.pth.values():
pth.remove()
Expand Down Expand Up @@ -311,9 +321,13 @@ def rollback(self):
logger.info('Rolling back uninstall of %s', self.dist.project_name)
for path, tmp_path in self._moved_paths:
logger.debug('Replacing %s', path)
if os.path.isdir(tmp_path) and os.path.isdir(path):
rmtree(path)
renames(tmp_path, path)
for pth in self.pth.values():
pth.rollback()
for save_dir in self._save_dirs:
save_dir.cleanup()

def commit(self):
"""Remove temporary save dir: rollback will no longer be possible."""
Expand Down
10 changes: 7 additions & 3 deletions src/pip/_internal/utils/temp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ class AdjacentTempDirectory(TempDirectory):

"""
# The characters that may be used to name the temp directory
LEADING_CHARS = "-~.+=%0123456789"
# We always prepend a ~ and then rotate through these until
# a usable name is found.
# pkg_resources raises a different error for .dist-info folder
# with leading '-' and invalid metadata
LEADING_CHARS = "-~.=%0123456789"

def __init__(self, original, delete=None):
super(AdjacentTempDirectory, self).__init__(delete=delete)
Expand All @@ -117,8 +121,8 @@ def _generate_names(cls, name):
if name[i] in cls.LEADING_CHARS:
continue
for candidate in itertools.combinations_with_replacement(
cls.LEADING_CHARS, i):
new_name = ''.join(candidate) + name[i:]
cls.LEADING_CHARS, i - 1):
new_name = '~' + ''.join(candidate) + name[i:]
if new_name != name:
yield new_name

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,9 @@ def names():
assert not any(n == name for n in names())

# Check the first group are correct
assert all(x == y for x, y in
zip(some_names, [c + name[1:] for c in chars]))
for x, y in zip(some_names, ['~' + name[1:]] +
['~' + c + name[2:] for c in chars]):
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
assert x == y


class TestGlibc(object):
Expand Down