/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Robert Collins
  • Date: 2009-05-23 21:01:51 UTC
  • mto: This revision was merged to the branch mainline in revision 4441.
  • Revision ID: robertc@robertcollins.net-20090523210151-69jmrka5l4eh0zf3
Get missing command support sorted out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
    )
50
50
""")
51
51
 
52
 
from bzrlib import registry
53
 
# Compatibility
54
52
from bzrlib.hooks import HookPoint, Hooks
 
53
# Compatibility - Option used to be in commands.
55
54
from bzrlib.option import Option
 
55
from bzrlib import registry
 
56
from bzrlib.symbol_versioning import deprecated_function, deprecated_in
56
57
 
57
58
 
58
59
class CommandInfo(object):
182
183
    return plugin_cmds.keys()
183
184
 
184
185
 
185
 
def _get_cmd_dict(plugins_override=True):
186
 
    """Return name->class mapping for all commands."""
 
186
@deprecated_function(deprecated_in((1, 16, 0)))
 
187
def get_all_cmds():
 
188
    """Return canonical name and class for most commands.
 
189
    
 
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.
 
193
 
 
194
    Use 'all_command_names' + 'get_cmd_object' instead.
 
195
    """
187
196
    d = _builtin_commands()
188
197
    if plugins_override:
189
198
        d.update(plugin_cmds.iteritems())
190
 
    return d
191
 
 
192
 
 
193
 
def get_all_cmds(plugins_override=True):
194
 
    """Return canonical name and class for all registered commands."""
195
 
    for k, v in _get_cmd_dict(plugins_override=plugins_override).iteritems():
 
199
    for k, v in d.iteritems():
196
200
        yield k,v
197
201
 
198
202
 
220
224
    # in a Unicode name. In that case, they should just get a
221
225
    # 'command not found' error later.
222
226
    # In the future, we may actually support Unicode command names.
223
 
 
224
227
    cmd = None
225
228
    # Get a command
226
229
    for hook in Command.hooks['get_command']:
231
234
            if not cmd.plugin_name():
232
235
                break
233
236
    if cmd is None:
234
 
        try:
235
 
            plugin_metadata, provider = probe_for_provider(cmd_name)
236
 
            raise errors.CommandAvailableInPlugin(cmd_name,
237
 
                plugin_metadata, provider)
238
 
        except errors.NoPluginAvailable:
239
 
            pass
 
237
        for hook in Command.hooks['get_missing_command']:
 
238
            cmd = hook(cmd_name)
 
239
            if cmd is not None:
 
240
                break
 
241
    if cmd is None:
240
242
        # No command found.
241
243
        raise KeyError
242
244
    # Allow plugins to extend commands
245
247
    return cmd
246
248
 
247
249
 
 
250
def _try_plugin_provider(cmd_name):
 
251
    """Probe for a plugin provider having cmd_name."""
 
252
    try:
 
253
        plugin_metadata, provider = probe_for_provider(cmd_name)
 
254
        raise errors.CommandAvailableInPlugin(cmd_name,
 
255
            plugin_metadata, provider)
 
256
    except errors.NoPluginAvailable:
 
257
        pass
 
258
 
 
259
 
248
260
def probe_for_provider(cmd_name):
249
261
    """Look for a provider for cmd_name.
250
262
 
263
275
 
264
276
def _get_bzr_command(cmd_or_None, cmd_name):
265
277
    """Get a command from bzr's core."""
266
 
    cmds = _get_cmd_dict(plugins_override=False)
 
278
    cmds = _builtin_commands()
267
279
    try:
268
280
        return cmds[cmd_name]()
269
281
    except KeyError:
684
696
            "Called when creating a single command. Called with "
685
697
            "(cmd_or_None, command_name). get_command should either return "
686
698
            "the cmd_or_None parameter, or a replacement Command object that "
687
 
            "should be used for the command.", (1, 14), None))
 
699
            "should be used for the command.", (1, 16), None))
 
700
        self.create_hook(HookPoint('get_missing_command',
 
701
            "Called when creating a single command if no command could be "
 
702
            "found. Called with (command_name). get_missing_command should "
 
703
            "either return None, or a Command object to be used for the "
 
704
            "command.", (1, 16), None))
688
705
        self.create_hook(HookPoint('list_commands',
689
706
            "Called when enumerating commands. Called with a dict of "
690
707
            "cmd_name: cmd_class tuples for all the commands found "
691
708
            "so far. This dict is safe to mutate - to remove a command or "
692
709
            "to replace it with another (eg plugin supplied) version. "
693
710
            "list_commands should return the updated dict of commands.",
694
 
            (1, 14), None))
695
 
        # We currently ship default hooks to get builtin and plugin supplied
696
 
        # command names.
697
 
        self.install_named_hook("list_commands", _list_bzr_commands,
698
 
            "bzr commands")
699
 
        self.install_named_hook("get_command", _get_bzr_command,
700
 
            "bzr commands")
701
 
        self.install_named_hook("get_command", _get_plugin_command,
702
 
            "bzr plugin commands")
703
 
        self.install_named_hook("get_command", _get_external_command,
704
 
            "bzr external command lookup")
 
711
            (1, 16), None))
705
712
 
706
713
Command.hooks = CommandHooks()
707
714
 
1046
1053
    return ignore_pipe
1047
1054
 
1048
1055
 
 
1056
def install_bzr_command_hooks():
 
1057
    """Install the hooks to supply bzr's own commands."""
 
1058
    Command.hooks.install_named_hook("list_commands", _list_bzr_commands,
 
1059
        "bzr commands")
 
1060
    Command.hooks.install_named_hook("get_command", _get_bzr_command,
 
1061
        "bzr commands")
 
1062
    Command.hooks.install_named_hook("get_command", _get_plugin_command,
 
1063
        "bzr plugin commands")
 
1064
    Command.hooks.install_named_hook("get_command", _get_external_command,
 
1065
        "bzr external command lookup")
 
1066
    Command.hooks.install_named_hook("get_missing_command", _try_plugin_provider,
 
1067
        "bzr plugin-provider-db check")
 
1068
 
 
1069
 
1049
1070
def main(argv=None):
1050
1071
    """Main entry point of command-line interface.
1051
1072
 
1062
1083
 
1063
1084
    # Is this a final release version? If so, we should suppress warnings
1064
1085
    if bzrlib.version_info[3] == 'final':
1065
 
        from bzrlib import symbol_versioning
1066
1086
        symbol_versioning.suppress_deprecation_warnings(override=False)
1067
1087
    if argv is None:
1068
1088
        argv = osutils.get_unicode_argv()
1078
1098
        except UnicodeDecodeError:
1079
1099
            raise errors.BzrError("argv should be list of unicode strings.")
1080
1100
        argv = new_argv
 
1101
    install_bzr_command_hooks()
1081
1102
    ret = run_bzr_catch_errors(argv)
1082
1103
    trace.mutter("return code %d", ret)
1083
1104
    return ret