freeze.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import sys
  2. from optparse import Values
  3. from pip._internal.cli import cmdoptions
  4. from pip._internal.cli.base_command import Command
  5. from pip._internal.cli.status_codes import SUCCESS
  6. from pip._internal.operations.freeze import freeze
  7. from pip._internal.utils.compat import stdlib_pkgs
  8. def _should_suppress_build_backends() -> bool:
  9. return sys.version_info < (3, 12)
  10. def _dev_pkgs() -> set[str]:
  11. pkgs = {"pip"}
  12. if _should_suppress_build_backends():
  13. pkgs |= {"setuptools", "distribute", "wheel"}
  14. return pkgs
  15. class FreezeCommand(Command):
  16. """
  17. Output installed packages in requirements format.
  18. packages are listed in a case-insensitive sorted order.
  19. """
  20. ignore_require_venv = True
  21. usage = """
  22. %prog [options]"""
  23. def add_options(self) -> None:
  24. self.cmd_opts.add_option(
  25. "-r",
  26. "--requirement",
  27. dest="requirements",
  28. action="append",
  29. default=[],
  30. metavar="file",
  31. help=(
  32. "Use the order in the given requirements file and its "
  33. "comments when generating output. This option can be "
  34. "used multiple times."
  35. ),
  36. )
  37. self.cmd_opts.add_option(
  38. "-l",
  39. "--local",
  40. dest="local",
  41. action="store_true",
  42. default=False,
  43. help=(
  44. "If in a virtualenv that has global access, do not output "
  45. "globally-installed packages."
  46. ),
  47. )
  48. self.cmd_opts.add_option(
  49. "--user",
  50. dest="user",
  51. action="store_true",
  52. default=False,
  53. help="Only output packages installed in user-site.",
  54. )
  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=(
  61. "Do not skip these packages in the output:"
  62. " {}".format(", ".join(_dev_pkgs()))
  63. ),
  64. )
  65. self.cmd_opts.add_option(
  66. "--exclude-editable",
  67. dest="exclude_editable",
  68. action="store_true",
  69. help="Exclude editable package from output.",
  70. )
  71. self.cmd_opts.add_option(cmdoptions.list_exclude())
  72. self.parser.insert_option_group(0, self.cmd_opts)
  73. def run(self, options: Values, args: list[str]) -> int:
  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. for line in freeze(
  81. requirement=options.requirements,
  82. local_only=options.local,
  83. user_only=options.user,
  84. paths=options.path,
  85. isolated=options.isolated_mode,
  86. skip=skip,
  87. exclude_editable=options.exclude_editable,
  88. ):
  89. sys.stdout.write(line + "\n")
  90. return SUCCESS