uninstall.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import logging
  2. from optparse import Values
  3. from typing import List
  4. from pip._vendor.packaging.utils import canonicalize_name
  5. from pip._internal.cli import cmdoptions
  6. from pip._internal.cli.base_command import Command
  7. from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root
  8. from pip._internal.cli.status_codes import SUCCESS
  9. from pip._internal.exceptions import InstallationError
  10. from pip._internal.req import parse_requirements
  11. from pip._internal.req.constructors import (
  12. install_req_from_line,
  13. install_req_from_parsed_requirement,
  14. )
  15. from pip._internal.utils.misc import protect_pip_from_modification_on_windows
  16. logger = logging.getLogger(__name__)
  17. class UninstallCommand(Command, SessionCommandMixin):
  18. """
  19. Uninstall packages.
  20. pip is able to uninstall most installed packages. Known exceptions are:
  21. - Pure distutils packages installed with ``python setup.py install``, which
  22. leave behind no metadata to determine what files were installed.
  23. - Script wrappers installed by ``python setup.py develop``.
  24. """
  25. usage = """
  26. %prog [options] <package> ...
  27. %prog [options] -r <requirements file> ..."""
  28. def add_options(self) -> None:
  29. self.cmd_opts.add_option(
  30. "-r",
  31. "--requirement",
  32. dest="requirements",
  33. action="append",
  34. default=[],
  35. metavar="file",
  36. help=(
  37. "Uninstall all the packages listed in the given requirements "
  38. "file. This option can be used multiple times."
  39. ),
  40. )
  41. self.cmd_opts.add_option(
  42. "-y",
  43. "--yes",
  44. dest="yes",
  45. action="store_true",
  46. help="Don't ask for confirmation of uninstall deletions.",
  47. )
  48. self.cmd_opts.add_option(cmdoptions.root_user_action())
  49. self.parser.insert_option_group(0, self.cmd_opts)
  50. def run(self, options: Values, args: List[str]) -> int:
  51. session = self.get_default_session(options)
  52. reqs_to_uninstall = {}
  53. for name in args:
  54. req = install_req_from_line(
  55. name,
  56. isolated=options.isolated_mode,
  57. )
  58. if req.name:
  59. reqs_to_uninstall[canonicalize_name(req.name)] = req
  60. else:
  61. logger.warning(
  62. "Invalid requirement: %r ignored -"
  63. " the uninstall command expects named"
  64. " requirements.",
  65. name,
  66. )
  67. for filename in options.requirements:
  68. for parsed_req in parse_requirements(
  69. filename, options=options, session=session
  70. ):
  71. req = install_req_from_parsed_requirement(
  72. parsed_req, isolated=options.isolated_mode
  73. )
  74. if req.name:
  75. reqs_to_uninstall[canonicalize_name(req.name)] = req
  76. if not reqs_to_uninstall:
  77. raise InstallationError(
  78. f"You must give at least one requirement to {self.name} (see "
  79. f'"pip help {self.name}")'
  80. )
  81. protect_pip_from_modification_on_windows(
  82. modifying_pip="pip" in reqs_to_uninstall
  83. )
  84. for req in reqs_to_uninstall.values():
  85. uninstall_pathset = req.uninstall(
  86. auto_confirm=options.yes,
  87. verbose=self.verbosity > 0,
  88. )
  89. if uninstall_pathset:
  90. uninstall_pathset.commit()
  91. if options.root_user_action == "warn":
  92. warn_if_run_as_root()
  93. return SUCCESS