__init__.py 4.0 KB

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