freeze.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from __future__ import absolute_import
  2. import sys
  3. from pip._internal.cache import WheelCache
  4. from pip._internal.cli import cmdoptions
  5. from pip._internal.cli.base_command import Command
  6. from pip._internal.cli.status_codes import SUCCESS
  7. from pip._internal.models.format_control import FormatControl
  8. from pip._internal.operations.freeze import freeze
  9. from pip._internal.utils.compat import stdlib_pkgs
  10. from pip._internal.utils.deprecation import deprecated
  11. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  12. DEV_PKGS = {'pip', 'setuptools', 'distribute', 'wheel'}
  13. if MYPY_CHECK_RUNNING:
  14. from optparse import Values
  15. from typing import List
  16. class FreezeCommand(Command):
  17. """
  18. Output installed packages in requirements format.
  19. packages are listed in a case-insensitive sorted order.
  20. """
  21. usage = """
  22. %prog [options]"""
  23. log_streams = ("ext://sys.stderr", "ext://sys.stderr")
  24. def add_options(self):
  25. # type: () -> None
  26. self.cmd_opts.add_option(
  27. '-r', '--requirement',
  28. dest='requirements',
  29. action='append',
  30. default=[],
  31. metavar='file',
  32. help="Use the order in the given requirements file and its "
  33. "comments when generating output. This option can be "
  34. "used multiple times.")
  35. self.cmd_opts.add_option(
  36. '-f', '--find-links',
  37. dest='find_links',
  38. action='append',
  39. default=[],
  40. metavar='URL',
  41. help='URL for finding packages, which will be added to the '
  42. 'output.')
  43. self.cmd_opts.add_option(
  44. '-l', '--local',
  45. dest='local',
  46. action='store_true',
  47. default=False,
  48. help='If in a virtualenv that has global access, do not output '
  49. 'globally-installed packages.')
  50. self.cmd_opts.add_option(
  51. '--user',
  52. dest='user',
  53. action='store_true',
  54. default=False,
  55. help='Only output packages installed in user-site.')
  56. self.cmd_opts.add_option(cmdoptions.list_path())
  57. self.cmd_opts.add_option(
  58. '--all',
  59. dest='freeze_all',
  60. action='store_true',
  61. help='Do not skip these packages in the output:'
  62. ' {}'.format(', '.join(DEV_PKGS)))
  63. self.cmd_opts.add_option(
  64. '--exclude-editable',
  65. dest='exclude_editable',
  66. action='store_true',
  67. help='Exclude editable package from output.')
  68. self.cmd_opts.add_option(cmdoptions.list_exclude())
  69. self.parser.insert_option_group(0, self.cmd_opts)
  70. def run(self, options, args):
  71. # type: (Values, List[str]) -> int
  72. format_control = FormatControl(set(), set())
  73. wheel_cache = WheelCache(options.cache_dir, format_control)
  74. skip = set(stdlib_pkgs)
  75. if not options.freeze_all:
  76. skip.update(DEV_PKGS)
  77. if options.excludes:
  78. skip.update(options.excludes)
  79. cmdoptions.check_list_path_option(options)
  80. if options.find_links:
  81. deprecated(
  82. "--find-links option in pip freeze is deprecated.",
  83. replacement=None,
  84. gone_in="21.2",
  85. issue=9069,
  86. )
  87. freeze_kwargs = dict(
  88. requirement=options.requirements,
  89. find_links=options.find_links,
  90. local_only=options.local,
  91. user_only=options.user,
  92. paths=options.path,
  93. isolated=options.isolated_mode,
  94. wheel_cache=wheel_cache,
  95. skip=skip,
  96. exclude_editable=options.exclude_editable,
  97. )
  98. for line in freeze(**freeze_kwargs):
  99. sys.stdout.write(line + '\n')
  100. return SUCCESS