uninstall.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 (
  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 __init__(self, *args, **kw):
  26. super(UninstallCommand, self).__init__(*args, **kw)
  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. session = self.get_default_session(options)
  44. reqs_to_uninstall = {}
  45. for name in args:
  46. req = install_req_from_line(
  47. name, isolated=options.isolated_mode,
  48. )
  49. if req.name:
  50. reqs_to_uninstall[canonicalize_name(req.name)] = req
  51. for filename in options.requirements:
  52. for parsed_req in parse_requirements(
  53. filename,
  54. options=options,
  55. session=session):
  56. req = install_req_from_parsed_requirement(
  57. parsed_req,
  58. isolated=options.isolated_mode
  59. )
  60. if req.name:
  61. reqs_to_uninstall[canonicalize_name(req.name)] = req
  62. if not reqs_to_uninstall:
  63. raise InstallationError(
  64. 'You must give at least one requirement to {self.name} (see '
  65. '"pip help {self.name}")'.format(**locals())
  66. )
  67. protect_pip_from_modification_on_windows(
  68. modifying_pip="pip" in reqs_to_uninstall
  69. )
  70. for req in reqs_to_uninstall.values():
  71. uninstall_pathset = req.uninstall(
  72. auto_confirm=options.yes, verbose=self.verbosity > 0,
  73. )
  74. if uninstall_pathset:
  75. uninstall_pathset.commit()