__init__.py 3.6 KB

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