From 9b9c4370051263d4d622e3d97f822adbb6a275eb Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 8 Apr 2022 11:15:43 -0300 Subject: [PATCH] [view.window] refactoring: upgrade logs --- bauh/view/qt/window.py | 48 +++++++++++++++++------------------ bauh/view/resources/locale/ca | 1 + bauh/view/resources/locale/de | 1 + bauh/view/resources/locale/en | 1 + bauh/view/resources/locale/es | 1 + bauh/view/resources/locale/fr | 1 + bauh/view/resources/locale/it | 1 + bauh/view/resources/locale/pt | 1 + bauh/view/resources/locale/ru | 1 + bauh/view/resources/locale/tr | 1 + 10 files changed, 33 insertions(+), 24 deletions(-) diff --git a/bauh/view/qt/window.py b/bauh/view/qt/window.py index d30f7190..f04e1179 100755 --- a/bauh/view/qt/window.py +++ b/bauh/view/qt/window.py @@ -1,5 +1,6 @@ import logging import operator +import os.path import time import traceback from pathlib import Path @@ -1154,20 +1155,10 @@ class ManageWindow(QWidget): self._finish_action() if res.get('id'): - output = self.textarea_details.toPlainText() - - if output: - try: - Path(UpgradeSelected.UPGRADE_LOGS_DIR).mkdir(parents=True, exist_ok=True) - logs_path = '{}/{}.log'.format(UpgradeSelected.UPGRADE_LOGS_DIR, res['id']) - with open(logs_path, 'w+') as f: - f.write(output) - - self.textarea_details.appendPlainText('\n* Upgrade summary generated at: {}'.format(UpgradeSelected.SUMMARY_FILE.format(res['id']))) - log_msg = '* ' + self.i18n['console.operation_log'].format(path=f'"{logs_path}"') - self.textarea_details.appendPlainText(log_msg) - except: - traceback.print_exc() + self._write_operation_logs('upgrade', custom_log_file=f"{UpgradeSelected.UPGRADE_LOGS_DIR}/{res['id']}.log") + sum_log_file = UpgradeSelected.SUMMARY_FILE.format(res['id']) + summ_msg = '* ' + self.i18n['console.upgrade_summary'].format(path=f'"{sum_log_file}"') + self.textarea_details.appendPlainText(summ_msg) if res['success']: self.comp_manager.remove_saved_state(ACTION_UPGRADE) @@ -1388,27 +1379,36 @@ class ManageWindow(QWidget): self.thread_install.root_pwd = pwd self.thread_install.start() - def _write_operation_logs(self, type_: str, pkg: PackageView): + def _write_operation_logs(self, type_: str, pkg: Optional[PackageView] = None, + custom_log_file: Optional[str] = None): + console_output = self.textarea_details.toPlainText() if console_output: - log_path = f"{LOGS_DIR}/{type_}/{pkg.model.get_type()}/{pkg.model.name}" - try: - Path(log_path).mkdir(parents=True, exist_ok=True) - except OSError: - self.textarea_details.appendPlainText(f"[error] Could not create the operation log directory '{log_path}'") - return + if custom_log_file: + log_dir = os.path.dirname(custom_log_file) + log_file = custom_log_file + else: + log_dir = f"{LOGS_DIR}/{type_}" + if pkg: + log_dir = f"{log_dir}/{pkg.model.get_type()}/{pkg.model.name}" - log_file = f'{log_path}/{int(time.time())}.log' + log_file = f'{log_dir}/{int(time.time())}.log' + + try: + Path(log_dir).mkdir(parents=True, exist_ok=True) + except OSError: + self.logger.error(f"Could not create the operation log directory '{log_dir}'") + return try: with open(log_file, 'w+') as f: f.write(console_output) except OSError: - self.textarea_details.appendPlainText(f"[error] Could not write the operation log to file '{log_file}'") + self.logger.error(f"Could not write the operation log to file '{log_file}'") return - log_msg = '* ' + self.i18n['console.operation_log'].format(path=f'"{log_file}"') + log_msg = '\n* ' + self.i18n['console.operation_log'].format(path=f'"{log_file}"') self.textarea_details.appendPlainText(log_msg) def _finish_install(self, res: dict): diff --git a/bauh/view/resources/locale/ca b/bauh/view/resources/locale/ca index 075fe4b8..161241d7 100644 --- a/bauh/view/resources/locale/ca +++ b/bauh/view/resources/locale/ca @@ -201,6 +201,7 @@ clean=netejar close=tanca confirmation=confirmació console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=continua copy=copia core.config.backup=Enabled diff --git a/bauh/view/resources/locale/de b/bauh/view/resources/locale/de index ecabb240..034d38d8 100644 --- a/bauh/view/resources/locale/de +++ b/bauh/view/resources/locale/de @@ -200,6 +200,7 @@ clean=Bereinigen close=Schließen confirmation=Bestätigung console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=Fortfahren copy=Kopieren core.config.backup=Enabled diff --git a/bauh/view/resources/locale/en b/bauh/view/resources/locale/en index ec6d5e33..eb30faa6 100644 --- a/bauh/view/resources/locale/en +++ b/bauh/view/resources/locale/en @@ -202,6 +202,7 @@ clean=clean close=close confirmation=confirmation console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=continue copy=copy core.config.backup.downgrade=Before downgrading diff --git a/bauh/view/resources/locale/es b/bauh/view/resources/locale/es index efea1271..dd06cac2 100644 --- a/bauh/view/resources/locale/es +++ b/bauh/view/resources/locale/es @@ -201,6 +201,7 @@ clean=limpiar close=cerrar confirmation=confirmación console.operation_log=Los registros de la operación se encuentran en {path} +console.upgrade_summary=El resumen de actualización se encurentra en {path} continue=continuar copy=copiar core.config.backup=Habilitada diff --git a/bauh/view/resources/locale/fr b/bauh/view/resources/locale/fr index c0686ce3..710e3b42 100644 --- a/bauh/view/resources/locale/fr +++ b/bauh/view/resources/locale/fr @@ -200,6 +200,7 @@ clean=vider close=fermer confirmation=confirmation console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=continuer copy=copier core.config.backup.downgrade=Avant de downgrader diff --git a/bauh/view/resources/locale/it b/bauh/view/resources/locale/it index aa5062f7..d8ba47c0 100644 --- a/bauh/view/resources/locale/it +++ b/bauh/view/resources/locale/it @@ -200,6 +200,7 @@ clean=pulire close=vicino confirmation=conferma console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=continua copy=copia core.config.backup=Enabled diff --git a/bauh/view/resources/locale/pt b/bauh/view/resources/locale/pt index 1c09ec48..1cf4b38e 100644 --- a/bauh/view/resources/locale/pt +++ b/bauh/view/resources/locale/pt @@ -200,6 +200,7 @@ clean=limpar close=fechar confirmation=confirmação console.operation_log=Os registros da operação podem ser encontrados em {path} +console.upgrade_summary=O resumo da atualização se encontra em {path} continue=continuar copy=copiar core.config.backup.downgrade=Antes de reverter versão diff --git a/bauh/view/resources/locale/ru b/bauh/view/resources/locale/ru index bf2a0605..d543de5a 100644 --- a/bauh/view/resources/locale/ru +++ b/bauh/view/resources/locale/ru @@ -200,6 +200,7 @@ clean=Очистка close=Закрыть confirmation=Подтверждение console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=Продолжить copy=Копировать core.config.backup=Enabled diff --git a/bauh/view/resources/locale/tr b/bauh/view/resources/locale/tr index 6a206443..22d26e06 100644 --- a/bauh/view/resources/locale/tr +++ b/bauh/view/resources/locale/tr @@ -200,6 +200,7 @@ clean=temizle close=kapat confirmation=onayla console.operation_log=The operation logs can be found at {path} +console.upgrade_summary=The upgrade summary can be found at {path} continue=devam copy=kopyala core.config.backup.downgrade=Önce sürüm düşürülüyor