freeze.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from __future__ import absolute_import
  2. import sys
  3. from pip.basecommand import Command
  4. from pip.operations.freeze import freeze
  5. class FreezeCommand(Command):
  6. """
  7. Output installed packages in requirements format.
  8. packages are listed in a case-insensitive sorted order.
  9. """
  10. name = 'freeze'
  11. usage = """
  12. %prog [options]"""
  13. summary = 'Output installed packages in requirements format.'
  14. log_streams = ("ext://sys.stderr", "ext://sys.stderr")
  15. def __init__(self, *args, **kw):
  16. super(FreezeCommand, self).__init__(*args, **kw)
  17. self.cmd_opts.add_option(
  18. '-r', '--requirement',
  19. dest='requirement',
  20. action='store',
  21. default=None,
  22. metavar='file',
  23. help="Use the order in the given requirements file and its "
  24. "comments when generating output.")
  25. self.cmd_opts.add_option(
  26. '-f', '--find-links',
  27. dest='find_links',
  28. action='append',
  29. default=[],
  30. metavar='URL',
  31. help='URL for finding packages, which will be added to the '
  32. 'output.')
  33. self.cmd_opts.add_option(
  34. '-l', '--local',
  35. dest='local',
  36. action='store_true',
  37. default=False,
  38. help='If in a virtualenv that has global access, do not output '
  39. 'globally-installed packages.')
  40. self.cmd_opts.add_option(
  41. '--user',
  42. dest='user',
  43. action='store_true',
  44. default=False,
  45. help='Only output packages installed in user-site.')
  46. self.parser.insert_option_group(0, self.cmd_opts)
  47. def run(self, options, args):
  48. freeze_kwargs = dict(
  49. requirement=options.requirement,
  50. find_links=options.find_links,
  51. local_only=options.local,
  52. user_only=options.user,
  53. skip_regex=options.skip_requirements_regex,
  54. isolated=options.isolated_mode)
  55. for line in freeze(**freeze_kwargs):
  56. sys.stdout.write(line + '\n')