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.

78 lines
2.9 KiB

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