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

confirming dependencies when uninstall package #4245

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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/4245.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
added confirming dependencies to ``uninstall`` command.
3 changes: 3 additions & 0 deletions src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pip._internal.basecommand import Command
from pip._internal.exceptions import InstallationError
from pip._internal.req import InstallRequirement, parse_requirements
from pip._internal.utils.misc import confirm_dependencies


class UninstallCommand(Command):
Expand Down Expand Up @@ -64,6 +65,8 @@ def run(self, options, args):
'"pip help %(name)s")' % dict(name=self.name)
)
for req in reqs_to_uninstall.values():
if not (options.yes or confirm_dependencies(req)):
continue
req.uninstall(
auto_confirm=options.yes, verbose=options.verbose != 0
)
Expand Down
16 changes: 16 additions & 0 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,19 @@ def enum(*sequential, **named):
reverse = dict((value, key) for key, value in enums.items())
enums['reverse_mapping'] = reverse
return type('Enum', (), enums)


def confirm_dependencies(req):
installed_distributions = get_installed_distributions()
dep_keys = set()
for dist in installed_distributions:
dep_keys.update(
[dist for d in dist.requires() if d.key == req.req.name])
if not dep_keys:
return True
logger.info(("The following packages depend on %s "
"and may break if it is uninstalled:") % req.req)
for dep in dep_keys:
logger.info(dep)
response = ask('Proceed (y/n)? ', ('y', 'n'))
return response == 'y'
37 changes: 37 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

import pytest
from mock import Mock, patch
from pip._vendor.packaging.requirements import Requirement
from pip._vendor.six import BytesIO

from pip._internal.exceptions import (
HashMismatch, HashMissing, InstallationError, UnsupportedPythonVersion
)
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.encoding import auto_decode
from pip._internal.utils.glibc import check_glibc_version
from pip._internal.utils.hashes import Hashes, MissingHashes
Expand Down Expand Up @@ -594,6 +596,41 @@ def test_check_requires(self, metadata, should_raise):
check_dist_requires_python(fake_dist)


@pytest.mark.parametrize(
"installed_requires,confirm_answer",
[
((), None),
(("dummy",), 'y'),
pytest.mark.xfail((("dummy",), 'n')),
],
)
def test_confirm_dependencies(installed_requires, confirm_answer):
from pip._internal.utils.misc import confirm_dependencies
patch_target = 'pip._internal.utils.misc.get_installed_distributions'
with patch(patch_target) as mock_gid:
with patch('pip._internal.utils.misc.ask') as mock_ask:
mock_ask.return_value = confirm_answer

class req(Requirement):
def __init__(self, key):
self.key = key

class installed(object):

def __init__(self, requires):
self._requires = [req(r) for r in requires]

def requires(self):
return self._requires

requirement = InstallRequirement(Requirement("dummy"), None)
installed_packages = [installed(installed_requires)]
mock_gid.return_value = installed_packages
assert confirm_dependencies(requirement)
if confirm_answer:
mock_ask.assert_called_with('Proceed (y/n)? ', ('y', 'n'))


class TestGetProg(object):

@pytest.mark.parametrize(
Expand Down