freeze.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import sys
  2. from optparse import Values
  3. from typing import List
  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.operations.freeze import freeze
  8. from pip._internal.utils.compat import stdlib_pkgs
  9. from pip._internal.utils.deprecation import deprecated
  10. DEV_PKGS = {'pip', 'setuptools', 'distribute', 'wheel'}
  11. class FreezeCommand(Command):
  12. """
  13. Output installed packages in requirements format.
  14. packages are listed in a case-insensitive sorted order.
  15. """
  16. usage = """
  17. %prog [options]"""
  18. log_streams = ("ext://sys.stderr", "ext://sys.stderr")
  19. def add_options(self):
  20. # type: () -> None
  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(cmdoptions.list_path())
  52. self.cmd_opts.add_option(
  53. '--all',
  54. dest='freeze_all',
  55. action='store_true',
  56. help='Do not skip these packages in the output:'
  57. ' {}'.format(', '.join(DEV_PKGS)))
  58. self.cmd_opts.add_option(
  59. '--exclude-editable',
  60. dest='exclude_editable',
  61. action='store_true',
  62. help='Exclude editable package from output.')
  63. self.cmd_opts.add_option(cmdoptions.list_exclude())
  64. self.parser.insert_option_group(0, self.cmd_opts)
  65. def run(self, options, args):
  66. # type: (Values, List[str]) -> int
  67. skip = set(stdlib_pkgs)
  68. if not options.freeze_all:
  69. skip.update(DEV_PKGS)
  70. if options.excludes:
  71. skip.update(options.excludes)
  72. cmdoptions.check_list_path_option(options)
  73. if options.find_links:
  74. deprecated(
  75. "--find-links option in pip freeze is deprecated.",
  76. replacement=None,
  77. gone_in="21.2",
  78. issue=9069,
  79. )
  80. for line in freeze(
  81. requirement=options.requirements,
  82. find_links=options.find_links,
  83. local_only=options.local,
  84. user_only=options.user,
  85. paths=options.path,
  86. isolated=options.isolated_mode,
  87. skip=skip,
  88. exclude_editable=options.exclude_editable,
  89. ):
  90. sys.stdout.write(line + '\n')
  91. return SUCCESS