3
3
"""
4
4
from __future__ import absolute_import
5
5
6
- from pip ._internal .commands .completion import CompletionCommand
7
- from pip ._internal .commands .configuration import ConfigurationCommand
8
- from pip ._internal .commands .debug import DebugCommand
9
- from pip ._internal .commands .download import DownloadCommand
10
- from pip ._internal .commands .freeze import FreezeCommand
11
- from pip ._internal .commands .hash import HashCommand
12
- from pip ._internal .commands .help import HelpCommand
13
- from pip ._internal .commands .list import ListCommand
14
- from pip ._internal .commands .check import CheckCommand
15
- from pip ._internal .commands .search import SearchCommand
16
- from pip ._internal .commands .show import ShowCommand
17
- from pip ._internal .commands .install import InstallCommand
18
- from pip ._internal .commands .uninstall import UninstallCommand
19
- from pip ._internal .commands .wheel import WheelCommand
6
+ import importlib
7
+ from collections import namedtuple , OrderedDict
20
8
21
9
from pip ._internal .utils .typing import MYPY_CHECK_RUNNING
22
10
23
11
if MYPY_CHECK_RUNNING :
24
- from typing import List , Type
12
+ from typing import Any
25
13
from pip ._internal .cli .base_command import Command
26
14
27
- commands_order = [
28
- InstallCommand ,
29
- DownloadCommand ,
30
- UninstallCommand ,
31
- FreezeCommand ,
32
- ListCommand ,
33
- ShowCommand ,
34
- CheckCommand ,
35
- ConfigurationCommand ,
36
- SearchCommand ,
37
- WheelCommand ,
38
- HashCommand ,
39
- CompletionCommand ,
40
- DebugCommand ,
41
- HelpCommand ,
42
- ] # type: List[Type[Command]]
43
15
44
- commands_dict = {c .name : c for c in commands_order }
45
-
46
-
47
- def get_summaries (ordered = True ):
48
- """Yields sorted (command name, command summary) tuples."""
49
-
50
- if ordered :
51
- cmditems = _sort_commands (commands_dict , commands_order )
52
- else :
53
- cmditems = commands_dict .items ()
54
-
55
- for name , command_class in cmditems :
56
- yield (name , command_class .summary )
16
+ CommandInfo = namedtuple ('CommandInfo' , 'module_path, class_name, summary' )
17
+
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
+ ('wheel' , CommandInfo (
64
+ 'pip._internal.commands.wheel' , 'WheelCommand' ,
65
+ 'Build wheels from your requirements.' ,
66
+ )),
67
+ ('hash' , CommandInfo (
68
+ 'pip._internal.commands.hash' , 'HashCommand' ,
69
+ 'Compute hashes of package archives.' ,
70
+ )),
71
+ ('completion' , CommandInfo (
72
+ 'pip._internal.commands.completion' , 'CompletionCommand' ,
73
+ 'A helper command used for command completion.' ,
74
+ )),
75
+ ('debug' , CommandInfo (
76
+ 'pip._internal.commands.debug' , 'DebugCommand' ,
77
+ 'Show information useful for debugging.' ,
78
+ )),
79
+ ('help' , CommandInfo (
80
+ 'pip._internal.commands.help' , 'HelpCommand' ,
81
+ 'Show help for commands.' ,
82
+ )),
83
+ ]) # type: OrderedDict[str, CommandInfo]
84
+
85
+
86
+ def create_command (name , ** kwargs ):
87
+ # type: (str, **Any) -> Command
88
+ """
89
+ Create an instance of the Command class with the given name.
90
+ """
91
+ module_path , class_name , summary = commands_dict [name ]
92
+ module = importlib .import_module (module_path )
93
+ command_class = getattr (module , class_name )
94
+ command = command_class (name = name , summary = summary , ** kwargs )
95
+
96
+ return command
57
97
58
98
59
99
def get_similar_commands (name ):
@@ -68,14 +108,3 @@ def get_similar_commands(name):
68
108
return close_commands [0 ]
69
109
else :
70
110
return False
71
-
72
-
73
- def _sort_commands (cmddict , order ):
74
- def keyfn (key ):
75
- try :
76
- return order .index (key [1 ])
77
- except ValueError :
78
- # unordered items should come last
79
- return 0xff
80
-
81
- return sorted (cmddict .items (), key = keyfn )
0 commit comments