Skip to content

Commit

Permalink
Now the !unload of a plugin is persistent
Browse files Browse the repository at this point in the history
The plugin is added to a blacklist to prevent its reload
  • Loading branch information
gbin committed Jul 15, 2012
1 parent 5eba5c3 commit 4c4c796
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version 1.4.2 (2012-07-)
Features:

- Better presentation on the !repos command
- load / unload of plugins is now persistent (they are blacklisted when unloaded)

Version 1.4.1 (2012-07-13)
--------------------------
Expand Down
59 changes: 53 additions & 6 deletions errbot/errBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from config import BOT_DATA_DIR, BOT_LOG_FILE, BOT_ADMINS

from errbot.jabberbot import JabberBot, botcmd, is_from_history
from errbot.plugin_manager import get_all_active_plugin_names, activate_all_plugins, deactivate_all_plugins, update_plugin_places, get_all_active_plugin_objects, get_all_plugins, global_restart, get_all_plugin_names, activate_plugin_with_version_check, deactivatePluginByName, get_plugin_obj_by_name, PluginConfigurationException, check_dependencies
from errbot.plugin_manager import get_all_active_plugin_names, deactivate_all_plugins, update_plugin_places, get_all_active_plugin_objects, get_all_plugins, global_restart, get_all_plugin_names, activate_plugin_with_version_check, deactivatePluginByName, get_plugin_obj_by_name, PluginConfigurationException, check_dependencies
from errbot.utils import PLUGINS_SUBDIR, human_name_for_git_url, tail, format_timedelta, which, get_jid_from_message
from errbot.repos import KNOWN_PUBLIC_REPOS

Expand All @@ -57,6 +57,29 @@ def add_plugin_repo(self, name, url):
repos[name] = url
self.internal_shelf['repos'] = repos

# plugin blacklisting management
def get_blacklisted_plugin(self):
return self.internal_shelf.get('bl_plugins', [])

def is_plugin_blacklisted(self, name):
return name in self.get_blacklisted_plugin()

def blacklist_plugin(self, name):
if self.is_plugin_blacklisted(name):
logging.warning('Plugin %s is already blacklisted' % name)
return
self.internal_shelf['bl_plugins'] = self.get_blacklisted_plugin() + [name]
logging.info('Plugin %s is now blacklisted' % name)

def unblacklist_plugin(self, name):
if not self.is_plugin_blacklisted(name):
logging.warning('Plugin %s is not blacklisted' % name)
return
l = self.get_blacklisted_plugin()
l.remove(name)
self.internal_shelf['bl_plugins'] = l
logging.info('Plugin %s is now unblacklisted' % name)

# configurations management
def get_plugin_configuration(self, name):
configs = self.internal_shelf['configs']
Expand Down Expand Up @@ -105,8 +128,24 @@ def warn_admins(self, warning):

def activate_non_started_plugins(self):
logging.info('Activating all the plugins...')
errors = activate_all_plugins(self.internal_shelf['configs'])
configs = self.internal_shelf['configs']
errors = ''
for pluginInfo in get_all_plugins():
try:
if self.is_plugin_blacklisted(pluginInfo.name):
errors += 'Notice: %s is blacklisted, use !load %s to unblacklist it\n' % (pluginInfo.name, pluginInfo.name)
continue
if hasattr(pluginInfo, 'is_activated') and not pluginInfo.is_activated:
logging.info('Activate plugin %s' % pluginInfo.name)
activate_plugin_with_version_check(pluginInfo.name, configs.get(pluginInfo.name, None))
except Exception, e:
logging.exception("Error loading %s" % pluginInfo.name)
errors += 'Error: %s failed to start : %s\n' % (pluginInfo.name ,e)
if errors: self.warn_admins(errors)
return errors




def signal_connect_to_all_plugins(self):
for bot in get_all_active_plugin_objects():
Expand All @@ -123,16 +162,17 @@ def connect(self):
logging.warning('Could not connect, deactivating all the plugins')
deactivate_all_plugins()
return None
self.activate_non_started_plugins()
loading_errors = self.activate_non_started_plugins()
logging.info(loading_errors)
logging.info('Notifying connection to all the plugins...')
self.signal_connect_to_all_plugins()
logging.info('Plugin activation done.')
return self.conn

def shutdown(self):
logging.info('Shutting down... deactivating all the plugins.')
deactivate_all_plugins()
self.internal_shelf.close()
deactivate_all_plugins()
logging.info('Bye.')

@botcmd
Expand Down Expand Up @@ -210,17 +250,22 @@ def deactivate_plugin(self, name):
@botcmd(admin_only = True)
def load(self, mess, args):
"""load a plugin"""
self.unblacklist_plugin(args)
return self.activate_plugin(args)

@botcmd(admin_only = True)
def unload(self, mess, args):
"""unload a plugin"""
result = self.deactivate_plugin(args)
return result
if args not in get_all_active_plugin_names():
return '%s in not active' % args
self.blacklist_plugin(args)
return self.deactivate_plugin(args)

@botcmd(admin_only = True)
def reload(self, mess, args):
"""reload a plugin"""
if self.is_plugin_blacklisted(args):
self.unblacklist_plugin(args)
result = "%s / %s" % (self.deactivate_plugin(args), self.activate_plugin(args))
return result

Expand Down Expand Up @@ -436,6 +481,8 @@ def config(self, mess, args):
{'LOGIN': 'my@email.com', 'PASSWORD': 'myrealpassword', 'DIRECTORY': '/tmp'}
"""
plugin_name = args[0]
if self.is_plugin_blacklisted(plugin_name):
return 'Load this plugin first with !load %s' % plugin_name
obj = get_plugin_obj_by_name(plugin_name)
if obj is None:
return 'Unknown plugin or the plugin could not load %s' % plugin_name
Expand Down
13 changes: 0 additions & 13 deletions errbot/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,6 @@ def update_plugin_places(list):
simplePluginManager.collectPlugins()
return errors


def activate_all_plugins(configs):
errors = ''
for pluginInfo in simplePluginManager.getAllPlugins():
try:
if hasattr(pluginInfo, 'is_activated') and not pluginInfo.is_activated:
logging.info('Activate plugin %s' % pluginInfo.name)
activate_plugin_with_version_check(pluginInfo.name, configs.get(pluginInfo.name, None))
except Exception, e:
logging.exception("Error loading %s" % pluginInfo.name)
errors += '%s failed to start : %s\n' % (pluginInfo.name ,e)
return errors

def get_all_plugins():
return simplePluginManager.getAllPlugins()

Expand Down

0 comments on commit 4c4c796

Please sign in to comment.