__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """
  2. Package containing all pip commands
  3. """
  4. import importlib
  5. from collections import OrderedDict, namedtuple
  6. from typing import Any, Optional
  7. from pip._internal.cli.base_command import Command
  8. CommandInfo = namedtuple('CommandInfo', 'module_path, class_name, summary')
  9. # The ordering matters for help display.
  10. # Also, even though the module path starts with the same
  11. # "pip._internal.commands" prefix in each case, we include the full path
  12. # because it makes testing easier (specifically when modifying commands_dict
  13. # in test setup / teardown by adding info for a FakeCommand class defined
  14. # in a test-related module).
  15. # Finally, we need to pass an iterable of pairs here rather than a dict
  16. # so that the ordering won't be lost when using Python 2.7.
  17. commands_dict = OrderedDict([
  18. ('install', CommandInfo(
  19. 'pip._internal.commands.install', 'InstallCommand',
  20. 'Install packages.',
  21. )),
  22. ('download', CommandInfo(
  23. 'pip._internal.commands.download', 'DownloadCommand',
  24. 'Download packages.',
  25. )),
  26. ('uninstall', CommandInfo(
  27. 'pip._internal.commands.uninstall', 'UninstallCommand',
  28. 'Uninstall packages.',
  29. )),
  30. ('freeze', CommandInfo(
  31. 'pip._internal.commands.freeze', 'FreezeCommand',
  32. 'Output installed packages in requirements format.',
  33. )),
  34. ('list', CommandInfo(
  35. 'pip._internal.commands.list', 'ListCommand',
  36. 'List installed packages.',
  37. )),
  38. ('show', CommandInfo(
  39. 'pip._internal.commands.show', 'ShowCommand',
  40. 'Show information about installed packages.',
  41. )),
  42. ('check', CommandInfo(
  43. 'pip._internal.commands.check', 'CheckCommand',
  44. 'Verify installed packages have compatible dependencies.',
  45. )),
  46. ('config', CommandInfo(
  47. 'pip._internal.commands.configuration', 'ConfigurationCommand',
  48. 'Manage local and global configuration.',
  49. )),
  50. ('search', CommandInfo(
  51. 'pip._internal.commands.search', 'SearchCommand',
  52. 'Search PyPI for packages.',
  53. )),
  54. ('cache', CommandInfo(
  55. 'pip._internal.commands.cache', 'CacheCommand',
  56. "Inspect and manage pip's wheel cache.",
  57. )),
  58. ('wheel', CommandInfo(
  59. 'pip._internal.commands.wheel', 'WheelCommand',
  60. 'Build wheels from your requirements.',
  61. )),
  62. ('hash', CommandInfo(
  63. 'pip._internal.commands.hash', 'HashCommand',
  64. 'Compute hashes of package archives.',
  65. )),
  66. ('completion', CommandInfo(
  67. 'pip._internal.commands.completion', 'CompletionCommand',
  68. 'A helper command used for command completion.',
  69. )),
  70. ('debug', CommandInfo(
  71. 'pip._internal.commands.debug', 'DebugCommand',
  72. 'Show information useful for debugging.',
  73. )),
  74. ('help', CommandInfo(
  75. 'pip._internal.commands.help', 'HelpCommand',
  76. 'Show help for commands.',
  77. )),
  78. ]) # type: OrderedDict[str, CommandInfo]
  79. def create_command(name, **kwargs):
  80. # type: (str, **Any) -> Command
  81. """
  82. Create an instance of the Command class with the given name.
  83. """
  84. module_path, class_name, summary = commands_dict[name]
  85. module = importlib.import_module(module_path)
  86. command_class = getattr(module, class_name)
  87. command = command_class(name=name, summary=summary, **kwargs)
  88. return command
  89. def get_similar_commands(name):
  90. # type: (str) -> Optional[str]
  91. """Command name auto-correct."""
  92. from difflib import get_close_matches
  93. name = name.lower()
  94. close_commands = get_close_matches(name, commands_dict.keys())
  95. if close_commands:
  96. return close_commands[0]
  97. else:
  98. return None