Use json for return

This commit is contained in:
Josué Tille 2018-08-26 14:59:26 +02:00
parent b64196c47d
commit b7554dec21
2 changed files with 17 additions and 8 deletions

View file

@ -199,6 +199,7 @@
"global_settings_unknown_type": "Unexpected situation, the setting {setting:s} appears to have the type {unknown_type:s} but it's not a type supported by the system.", "global_settings_unknown_type": "Unexpected situation, the setting {setting:s} appears to have the type {unknown_type:s} but it's not a type supported by the system.",
"hook_exec_failed": "Script execution failed: {path:s}", "hook_exec_failed": "Script execution failed: {path:s}",
"hook_exec_not_terminated": "Script execution hasn\u2019t terminated: {path:s}", "hook_exec_not_terminated": "Script execution hasn\u2019t terminated: {path:s}",
"hook_json_return_error": "Faild to read return from hook {path:s}. Error: {msg:s}",
"hook_list_by_invalid": "Invalid property to list hook by", "hook_list_by_invalid": "Invalid property to list hook by",
"hook_name_unknown": "Unknown hook name '{name:s}'", "hook_name_unknown": "Unknown hook name '{name:s}'",
"installation_complete": "Installation complete", "installation_complete": "Installation complete",

View file

@ -32,6 +32,7 @@ from glob import iglob
from moulinette import m18n from moulinette import m18n
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from moulinette.utils import log from moulinette.utils import log
from moulinette.utils.filesystem import read_json
HOOK_FOLDER = '/usr/share/yunohost/hooks/' HOOK_FOLDER = '/usr/share/yunohost/hooks/'
CUSTOM_HOOK_FOLDER = '/etc/yunohost/hooks.d/' CUSTOM_HOOK_FOLDER = '/etc/yunohost/hooks.d/'
@ -229,7 +230,7 @@ def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None,
(name, priority, path, succeed) as arguments (name, priority, path, succeed) as arguments
""" """
result = {'succeed': {}, 'failed': {}} result = {'succeed': {}, 'failed': {}, 'stdreturn' : []}
hooks_dict = {} hooks_dict = {}
# Retrieve hooks # Retrieve hooks
@ -294,10 +295,13 @@ def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None,
result[state][name].append(path) result[state][name].append(path)
except KeyError: except KeyError:
result[state][name] = [path] result[state][name] = [path]
try:
result['stdreturn'].append(hook_return) #print(hook_return)
except KeyError: #for r in hook_return.:
result['stdreturn'] = [hook_return] result['stdreturn'].extend(hook_return) #for r in hook_return
#print(r)
#print(result['stdreturn'])
return result return result
@ -402,13 +406,17 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False,
raise MoulinetteError( raise MoulinetteError(
errno.EIO, m18n.n('hook_exec_failed', path=path)) errno.EIO, m18n.n('hook_exec_failed', path=path))
with open(stdreturn, 'r') as f: try:
returnstring = f.read() returnjson = read_json(stdreturn)
except Exception as e:
returnjson = {}
errno.EIO, m18n.n('hook_json_return_error', path=path, msg=str(e))
stdreturndir = os.path.split(stdreturn)[0] stdreturndir = os.path.split(stdreturn)[0]
os.remove(stdreturn) os.remove(stdreturn)
os.rmdir(stdreturndir) os.rmdir(stdreturndir)
return returncode, returnstring return returncode, returnjson
def _extract_filename_parts(filename): def _extract_filename_parts(filename):