uninstall.py 3.4 KB

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