uninstall.py 2.7 KB

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