2019-09-28 20:12:49 +02:00
|
|
|
# The following comment should be removed at some point in the future.
|
|
|
|
# mypy: disallow-untyped-defs=False
|
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-04-02 12:54:43 +02:00
|
|
|
from pip._vendor.packaging.utils import canonicalize_name
|
|
|
|
|
2018-07-30 06:02:47 +02:00
|
|
|
from pip._internal.cli.base_command import Command
|
2019-08-07 22:04:02 +02:00
|
|
|
from pip._internal.cli.req_command import SessionCommandMixin
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.exceptions import InstallationError
|
2018-08-21 17:07:40 +02:00
|
|
|
from pip._internal.req import parse_requirements
|
|
|
|
from pip._internal.req.constructors import install_req_from_line
|
2018-04-20 11:49:06 +02:00
|
|
|
from pip._internal.utils.misc import protect_pip_from_modification_on_windows
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2019-09-03 00:48:15 +02:00
|
|
|
class UninstallCommand(Command, SessionCommandMixin):
|
2013-02-16 19:00:29 +01:00
|
|
|
"""
|
|
|
|
Uninstall packages.
|
|
|
|
|
|
|
|
pip is able to uninstall most installed packages. Known exceptions are:
|
|
|
|
|
|
|
|
- Pure distutils packages installed with ``python setup.py install``, which
|
|
|
|
leave behind no metadata to determine what files were installed.
|
|
|
|
- Script wrappers installed by ``python setup.py develop``.
|
|
|
|
"""
|
2019-07-10 09:36:33 +02:00
|
|
|
|
2013-01-18 22:25:15 +01:00
|
|
|
usage = """
|
|
|
|
%prog [options] <package> ...
|
|
|
|
%prog [options] -r <requirements file> ..."""
|
2009-11-20 19:20:52 +01:00
|
|
|
|
2012-11-10 05:15:45 +01:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super(UninstallCommand, self).__init__(*args, **kw)
|
2012-12-10 02:29:35 +01:00
|
|
|
self.cmd_opts.add_option(
|
2009-11-20 19:20:52 +01:00
|
|
|
'-r', '--requirement',
|
|
|
|
dest='requirements',
|
|
|
|
action='append',
|
|
|
|
default=[],
|
2013-01-18 22:25:15 +01:00
|
|
|
metavar='file',
|
2014-01-27 15:07:10 +01:00
|
|
|
help='Uninstall all the packages listed in the given requirements '
|
|
|
|
'file. This option can be used multiple times.',
|
|
|
|
)
|
2012-12-10 02:29:35 +01:00
|
|
|
self.cmd_opts.add_option(
|
2009-11-20 19:20:52 +01:00
|
|
|
'-y', '--yes',
|
|
|
|
dest='yes',
|
|
|
|
action='store_true',
|
|
|
|
help="Don't ask for confirmation of uninstall deletions.")
|
|
|
|
|
2012-12-12 14:06:05 +01:00
|
|
|
self.parser.insert_option_group(0, self.cmd_opts)
|
2012-12-10 02:29:35 +01:00
|
|
|
|
2009-11-20 19:20:52 +01:00
|
|
|
def run(self, options, args):
|
2019-09-03 00:48:15 +02:00
|
|
|
session = self.get_default_session(options)
|
|
|
|
|
|
|
|
reqs_to_uninstall = {}
|
|
|
|
for name in args:
|
|
|
|
req = install_req_from_line(
|
|
|
|
name, isolated=options.isolated_mode,
|
|
|
|
)
|
|
|
|
if req.name:
|
|
|
|
reqs_to_uninstall[canonicalize_name(req.name)] = req
|
|
|
|
for filename in options.requirements:
|
|
|
|
for req in parse_requirements(
|
|
|
|
filename,
|
|
|
|
options=options,
|
|
|
|
session=session):
|
2017-04-02 12:54:43 +02:00
|
|
|
if req.name:
|
|
|
|
reqs_to_uninstall[canonicalize_name(req.name)] = req
|
2019-09-03 00:48:15 +02:00
|
|
|
if not reqs_to_uninstall:
|
|
|
|
raise InstallationError(
|
|
|
|
'You must give at least one requirement to %(name)s (see '
|
|
|
|
'"pip help %(name)s")' % dict(name=self.name)
|
2018-04-20 11:49:06 +02:00
|
|
|
)
|
|
|
|
|
2019-09-03 00:48:15 +02:00
|
|
|
protect_pip_from_modification_on_windows(
|
|
|
|
modifying_pip="pip" in reqs_to_uninstall
|
|
|
|
)
|
|
|
|
|
|
|
|
for req in reqs_to_uninstall.values():
|
|
|
|
uninstall_pathset = req.uninstall(
|
|
|
|
auto_confirm=options.yes, verbose=self.verbosity > 0,
|
|
|
|
)
|
|
|
|
if uninstall_pathset:
|
|
|
|
uninstall_pathset.commit()
|