freeze.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import sys
  5. from pip._internal.cache import WheelCache
  6. from pip._internal.cli import cmdoptions
  7. from pip._internal.cli.base_command import Command
  8. from pip._internal.models.format_control import FormatControl
  9. from pip._internal.operations.freeze import freeze
  10. from pip._internal.utils.compat import stdlib_pkgs
  11. DEV_PKGS = {'pip', 'setuptools', 'distribute', 'wheel'}
  12. class FreezeCommand(Command):
  13. """
  14. Output installed packages in requirements format.
  15. packages are listed in a case-insensitive sorted order.
  16. """
  17. usage = """
  18. %prog [options]"""
  19. log_streams = ("ext://sys.stderr", "ext://sys.stderr")
  20. def __init__(self, *args, **kw):
  21. super(FreezeCommand, 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="Use the order in the given requirements file and its "
  29. "comments when generating output. This option can be "
  30. "used multiple times.")
  31. self.cmd_opts.add_option(
  32. '-f', '--find-links',
  33. dest='find_links',
  34. action='append',
  35. default=[],
  36. metavar='URL',
  37. help='URL for finding packages, which will be added to the '
  38. 'output.')
  39. self.cmd_opts.add_option(
  40. '-l', '--local',
  41. dest='local',
  42. action='store_true',
  43. default=False,
  44. help='If in a virtualenv that has global access, do not output '
  45. 'globally-installed packages.')
  46. self.cmd_opts.add_option(
  47. '--user',
  48. dest='user',
  49. action='store_true',
  50. default=False,
  51. help='Only output packages installed in user-site.')
  52. self.cmd_opts.add_option(cmdoptions.list_path())
  53. self.cmd_opts.add_option(
  54. '--all',
  55. dest='freeze_all',
  56. action='store_true',
  57. help='Do not skip these packages in the output:'
  58. ' %s' % ', '.join(DEV_PKGS))
  59. self.cmd_opts.add_option(
  60. '--exclude-editable',
  61. dest='exclude_editable',
  62. action='store_true',
  63. help='Exclude editable package from output.')
  64. self.parser.insert_option_group(0, self.cmd_opts)
  65. def run(self, options, args):
  66. format_control = FormatControl(set(), set())
  67. wheel_cache = WheelCache(options.cache_dir, format_control)
  68. skip = set(stdlib_pkgs)
  69. if not options.freeze_all:
  70. skip.update(DEV_PKGS)
  71. cmdoptions.check_list_path_option(options)
  72. freeze_kwargs = dict(
  73. requirement=options.requirements,
  74. find_links=options.find_links,
  75. local_only=options.local,
  76. user_only=options.user,
  77. paths=options.path,
  78. skip_regex=options.skip_requirements_regex,
  79. isolated=options.isolated_mode,
  80. wheel_cache=wheel_cache,
  81. skip=skip,
  82. exclude_editable=options.exclude_editable,
  83. )
  84. try:
  85. for line in freeze(**freeze_kwargs):
  86. sys.stdout.write(line + '\n')
  87. finally:
  88. wheel_cache.cleanup()