uninstall.py 3.1 KB

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