From cf077a50aaa20cf8a3ba85bfafc317bb876b67ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Sun, 10 Apr 2016 16:48:46 +0200 Subject: [PATCH] [enh] Add pre/post script execution callbacks to hook_callback --- data/actionsmap/yunohost.yml | 12 ++++++++++-- src/yunohost/hook.py | 38 ++++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 9105e3daa..89bb0b882 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -1380,8 +1380,15 @@ hook: nargs: "*" -a: full: --args - help: Ordered list of arguments to pass to the script + help: Ordered list of arguments to pass to the scripts nargs: "*" + -q: + full: --no-trace + help: Do not print each command that will be executed + action: store_true + -d: + full: --chdir + help: The directory from where the scripts will be executed ### hook_exec() exec: @@ -1391,7 +1398,8 @@ hook: help: Path of the script to execute -a: full: --args - help: Arguments to pass to the script + help: Ordered list of arguments to pass to the script + nargs: "*" --raise-on-error: help: Raise if the script returns a non-zero exit code action: store_true diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index e6e2e797e..c90a74ca2 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -205,14 +205,22 @@ def hook_list(action, list_by='name', show_info=False): return { 'hooks': result } -def hook_callback(action, hooks=[], args=None): +def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None, + pre_callback=None, post_callback=None): """ Execute all scripts binded to an action Keyword argument: action -- Action name hooks -- List of hooks names to execute - args -- Ordered list of arguments to pass to the script + args -- Ordered list of arguments to pass to the scripts + no_trace -- Do not print each command that will be executed + chdir -- The directory from where the scripts will be executed + pre_callback -- An object to call before each script execution with + (name, priority, path, args) as arguments and which must return + the arguments to pass to the script + post_callback -- An object to call after each script execution with + (name, priority, path, succeed) as arguments """ result = { 'succeed': {}, 'failed': {} } @@ -258,20 +266,34 @@ def hook_callback(action, hooks=[], args=None): elif not isinstance(args, list): args = [args] + # Validate callbacks + if not callable(pre_callback): + pre_callback = lambda name, priority, path, args: args + if not callable(post_callback): + post_callback = lambda name, priority, path, succeed: None + # Iterate over hooks and execute them for priority in sorted(hooks_dict): for name, info in iter(hooks_dict[priority].items()): state = 'succeed' - filename = '%s-%s' % (priority, name) + path = info['path'] try: - hook_exec(info['path'], args=args, raise_on_error=True) + hook_args = pre_callback(name=name, priority=priority, + path=path, args=args) + hook_exec(path, args=hook_args, chdir=chdir, + no_trace=no_trace, raise_on_error=True) except MoulinetteError as e: - logger.error(str(e)) state = 'failed' + logger.error(str(e)) + post_callback(name=name, priority=priority, path=path, + succeed=False) + else: + post_callback(name=name, priority=priority, path=path, + succeed=True) try: - result[state][name].append(info['path']) + result[state][name].append(path) except KeyError: - result[state][name] = [info['path']] + result[state][name] = [path] return result @@ -282,7 +304,7 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, Keyword argument: path -- Path of the script to execute - args -- A list of arguments to pass to the script + args -- Ordered list of arguments to pass to the script raise_on_error -- Raise if the script returns a non-zero exit code no_trace -- Do not print each command that will be executed chdir -- The directory from where the script will be executed