Need help defining commands conditionally on other plugins
Neil Martinsen-Burrell
nmb at wartburg.edu
Fri Apr 9 17:09:23 BST 2010
In bzr-colo, there are two GUI commands for dealing with colocated
branches that depend on the presence of the qbzr plugin. Ideally, the
help for these commands would be available in any case but when run,
they would just raise errors if qbzr wasn't present. I'm having trouble
doing this in a way that interacts well with BZR_DISABLE_PLUGINS.
Currently, the commands are lazy_registered from commands.py which does
the following:
class cmd_qbranches(commands.Command):
"""List colocated branches (requires qbzr)"""
def run(self):
raise NoQBzrError(name='qbranches')
try:
import bzrlib.plugins.qbzr as qbzr
import qcommands
except ImportError, e:
trace.mutter("Can't import qbzr or some qbzr functions, "
"not defining GUI commands.\n"
"ImportError: %s" % str(e))
else:
cmd_qbranches = qcommands.cmd_qbranches
where the cmd_qbranches object is replaced by the graphical one if qbzr
is present. This is good because qcommands.cmd_qbranches inherits from
qbzr.lib.command.QBzrCommand and so we shouldn't even import that module
unless we have access to qbzr. The problem is that this code for
replacement runs when the commands.py file is first imported and that
seems to break the use of the BZR_DISABLE_PLUGINS environment variable.
For example:
$ bzr plugins
[...]
colo 0.2.0dev
Work with colocated branches using current technology.
[...]
qbzr 0.19.0dev1
QBzr - Qt-based frontend for Bazaar
$ bzr help qbranches
Purpose: List colocated branches.
Usage: bzr qbranches
[...]
$ mv ~/.bazaar/plugins/qbzr ~/.bazaar/plugins_disabled/
$ bzr help qbranches
Purpose: List colocated branches (requires qbzr)
Usage: bzr qbranches
[...]
$ bzr qbranches
bzr: ERROR: qbranches requires the qbzr plugin version >= 0.18.1.
where the help indicates "requires qbzr" when that plugin is not
installed. But, if I use the BZR_DISABLE_PLUGINS evironment variable
$ mv ~/.bazaar/plugins_disabled/qbzr ~/.bazaar/plugins
$ BZR_DISABLE_PLUGINS=qbzr bzr help qbranches
Purpose: List colocated branches.
[...]
$ BZR_DISABLE_PLUGINS=qbzr bzr qbranches
^C
$
where the final command works and brings up a GUI although I expect it
not to. (The current situation works for users, but I would like to use
BZR_DISABLE_PLUGINS in the test suite.)
Am I misusing the plugin architecture? Is there a better way to test
that a plugin is available than trying to import it and catching the
import error? Is there a better time to replace command objects than at
import time? Am I missing some better way to code this? Thanks for any
help.
More information about the bazaar
mailing list