freeze.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ignore_require_venv = True
  22. usage = """
  23. %prog [options]"""
  24. log_streams = ("ext://sys.stderr", "ext://sys.stderr")
  25. def add_options(self) -> None:
  26. self.cmd_opts.add_option(
  27. "-r",
  28. "--requirement",
  29. dest="requirements",
  30. action="append",
  31. default=[],
  32. metavar="file",
  33. help=(
  34. "Use the order in the given requirements file and its "
  35. "comments when generating output. This option can be "
  36. "used multiple times."
  37. ),
  38. )
  39. self.cmd_opts.add_option(
  40. "-l",
  41. "--local",
  42. dest="local",
  43. action="store_true",
  44. default=False,
  45. help=(
  46. "If in a virtualenv that has global access, do not output "
  47. "globally-installed packages."
  48. ),
  49. )
  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. )
  57. self.cmd_opts.add_option(cmdoptions.list_path())
  58. self.cmd_opts.add_option(
  59. "--all",
  60. dest="freeze_all",
  61. action="store_true",
  62. help=(
  63. "Do not skip these packages in the output:"
  64. " {}".format(", ".join(_dev_pkgs()))
  65. ),
  66. )
  67. self.cmd_opts.add_option(
  68. "--exclude-editable",
  69. dest="exclude_editable",
  70. action="store_true",
  71. help="Exclude editable package from output.",
  72. )
  73. self.cmd_opts.add_option(cmdoptions.list_exclude())
  74. self.parser.insert_option_group(0, self.cmd_opts)
  75. def run(self, options: Values, args: List[str]) -> int:
  76. skip = set(stdlib_pkgs)
  77. if not options.freeze_all:
  78. skip.update(_dev_pkgs())
  79. if options.excludes:
  80. skip.update(options.excludes)
  81. cmdoptions.check_list_path_option(options)
  82. for line in freeze(
  83. requirement=options.requirements,
  84. local_only=options.local,
  85. user_only=options.user,
  86. paths=options.path,
  87. isolated=options.isolated_mode,
  88. skip=skip,
  89. exclude_editable=options.exclude_editable,
  90. ):
  91. sys.stdout.write(line + "\n")
  92. return SUCCESS