uninstall.py 2.2 KB

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