[enh] Add hook_list action with custom hook folder's integration

This commit is contained in:
Jérôme Lebleu 2014-11-27 17:45:14 +01:00
parent f4472ed6e1
commit cc034dbf63
2 changed files with 60 additions and 5 deletions

View file

@ -1134,6 +1134,14 @@ hook:
app:
help: Scripts related to app will be removed
### hook_list()
list:
action_help: List available hooks for an action
api: GET /hooks/<action>
arguments:
action:
help: Action name
### hook_callback()
callback:
action_help: Execute all scripts binded to an action

57
hook.py
View file

@ -32,8 +32,13 @@ import subprocess
from shlex import split as arg_split
from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger
hook_folder = '/usr/share/yunohost/hooks/'
custom_hook_folder = '/etc/yunohost/hooks.d/'
logger = getActionLogger('yunohost.hook')
def hook_add(app, file):
"""
@ -45,11 +50,7 @@ def hook_add(app, file):
"""
path, filename = os.path.split(file)
if '-' in filename:
priority, action = filename.split('-')
else:
priority = '50'
action = filename
priority, action = _extract_filename_parts(filename)
try: os.listdir(hook_folder + action)
except OSError: os.makedirs(hook_folder + action)
@ -77,6 +78,42 @@ def hook_remove(app):
except OSError: pass
def hook_list(action):
"""
List available hooks for an action
Keyword argument:
action -- Action name
"""
hooks = {}
def _append_folder(folder):
for f in os.listdir(folder + action):
path = '%s%s/%s' % (folder, action, f)
priority, name = _extract_filename_parts(f)
try:
hooks[priority][name] = path
except KeyError:
hooks[priority] = {name: path}
try:
# Append system hooks first
_append_folder(hook_folder)
except OSError:
logger.debug("system hook folder not found for action '%s' in %s",
action, hook_folder)
try:
# Append custom hooks
_append_folder(custom_hook_folder)
except OSError:
logger.debug("custom hook folder not found for action '%s' in %s",
action, custom_hook_folder)
return hooks
def hook_callback(action, args=None):
"""
Execute all scripts binded to an action
@ -206,3 +243,13 @@ def hook_exec(file, args=None):
stream.close()
return returncode
def _extract_filename_parts(filename):
"""Extract hook parts from filename"""
if '-' in filename:
priority, action = filename.split('-', 1)
else:
priority = '50'
action = filename
return priority, action