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.
 
 
 
 

77 lines
2.8 KiB

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