72
"""Return canonical name and class for all registered commands."""
72
"""Find all python files which are plugins, and load their commands
73
to add to the list of "all commands"
75
The environment variable BZRPATH is considered a delimited set of
76
paths to look through. Each entry is searched for *.py files.
77
If a directory is found, it is also searched, but they are
78
not searched recursively. This allows you to revctl the plugins.
80
Inside the plugin should be a series of cmd_* function, which inherit from
81
the bzrlib.commands.Command class.
83
bzrpath = os.environ.get('BZRPLUGINPATH', '')
88
_platform_extensions = {
94
if _platform_extensions.has_key(sys.platform):
95
platform_extension = _platform_extensions[sys.platform]
97
platform_extension = None
98
for d in bzrpath.split(os.pathsep):
99
plugin_names = {} # This should really be a set rather than a dict
100
for f in os.listdir(d):
101
if f.endswith('.py'):
103
elif f.endswith('.pyc') or f.endswith('.pyo'):
105
elif platform_extension and f.endswith(platform_extension):
106
f = f[:-len(platform_extension)]
107
if f.endswidth('module'):
108
f = f[:-len('module')]
111
if not plugin_names.has_key(f):
112
plugin_names[f] = True
114
plugin_names = plugin_names.keys()
117
sys.path.insert(0, d)
118
for name in plugin_names:
122
if sys.modules.has_key(name):
123
old_module = sys.modules[name]
124
del sys.modules[name]
125
plugin = __import__(name, locals())
126
for k in dir(plugin):
127
if k.startswith('cmd_'):
128
k_unsquished = _unsquish_command_name(k)
129
if not plugin_cmds.has_key(k_unsquished):
130
plugin_cmds[k_unsquished] = getattr(plugin, k)
132
log_error('Two plugins defined the same command: %r' % k)
133
log_error('Not loading the one in %r in dir %r' % (name, d))
136
sys.modules[name] = old_module
137
except ImportError, e:
138
log_error('Unable to load plugin: %r from %r\n%s' % (name, d, e))
143
def _get_cmd_dict(include_plugins=True):
73
145
for k, v in globals().iteritems():
74
146
if k.startswith("cmd_"):
75
yield _unsquish_command_name(k), v
77
def get_cmd_class(cmd):
147
d[_unsquish_command_name(k)] = v
149
d.update(_find_plugins())
152
def get_all_cmds(include_plugins=True):
153
"""Return canonical name and class for all registered commands."""
154
for k, v in _get_cmd_dict(include_plugins=include_plugins).iteritems():
158
def get_cmd_class(cmd,include_plugins=True):
78
159
"""Return the canonical name and command class for a command.
80
161
cmd = str(cmd) # not unicode
82
163
# first look up this command under the specified name
164
cmds = _get_cmd_dict(include_plugins=include_plugins)
84
return cmd, globals()[_squish_command_name(cmd)]
166
return cmd, cmds[cmd]
88
170
# look for any command which claims this as an alias
89
for cmdname, cmdclass in get_all_cmds():
171
for cmdname, cmdclass in cmds.iteritems():
90
172
if cmd in cmdclass.aliases:
91
173
return cmdname, cmdclass