freeze.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from __future__ import absolute_import
  2. import sys
  3. from pip._internal import index
  4. from pip._internal.basecommand import Command
  5. from pip._internal.cache import WheelCache
  6. from pip._internal.compat import stdlib_pkgs
  7. from pip._internal.operations.freeze import freeze
  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.cmd_opts.add_option(
  58. '--exclude-editable',
  59. dest='exclude_editable',
  60. action='store_true',
  61. help='Exclude editable package from output.')
  62. self.parser.insert_option_group(0, self.cmd_opts)
  63. def run(self, options, args):
  64. format_control = index.FormatControl(set(), set())
  65. wheel_cache = WheelCache(options.cache_dir, format_control)
  66. skip = set(stdlib_pkgs)
  67. if not options.freeze_all:
  68. skip.update(DEV_PKGS)
  69. freeze_kwargs = dict(
  70. requirement=options.requirements,
  71. find_links=options.find_links,
  72. local_only=options.local,
  73. user_only=options.user,
  74. skip_regex=options.skip_requirements_regex,
  75. isolated=options.isolated_mode,
  76. wheel_cache=wheel_cache,
  77. skip=skip,
  78. exclude_editable=options.exclude_editable,
  79. )
  80. try:
  81. for line in freeze(**freeze_kwargs):
  82. sys.stdout.write(line + '\n')
  83. finally:
  84. wheel_cache.cleanup()