freeze.py 3.4 KB

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