freeze.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from __future__ import absolute_import
  2. import sys
  3. import pip
  4. from pip.compat import stdlib_pkgs
  5. from pip.basecommand import Command
  6. from pip.operations.freeze import freeze
  7. from pip.wheel import WheelCache
  8. DEV_PKGS = ('pip', 'setuptools', 'distribute', 'wheel')
  9. class FreezeCommand(Command):
  10. """
  11. Output installed packages in requirements format.
  12. packages are listed in a case-insensitive sorted order.
  13. """
  14. name = 'freeze'
  15. usage = """
  16. %prog [options]"""
  17. summary = 'Output installed packages in requirements format.'
  18. log_streams = ("ext://sys.stderr", "ext://sys.stderr")
  19. def __init__(self, *args, **kw):
  20. super(FreezeCommand, 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="Use the order in the given requirements file and its "
  28. "comments when generating output. This option can be "
  29. "used multiple times.")
  30. self.cmd_opts.add_option(
  31. '-f', '--find-links',
  32. dest='find_links',
  33. action='append',
  34. default=[],
  35. metavar='URL',
  36. help='URL for finding packages, which will be added to the '
  37. 'output.')
  38. self.cmd_opts.add_option(
  39. '-l', '--local',
  40. dest='local',
  41. action='store_true',
  42. default=False,
  43. help='If in a virtualenv that has global access, do not output '
  44. 'globally-installed packages.')
  45. self.cmd_opts.add_option(
  46. '--user',
  47. dest='user',
  48. action='store_true',
  49. default=False,
  50. help='Only output packages installed in user-site.')
  51. self.cmd_opts.add_option(
  52. '--all',
  53. dest='freeze_all',
  54. action='store_true',
  55. help='Do not skip these packages in the output:'
  56. ' %s' % ', '.join(DEV_PKGS))
  57. self.parser.insert_option_group(0, self.cmd_opts)
  58. def run(self, options, args):
  59. format_control = pip.index.FormatControl(set(), set())
  60. wheel_cache = WheelCache(options.cache_dir, format_control)
  61. skip = set(stdlib_pkgs)
  62. if not options.freeze_all:
  63. skip.update(DEV_PKGS)
  64. freeze_kwargs = dict(
  65. requirement=options.requirements,
  66. find_links=options.find_links,
  67. local_only=options.local,
  68. user_only=options.user,
  69. skip_regex=options.skip_requirements_regex,
  70. isolated=options.isolated_mode,
  71. wheel_cache=wheel_cache,
  72. skip=skip)
  73. for line in freeze(**freeze_kwargs):
  74. sys.stdout.write(line + '\n')