[enh] Add pre/post script execution callbacks to hook_callback

This commit is contained in:
Jérôme Lebleu 2016-04-10 16:48:46 +02:00
parent a7657c1a39
commit cf077a50aa
2 changed files with 40 additions and 10 deletions

View file

@ -1380,8 +1380,15 @@ hook:
nargs: "*" nargs: "*"
-a: -a:
full: --args full: --args
help: Ordered list of arguments to pass to the script help: Ordered list of arguments to pass to the scripts
nargs: "*" 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() ### hook_exec()
exec: exec:
@ -1391,7 +1398,8 @@ hook:
help: Path of the script to execute help: Path of the script to execute
-a: -a:
full: --args full: --args
help: Arguments to pass to the script help: Ordered list of arguments to pass to the script
nargs: "*"
--raise-on-error: --raise-on-error:
help: Raise if the script returns a non-zero exit code help: Raise if the script returns a non-zero exit code
action: store_true action: store_true

View file

@ -205,14 +205,22 @@ def hook_list(action, list_by='name', show_info=False):
return { 'hooks': result } 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 Execute all scripts binded to an action
Keyword argument: Keyword argument:
action -- Action name action -- Action name
hooks -- List of hooks names to execute 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': {} } result = { 'succeed': {}, 'failed': {} }
@ -258,20 +266,34 @@ def hook_callback(action, hooks=[], args=None):
elif not isinstance(args, list): elif not isinstance(args, list):
args = [args] 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 # Iterate over hooks and execute them
for priority in sorted(hooks_dict): for priority in sorted(hooks_dict):
for name, info in iter(hooks_dict[priority].items()): for name, info in iter(hooks_dict[priority].items()):
state = 'succeed' state = 'succeed'
filename = '%s-%s' % (priority, name) path = info['path']
try: 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: except MoulinetteError as e:
logger.error(str(e))
state = 'failed' 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: try:
result[state][name].append(info['path']) result[state][name].append(path)
except KeyError: except KeyError:
result[state][name] = [info['path']] result[state][name] = [path]
return result return result
@ -282,7 +304,7 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False,
Keyword argument: Keyword argument:
path -- Path of the script to execute 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 raise_on_error -- Raise if the script returns a non-zero exit code
no_trace -- Do not print each command that will be executed no_trace -- Do not print each command that will be executed
chdir -- The directory from where the script will be executed chdir -- The directory from where the script will be executed