You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uninstall.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  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
  7. from pip._internal.exceptions import InstallationError
  8. from pip._internal.req import parse_requirements
  9. from pip._internal.req.constructors import install_req_from_line
  10. from pip._internal.utils.misc import protect_pip_from_modification_on_windows
  11. class UninstallCommand(Command, SessionCommandMixin):
  12. """
  13. Uninstall packages.
  14. pip is able to uninstall most installed packages. Known exceptions are:
  15. - Pure distutils packages installed with ``python setup.py install``, which
  16. leave behind no metadata to determine what files were installed.
  17. - Script wrappers installed by ``python setup.py develop``.
  18. """
  19. usage = """
  20. %prog [options] <package> ...
  21. %prog [options] -r <requirements file> ..."""
  22. def __init__(self, *args, **kw):
  23. super(UninstallCommand, self).__init__(*args, **kw)
  24. self.cmd_opts.add_option(
  25. '-r', '--requirement',
  26. dest='requirements',
  27. action='append',
  28. default=[],
  29. metavar='file',
  30. help='Uninstall all the packages listed in the given requirements '
  31. 'file. This option can be used multiple times.',
  32. )
  33. self.cmd_opts.add_option(
  34. '-y', '--yes',
  35. dest='yes',
  36. action='store_true',
  37. help="Don't ask for confirmation of uninstall deletions.")
  38. self.parser.insert_option_group(0, self.cmd_opts)
  39. def run(self, options, args):
  40. session = self.get_default_session(options)
  41. reqs_to_uninstall = {}
  42. for name in args:
  43. req = install_req_from_line(
  44. name, isolated=options.isolated_mode,
  45. )
  46. if req.name:
  47. reqs_to_uninstall[canonicalize_name(req.name)] = req
  48. for filename in options.requirements:
  49. for req in parse_requirements(
  50. filename,
  51. options=options,
  52. session=session):
  53. if req.name:
  54. reqs_to_uninstall[canonicalize_name(req.name)] = req
  55. if not reqs_to_uninstall:
  56. raise InstallationError(
  57. 'You must give at least one requirement to %(name)s (see '
  58. '"pip help %(name)s")' % dict(name=self.name)
  59. )
  60. protect_pip_from_modification_on_windows(
  61. modifying_pip="pip" in reqs_to_uninstall
  62. )
  63. for req in reqs_to_uninstall.values():
  64. uninstall_pathset = req.uninstall(
  65. auto_confirm=options.yes, verbose=self.verbosity > 0,
  66. )
  67. if uninstall_pathset:
  68. uninstall_pathset.commit()