133
139
def _builtin_commands():
134
140
import bzrlib.builtins
141
return _scan_module_for_commands(bzrlib.builtins)
144
def _scan_module_for_commands(module):
136
builtins = bzrlib.builtins.__dict__
137
for name in builtins:
146
for name, obj in module.__dict__.iteritems():
138
147
if name.startswith("cmd_"):
139
148
real_name = _unsquish_command_name(name)
140
r[real_name] = builtins[name]
153
def _list_bzr_commands(names):
154
"""Find commands from bzr's core and plugins."""
155
# to eliminate duplicates
156
names.update(builtin_command_names())
157
names.update(plugin_command_names())
161
def all_command_names():
162
"""Return a set of all command names."""
164
for hook in Command.hooks['list_commands']:
167
raise AssertionError(
168
'hook %s returned None' % Command.hooks.get_hook_name(hook))
144
172
def builtin_command_names():
145
"""Return list of builtin command names."""
173
"""Return list of builtin command names.
175
Use of all_command_names() is encouraged rather than builtin_command_names
176
and/or plugin_command_names.
146
178
return _builtin_commands().keys()
149
181
def plugin_command_names():
182
"""Returns command names from commands registered by plugins."""
150
183
return plugin_cmds.keys()
153
def _get_cmd_dict(plugins_override=True):
154
"""Return name->class mapping for all commands."""
186
@deprecated_function(deprecated_in((1, 17, 0)))
187
def get_all_cmds(plugins_override=False):
188
"""Return canonical name and class for most commands.
190
NB: This does not return all commands since the introduction of
191
command hooks, and returning the class is not sufficient to
192
get correctly setup commands, which is why it is deprecated.
194
Use 'all_command_names' + 'get_cmd_object' instead.
155
196
d = _builtin_commands()
156
197
if plugins_override:
157
198
d.update(plugin_cmds.iteritems())
161
def get_all_cmds(plugins_override=True):
162
"""Return canonical name and class for all registered commands."""
163
for k, v in _get_cmd_dict(plugins_override=plugins_override).iteritems():
199
for k, v in d.iteritems():
167
203
def get_cmd_object(cmd_name, plugins_override=True):
168
"""Return the canonical name and command class for a command.
204
"""Return the command object for a command.
171
207
If true, plugin commands can override builtins.
174
cmd = _get_cmd_object(cmd_name, plugins_override)
175
# Allow plugins to extend commands
176
for hook in Command.hooks['extend_command']:
210
return _get_cmd_object(cmd_name, plugins_override)
180
212
raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
183
215
def _get_cmd_object(cmd_name, plugins_override=True):
184
"""Worker for get_cmd_object which raises KeyError rather than BzrCommandError."""
185
from bzrlib.externalcommand import ExternalCommand
216
"""Get a command object.
218
:param cmd_name: The name of the command.
219
:param plugins_override: Allow plugins to override builtins.
220
:return: A Command object instance
221
:raises KeyError: If no command is found.
187
223
# We want only 'ascii' command names, but the user may have typed
188
224
# in a Unicode name. In that case, they should just get a
189
225
# 'command not found' error later.
190
226
# In the future, we may actually support Unicode command names.
192
# first look up this command under the specified name
229
for hook in Command.hooks['get_command']:
230
cmd = hook(cmd, cmd_name)
231
if cmd is not None and not plugins_override and not cmd.plugin_name():
232
# We've found a non-plugin command, don't permit it to be
236
for hook in Command.hooks['get_missing_command']:
243
# Allow plugins to extend commands
244
for hook in Command.hooks['extend_command']:
249
def _try_plugin_provider(cmd_name):
250
"""Probe for a plugin provider having cmd_name."""
252
plugin_metadata, provider = probe_for_provider(cmd_name)
253
raise errors.CommandAvailableInPlugin(cmd_name,
254
plugin_metadata, provider)
255
except errors.NoPluginAvailable:
259
def probe_for_provider(cmd_name):
260
"""Look for a provider for cmd_name.
262
:param cmd_name: The command name.
263
:return: plugin_metadata, provider for getting cmd_name.
264
:raises NoPluginAvailable: When no provider can supply the plugin.
266
# look for providers that provide this command but aren't installed
267
for provider in command_providers_registry:
195
return plugin_cmds.get(cmd_name)()
269
return provider.plugin_for_command(cmd_name), provider
270
except errors.NoPluginAvailable:
198
cmds = _get_cmd_dict(plugins_override=False)
272
raise errors.NoPluginAvailable(cmd_name)
275
def _get_bzr_command(cmd_or_None, cmd_name):
276
"""Get a command from bzr's core."""
277
cmds = _builtin_commands()
200
279
return cmds[cmd_name]()
204
for key in plugin_cmds.keys():
205
info = plugin_cmds.get_info(key)
206
if cmd_name in info.aliases:
207
return plugin_cmds.get(key)()
208
282
# look for any command which claims this as an alias
209
283
for real_cmd_name, cmd_class in cmds.iteritems():
210
284
if cmd_name in cmd_class.aliases:
211
285
return cmd_class()
289
def _get_external_command(cmd_or_None, cmd_name):
290
"""Lookup a command that is a shell script."""
291
# Only do external command lookups when no command is found so far.
292
if cmd_or_None is not None:
294
from bzrlib.externalcommand import ExternalCommand
213
295
cmd_obj = ExternalCommand.find_command(cmd_name)
217
# look for plugins that provide this command but aren't installed
218
for provider in command_providers_registry:
220
plugin_metadata = provider.plugin_for_command(cmd_name)
221
except errors.NoPluginAvailable:
224
raise errors.CommandAvailableInPlugin(cmd_name,
225
plugin_metadata, provider)
300
def _get_plugin_command(cmd_or_None, cmd_name):
301
"""Get a command from bzr's plugins."""
303
return plugin_cmds.get(cmd_name)()
306
for key in plugin_cmds.keys():
307
info = plugin_cmds.get_info(key)
308
if cmd_name in info.aliases:
309
return plugin_cmds.get(key)()
229
313
class Command(object):
607
702
"Called after creating a command object to allow modifications "
608
703
"such as adding or removing options, docs etc. Called with the "
609
704
"new bzrlib.commands.Command object.", (1, 13), None))
705
self.create_hook(HookPoint('get_command',
706
"Called when creating a single command. Called with "
707
"(cmd_or_None, command_name). get_command should either return "
708
"the cmd_or_None parameter, or a replacement Command object that "
709
"should be used for the command. Note that the Command.hooks "
710
"hooks are core infrastructure. Many users will prefer to use "
711
"bzrlib.commands.register_command or plugin_cmds.register_lazy.",
713
self.create_hook(HookPoint('get_missing_command',
714
"Called when creating a single command if no command could be "
715
"found. Called with (command_name). get_missing_command should "
716
"either return None, or a Command object to be used for the "
717
"command.", (1, 17), None))
718
self.create_hook(HookPoint('list_commands',
719
"Called when enumerating commands. Called with a set of "
720
"cmd_name strings for all the commands found so far. This set "
721
" is safe to mutate - e.g. to remove a command. "
722
"list_commands should return the updated set of command names.",
611
725
Command.hooks = CommandHooks()
950
1065
return ignore_pipe
1068
def install_bzr_command_hooks():
1069
"""Install the hooks to supply bzr's own commands."""
1070
if _list_bzr_commands in Command.hooks["list_commands"]:
1072
Command.hooks.install_named_hook("list_commands", _list_bzr_commands,
1074
Command.hooks.install_named_hook("get_command", _get_bzr_command,
1076
Command.hooks.install_named_hook("get_command", _get_plugin_command,
1077
"bzr plugin commands")
1078
Command.hooks.install_named_hook("get_command", _get_external_command,
1079
"bzr external command lookup")
1080
Command.hooks.install_named_hook("get_missing_command", _try_plugin_provider,
1081
"bzr plugin-provider-db check")
1084
def main(argv=None):
1085
"""Main entry point of command-line interface.
1087
:param argv: list of unicode command-line arguments similar to sys.argv.
1088
argv[0] is script name usually, it will be ignored.
1089
Don't pass here sys.argv because this list contains plain strings
1090
and not unicode; pass None instead.
1092
:return: exit code of bzr command.
954
1094
import bzrlib.ui
955
1095
bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
956
1096
sys.stdin, sys.stdout, sys.stderr)
958
1098
# Is this a final release version? If so, we should suppress warnings
959
1099
if bzrlib.version_info[3] == 'final':
960
from bzrlib import symbol_versioning
961
symbol_versioning.suppress_deprecation_warnings(override=False)
963
user_encoding = osutils.get_user_encoding()
964
argv = [a.decode(user_encoding) for a in argv[1:]]
965
except UnicodeDecodeError:
966
raise errors.BzrError(("Parameter '%r' is unsupported by the current "
1100
suppress_deprecation_warnings(override=True)
1102
argv = osutils.get_unicode_argv()
1106
# ensure all arguments are unicode strings
1108
if isinstance(a, unicode):
1111
new_argv.append(a.decode('ascii'))
1112
except UnicodeDecodeError:
1113
raise errors.BzrError("argv should be list of unicode strings.")
968
1115
ret = run_bzr_catch_errors(argv)
969
1116
trace.mutter("return code %d", ret)