2014-03-19 23:07:30 -04:00
|
|
|
import os
|
2020-12-02 18:49:36 -05:00
|
|
|
from argparse import Namespace
|
2022-04-30 21:46:09 -04:00
|
|
|
|
2016-03-02 20:53:19 -05:00
|
|
|
from .context import Context
|
2022-04-30 21:46:09 -04:00
|
|
|
from .messenger import Messenger
|
|
|
|
from .plugin import Plugin
|
2014-03-19 23:07:30 -04:00
|
|
|
|
2022-01-30 18:48:30 -05:00
|
|
|
|
2023-09-09 20:39:45 -04:00
|
|
|
class Dispatcher:
|
2022-01-30 18:48:30 -05:00
|
|
|
def __init__(
|
2022-04-30 21:42:36 -04:00
|
|
|
self,
|
|
|
|
base_directory,
|
|
|
|
only=None,
|
|
|
|
skip=None,
|
|
|
|
exit_on_failure=False,
|
|
|
|
options=Namespace(),
|
|
|
|
plugins=None,
|
2022-01-30 18:48:30 -05:00
|
|
|
):
|
2014-03-19 23:07:30 -04:00
|
|
|
self._log = Messenger()
|
2020-12-02 18:49:36 -05:00
|
|
|
self._setup_context(base_directory, options)
|
2022-04-30 21:19:22 -04:00
|
|
|
plugins = plugins or []
|
|
|
|
self._plugins = [plugin(self._context) for plugin in plugins]
|
2020-03-25 21:38:39 -04:00
|
|
|
self._only = only
|
|
|
|
self._skip = skip
|
2021-09-12 20:32:25 -04:00
|
|
|
self._exit = exit_on_failure
|
2014-03-19 23:07:30 -04:00
|
|
|
|
2020-12-02 18:49:36 -05:00
|
|
|
def _setup_context(self, base_directory, options):
|
2022-01-30 18:48:30 -05:00
|
|
|
path = os.path.abspath(os.path.expanduser(base_directory))
|
2016-03-02 20:53:19 -05:00
|
|
|
if not os.path.exists(path):
|
2022-01-30 18:48:30 -05:00
|
|
|
raise DispatchError("Nonexistent base directory")
|
2020-12-02 18:49:36 -05:00
|
|
|
self._context = Context(path, options)
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def dispatch(self, tasks):
|
|
|
|
success = True
|
|
|
|
for task in tasks:
|
|
|
|
for action in task:
|
2022-01-30 18:48:30 -05:00
|
|
|
if (
|
|
|
|
self._only is not None
|
|
|
|
and action not in self._only
|
|
|
|
or self._skip is not None
|
|
|
|
and action in self._skip
|
|
|
|
) and action != "defaults":
|
|
|
|
self._log.info("Skipping action %s" % action)
|
2020-03-25 21:38:39 -04:00
|
|
|
continue
|
2014-03-19 23:07:30 -04:00
|
|
|
handled = False
|
2022-01-30 18:48:30 -05:00
|
|
|
if action == "defaults":
|
|
|
|
self._context.set_defaults(task[action]) # replace, not update
|
2016-03-02 20:53:19 -05:00
|
|
|
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:
|
2021-09-12 20:32:25 -04:00
|
|
|
local_success = plugin.handle(action, task[action])
|
|
|
|
if not local_success and self._exit:
|
|
|
|
# The action has failed exit
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.error("Action %s failed" % action)
|
2021-09-12 20:32:25 -04:00
|
|
|
return False
|
|
|
|
success &= local_success
|
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(
|
2022-01-30 18:48:30 -05:00
|
|
|
"An error was encountered while executing action %s" % action
|
|
|
|
)
|
2018-01-27 02:27:44 -05:00
|
|
|
self._log.debug(err)
|
2021-09-12 20:32:25 -04:00
|
|
|
if self._exit:
|
|
|
|
# There was an execption exit
|
|
|
|
return False
|
2014-03-19 23:07:30 -04:00
|
|
|
if not handled:
|
|
|
|
success = False
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.error("Action %s not handled" % action)
|
2021-09-12 20:32:25 -04:00
|
|
|
if self._exit:
|
|
|
|
# Invalid action exit
|
|
|
|
return False
|
2014-03-19 23:07:30 -04:00
|
|
|
return success
|
|
|
|
|
|
|
|
|
|
|
|
class DispatchError(Exception):
|
|
|
|
pass
|