freeze.py 2.7 KB

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