1
0
Fork 0
mirror of synced 2024-06-26 10:51:08 -04:00
dotbot/dotbot/dispatcher.py

70 lines
2.7 KiB
Python
Raw Normal View History

2014-03-19 23:07:30 -04:00
import os
2016-01-16 22:00:15 -05:00
from .plugin import Plugin
2014-03-19 23:07:30 -04:00
from .messenger import Messenger
from .context import Context
from .util import test_success
2014-03-19 23:07:30 -04:00
class Dispatcher(object):
def __init__(self, base_directory, only=None, skip=None):
2014-03-19 23:07:30 -04:00
self._log = Messenger()
self._setup_context(base_directory)
2014-03-19 23:07:30 -04:00
self._load_plugins()
self._only = only
self._skip = skip
2014-03-19 23:07:30 -04:00
def _setup_context(self, base_directory):
path = os.path.abspath(
os.path.expanduser(base_directory))
if not os.path.exists(path):
raise DispatchError('Nonexistent base directory')
self._context = Context(path)
2014-03-19 23:07:30 -04:00
def dispatch(self, tasks):
success = True
for task in tasks:
actions = task
name = task.get('task', None)
if name is not None:
test = task.get('if', None)
if test is not None and not test_success(test, cwd=self._context.base_directory(), log=self._log):
self._log.info('Skipping task %s' % name)
actions = []
else:
actions = task.get('actions', [])
if not actions:
self._log.info('Task %s has no actions' % name)
else:
self._log.info('Starting task %s' % name)
for action in actions:
if self._only is not None and action not in self._only \
or self._skip is not None and action in self._skip:
self._log.info('Skipping action %s' % action)
continue
2014-03-19 23:07:30 -04:00
handled = False
if action == 'defaults':
self._context.set_defaults(actions[action]) # replace, not update
handled = True
# keep going, let other plugins handle this if they want
2014-03-19 23:07:30 -04:00
for plugin in self._plugins:
if plugin.can_handle(action):
try:
success &= plugin.handle(action, actions[action])
2014-03-19 23:07:30 -04:00
handled = True
2018-01-27 02:27:44 -05:00
except Exception as err:
2014-03-19 23:07:30 -04:00
self._log.error(
'An error was encountered while executing action %s' %
action)
if not handled:
success = False
self._log.error('Action %s not handled' % action)
if name and actions:
self._log.info('Task %s completed' % name)
2014-03-19 23:07:30 -04:00
return success
def _load_plugins(self):
self._plugins = [plugin(self._context)
2016-01-16 22:00:15 -05:00
for plugin in Plugin.__subclasses__()]
2014-03-19 23:07:30 -04:00
class DispatchError(Exception):
pass