From b7554dec2176f38df20a0d49a42d522a7c702ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Sun, 26 Aug 2018 14:59:26 +0200 Subject: [PATCH] Use json for return --- locales/en.json | 1 + src/yunohost/hook.py | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/locales/en.json b/locales/en.json index 074512311..1e7e78490 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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.", "hook_exec_failed": "Script execution failed: {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_name_unknown": "Unknown hook name '{name:s}'", "installation_complete": "Installation complete", diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 143742df1..3ef05980a 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -32,6 +32,7 @@ from glob import iglob from moulinette import m18n from moulinette.core import MoulinetteError from moulinette.utils import log +from moulinette.utils.filesystem import read_json HOOK_FOLDER = '/usr/share/yunohost/hooks/' 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 """ - result = {'succeed': {}, 'failed': {}} + result = {'succeed': {}, 'failed': {}, 'stdreturn' : []} hooks_dict = {} # Retrieve hooks @@ -294,10 +295,13 @@ def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None, result[state][name].append(path) except KeyError: result[state][name] = [path] - try: - result['stdreturn'].append(hook_return) - except KeyError: - result['stdreturn'] = [hook_return] + + #print(hook_return) + #for r in hook_return.: + result['stdreturn'].extend(hook_return) #for r in hook_return + #print(r) + + #print(result['stdreturn']) return result @@ -402,13 +406,17 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, raise MoulinetteError( errno.EIO, m18n.n('hook_exec_failed', path=path)) - with open(stdreturn, 'r') as f: - returnstring = f.read() + try: + 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] os.remove(stdreturn) os.rmdir(stdreturndir) - return returncode, returnstring + return returncode, returnjson def _extract_filename_parts(filename):