Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/commands/uninstall.py
4804 views
import logging1from optparse import Values2from typing import List34from pip._vendor.packaging.utils import canonicalize_name56from pip._internal.cli import cmdoptions7from pip._internal.cli.base_command import Command8from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root9from pip._internal.cli.status_codes import SUCCESS10from pip._internal.exceptions import InstallationError11from pip._internal.req import parse_requirements12from pip._internal.req.constructors import (13install_req_from_line,14install_req_from_parsed_requirement,15)16from pip._internal.utils.misc import protect_pip_from_modification_on_windows1718logger = logging.getLogger(__name__)192021class UninstallCommand(Command, SessionCommandMixin):22"""23Uninstall packages.2425pip is able to uninstall most installed packages. Known exceptions are:2627- Pure distutils packages installed with ``python setup.py install``, which28leave behind no metadata to determine what files were installed.29- Script wrappers installed by ``python setup.py develop``.30"""3132usage = """33%prog [options] <package> ...34%prog [options] -r <requirements file> ..."""3536def add_options(self) -> None:37self.cmd_opts.add_option(38"-r",39"--requirement",40dest="requirements",41action="append",42default=[],43metavar="file",44help=(45"Uninstall all the packages listed in the given requirements "46"file. This option can be used multiple times."47),48)49self.cmd_opts.add_option(50"-y",51"--yes",52dest="yes",53action="store_true",54help="Don't ask for confirmation of uninstall deletions.",55)56self.cmd_opts.add_option(cmdoptions.root_user_action())57self.parser.insert_option_group(0, self.cmd_opts)5859def run(self, options: Values, args: List[str]) -> int:60session = self.get_default_session(options)6162reqs_to_uninstall = {}63for name in args:64req = install_req_from_line(65name,66isolated=options.isolated_mode,67)68if req.name:69reqs_to_uninstall[canonicalize_name(req.name)] = req70else:71logger.warning(72"Invalid requirement: %r ignored -"73" the uninstall command expects named"74" requirements.",75name,76)77for filename in options.requirements:78for parsed_req in parse_requirements(79filename, options=options, session=session80):81req = install_req_from_parsed_requirement(82parsed_req, isolated=options.isolated_mode83)84if req.name:85reqs_to_uninstall[canonicalize_name(req.name)] = req86if not reqs_to_uninstall:87raise InstallationError(88f"You must give at least one requirement to {self.name} (see "89f'"pip help {self.name}")'90)9192protect_pip_from_modification_on_windows(93modifying_pip="pip" in reqs_to_uninstall94)9596for req in reqs_to_uninstall.values():97uninstall_pathset = req.uninstall(98auto_confirm=options.yes,99verbose=self.verbosity > 0,100)101if uninstall_pathset:102uninstall_pathset.commit()103if options.root_user_action == "warn":104warn_if_run_as_root()105return SUCCESS106107108