__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """
  2. Package containing all pip commands
  3. """
  4. import importlib
  5. from collections import namedtuple
  6. from typing import Any, Dict, Optional
  7. from pip._internal.cli.base_command import Command
  8. CommandInfo = namedtuple("CommandInfo", "module_path, class_name, summary")
  9. # This dictionary does a bunch of heavy lifting for help output:
  10. # - Enables avoiding additional (costly) imports for presenting `--help`.
  11. # - The ordering matters for help display.
  12. #
  13. # Even though the module path starts with the same "pip._internal.commands"
  14. # prefix, the full path makes testing easier (specifically when modifying
  15. # `commands_dict` in test setup / teardown).
  16. commands_dict: Dict[str, CommandInfo] = {
  17. "install": CommandInfo(
  18. "pip._internal.commands.install",
  19. "InstallCommand",
  20. "Install packages.",
  21. ),
  22. "download": CommandInfo(
  23. "pip._internal.commands.download",
  24. "DownloadCommand",
  25. "Download packages.",
  26. ),
  27. "uninstall": CommandInfo(
  28. "pip._internal.commands.uninstall",
  29. "UninstallCommand",
  30. "Uninstall packages.",
  31. ),
  32. "freeze": CommandInfo(
  33. "pip._internal.commands.freeze",
  34. "FreezeCommand",
  35. "Output installed packages in requirements format.",
  36. ),
  37. "inspect": CommandInfo(
  38. "pip._internal.commands.inspect",
  39. "InspectCommand",
  40. "Inspect the python environment.",
  41. ),
  42. "list": CommandInfo(
  43. "pip._internal.commands.list",
  44. "ListCommand",
  45. "List installed packages.",
  46. ),
  47. "show": CommandInfo(
  48. "pip._internal.commands.show",
  49. "ShowCommand",
  50. "Show information about installed packages.",
  51. ),
  52. "check": CommandInfo(
  53. "pip._internal.commands.check",
  54. "CheckCommand",
  55. "Verify installed packages have compatible dependencies.",
  56. ),
  57. "config": CommandInfo(
  58. "pip._internal.commands.configuration",
  59. "ConfigurationCommand",
  60. "Manage local and global configuration.",
  61. ),
  62. "search": CommandInfo(
  63. "pip._internal.commands.search",
  64. "SearchCommand",
  65. "Search PyPI for packages.",
  66. ),
  67. "cache": CommandInfo(
  68. "pip._internal.commands.cache",
  69. "CacheCommand",
  70. "Inspect and manage pip's wheel cache.",
  71. ),
  72. "index": CommandInfo(
  73. "pip._internal.commands.index",
  74. "IndexCommand",
  75. "Inspect information available from package indexes.",
  76. ),
  77. "wheel": CommandInfo(
  78. "pip._internal.commands.wheel",
  79. "WheelCommand",
  80. "Build wheels from your requirements.",
  81. ),
  82. "hash": CommandInfo(
  83. "pip._internal.commands.hash",
  84. "HashCommand",
  85. "Compute hashes of package archives.",
  86. ),
  87. "completion": CommandInfo(
  88. "pip._internal.commands.completion",
  89. "CompletionCommand",
  90. "A helper command used for command completion.",
  91. ),
  92. "debug": CommandInfo(
  93. "pip._internal.commands.debug",
  94. "DebugCommand",
  95. "Show information useful for debugging.",
  96. ),
  97. "help": CommandInfo(
  98. "pip._internal.commands.help",
  99. "HelpCommand",
  100. "Show help for commands.",
  101. ),
  102. }
  103. def create_command(name: str, **kwargs: Any) -> Command:
  104. """
  105. Create an instance of the Command class with the given name.
  106. """
  107. module_path, class_name, summary = commands_dict[name]
  108. module = importlib.import_module(module_path)
  109. command_class = getattr(module, class_name)
  110. command = command_class(name=name, summary=summary, **kwargs)
  111. return command
  112. def get_similar_commands(name: str) -> Optional[str]:
  113. """Command name auto-correct."""
  114. from difflib import get_close_matches
  115. name = name.lower()
  116. close_commands = get_close_matches(name, commands_dict.keys())
  117. if close_commands:
  118. return close_commands[0]
  119. else:
  120. return None