From 99d6daf3abe758e2aa3fef6a1c764233a47b4f1c Mon Sep 17 00:00:00 2001 From: "ljf (zamentur)" Date: Fri, 12 Jul 2019 14:17:35 +0200 Subject: [PATCH 1/3] [fix] Path not displayed in log_corrupted_md_file --- src/yunohost/log.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yunohost/log.py b/src/yunohost/log.py index 8f8c92010..62775d619 100644 --- a/src/yunohost/log.py +++ b/src/yunohost/log.py @@ -106,7 +106,7 @@ def log_list(category=[], limit=None, with_details=False): try: metadata = yaml.safe_load(md_file) except yaml.YAMLError: - logger.warning(m18n.n('log_corrupted_md_file', file=md_path)) + logger.warning(m18n.n('log_corrupted_md_file', md_file=md_path)) entry["success"] = metadata.get("success", "?") if metadata else "?" @@ -192,7 +192,7 @@ def log_display(path, number=50, share=False): if 'log_path' in metadata: log_path = metadata['log_path'] except yaml.YAMLError: - error = m18n.n('log_corrupted_md_file', file=md_path) + error = m18n.n('log_corrupted_md_file', md_file=md_path) if os.path.exists(log_path): logger.warning(error) else: From 2db493c7d6e9cee59e7d17115421dcbd611ebba5 Mon Sep 17 00:00:00 2001 From: "ljf (zamentur)" Date: Mon, 15 Jul 2019 13:30:22 +0200 Subject: [PATCH 2/3] [fix] Use read_yaml instead of custom yaml load --- src/yunohost/log.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/yunohost/log.py b/src/yunohost/log.py index 62775d619..c9314eca8 100644 --- a/src/yunohost/log.py +++ b/src/yunohost/log.py @@ -35,7 +35,7 @@ from logging import FileHandler, getLogger, Formatter from moulinette import m18n, msettings from yunohost.utils.error import YunohostError from moulinette.utils.log import getActionLogger -from moulinette.utils.filesystem import read_file +from moulinette.utils.filesystem import read_file, read_yaml CATEGORIES_PATH = '/var/log/yunohost/categories/' OPERATIONS_PATH = '/var/log/yunohost/categories/operation/' @@ -102,13 +102,8 @@ def log_list(category=[], limit=None, with_details=False): entry["started_at"] = log_datetime if with_details: - with open(md_path, "r") as md_file: - try: - metadata = yaml.safe_load(md_file) - except yaml.YAMLError: - logger.warning(m18n.n('log_corrupted_md_file', md_file=md_path)) - - entry["success"] = metadata.get("success", "?") if metadata else "?" + metadata = read_yaml(md_path) + entry["success"] = metadata.get("success", "?") if metadata else "?" result[category].append(entry) From 04e5c7c203e26a265d2c292e115667649d2d342e Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 4 Aug 2019 17:44:53 +0200 Subject: [PATCH 3/3] [mod] use read_yaml and only keep sensible code inside of exception --- locales/en.json | 2 +- src/yunohost/log.py | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/locales/en.json b/locales/en.json index b792081a2..f67ce9088 100644 --- a/locales/en.json +++ b/locales/en.json @@ -252,7 +252,7 @@ "invalid_url_format": "Invalid URL format", "ip6tables_unavailable": "You cannot play with ip6tables here. You are either in a container or your kernel does not support it", "iptables_unavailable": "You cannot play with iptables here. You are either in a container or your kernel does not support it", - "log_corrupted_md_file": "The yaml metadata file associated with logs is corrupted: '{md_file}'", + "log_corrupted_md_file": "The yaml metadata file associated with logs is corrupted: '{md_file}\nError: {error}'", "log_category_404": "The log category '{category}' does not exist", "log_link_to_log": "Full log of this operation: '{desc}'", "log_help_to_get_log": "To view the log of the operation '{desc}', use the command 'yunohost log display {name}'", diff --git a/src/yunohost/log.py b/src/yunohost/log.py index c9314eca8..aa41cd85f 100644 --- a/src/yunohost/log.py +++ b/src/yunohost/log.py @@ -33,6 +33,7 @@ from datetime import datetime from logging import FileHandler, getLogger, Formatter from moulinette import m18n, msettings +from moulinette.core import MoulinetteError from yunohost.utils.error import YunohostError from moulinette.utils.log import getActionLogger from moulinette.utils.filesystem import read_file, read_yaml @@ -181,17 +182,19 @@ def log_display(path, number=50, share=False): if os.path.exists(md_path): with open(md_path, "r") as md_file: try: - metadata = yaml.safe_load(md_file) - infos['metadata_path'] = md_path - infos['metadata'] = metadata - if 'log_path' in metadata: - log_path = metadata['log_path'] - except yaml.YAMLError: - error = m18n.n('log_corrupted_md_file', md_file=md_path) + metadata = read_yaml(md_file) + except MoulinetteError as e: + error = m18n.n('log_corrupted_md_file', md_file=md_path, error=e) if os.path.exists(log_path): logger.warning(error) else: raise YunohostError(error) + else: + infos['metadata_path'] = md_path + infos['metadata'] = metadata + + if 'log_path' in metadata: + log_path = metadata['log_path'] # Display logs if exist if os.path.exists(log_path):