uninstall.py 2.6 KB

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