uninstall.py 3.8 KB

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