freeze.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import sys
  2. from optparse import Values
  3. from typing import AbstractSet, 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. def _should_suppress_build_backends() -> bool:
  10. return sys.version_info < (3, 12)
  11. def _dev_pkgs() -> AbstractSet[str]:
  12. pkgs = {"pip"}
  13. if _should_suppress_build_backends():
  14. pkgs |= {"setuptools", "distribute", "wheel"}
  15. return pkgs
  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) -> None:
  25. self.cmd_opts.add_option(
  26. "-r",
  27. "--requirement",
  28. dest="requirements",
  29. action="append",
  30. default=[],
  31. metavar="file",
  32. help=(
  33. "Use the order in the given requirements file and its "
  34. "comments when generating output. This option can be "
  35. "used multiple times."
  36. ),
  37. )
  38. self.cmd_opts.add_option(
  39. "-l",
  40. "--local",
  41. dest="local",
  42. action="store_true",
  43. default=False,
  44. help=(
  45. "If in a virtualenv that has global access, do not output "
  46. "globally-installed packages."
  47. ),
  48. )
  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. )
  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=(
  62. "Do not skip these packages in the output:"
  63. " {}".format(", ".join(_dev_pkgs()))
  64. ),
  65. )
  66. self.cmd_opts.add_option(
  67. "--exclude-editable",
  68. dest="exclude_editable",
  69. action="store_true",
  70. help="Exclude editable package from output.",
  71. )
  72. self.cmd_opts.add_option(cmdoptions.list_exclude())
  73. self.parser.insert_option_group(0, self.cmd_opts)
  74. def run(self, options: Values, args: List[str]) -> int:
  75. skip = set(stdlib_pkgs)
  76. if not options.freeze_all:
  77. skip.update(_dev_pkgs())
  78. if options.excludes:
  79. skip.update(options.excludes)
  80. cmdoptions.check_list_path_option(options)
  81. for line in freeze(
  82. requirement=options.requirements,
  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