From f5bb7595e01fa6b12e651e8d889eaccdf381ac25 Mon Sep 17 00:00:00 2001 From: Magdalena Kasenberg Date: Fri, 23 Aug 2024 12:34:26 +0200 Subject: [PATCH] bot: Move stats files after bot run In backup mode, these files have to be moved/deleted to start fresh bot start. Cron does this automatically, but without the cron, one had to do it manually. --- autopts/bot/common.py | 49 +++++++++++++++++++++++++++++++------------ autopts/config.py | 1 + 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/autopts/bot/common.py b/autopts/bot/common.py index bcf0210667..67fa1193e5 100644 --- a/autopts/bot/common.py +++ b/autopts/bot/common.py @@ -281,21 +281,27 @@ def bot_pre_cleanup(self): """Perform cleanup before test run :return: None """ - try: - files_to_save = [ - self.file_paths['TMP_DIR'], - self.file_paths['IUT_LOGS_DIR'], - ] + files_to_save = [ + self.file_paths['TMP_DIR'], + self.file_paths['IUT_LOGS_DIR'], + ] - save_dir = os.path.join(self.file_paths['OLD_LOGS_DIR'], - datetime.datetime.now().strftime("%Y_%m_%d_%H_%M")) - Path(save_dir).mkdir(parents=True, exist_ok=True) + save_dir = os.path.join(self.file_paths['OLD_LOGS_DIR'], + datetime.datetime.now().strftime("%Y_%m_%d_%H_%M")) + save_files(files_to_save, save_dir) + + def bot_post_cleanup(self): + files_to_save = [ + self.file_paths['ALL_STATS_RESULTS_XML_FILE'], + self.file_paths['TC_STATS_RESULTS_XML_FILE'], + self.file_paths['TEST_CASES_JSON_FILE'], + self.file_paths['ALL_STATS_JSON_FILE'], + self.file_paths['TC_STATS_JSON_FILE'], + self.file_paths['BOT_STATE_JSON_FILE'], + ] - for file_path in files_to_save: - if os.path.exists(file_path): - shutil.move(file_path, os.path.join(save_dir, os.path.basename(file_path))) - except OSError as e: - pass + save_dir = self.file_paths['BOT_STATE_DIR'] + save_files(files_to_save, save_dir) def _yield_next_config(self): limit_counter = 0 @@ -548,6 +554,8 @@ def start(self, args=None): if 'mail' in self.bot_config: self.send_email(report_data) + self.bot_post_cleanup() + print("Done") def run_tests(self): @@ -943,3 +951,18 @@ def load_module_from_path(cfg): sys.path.remove(config_dirname) return module + + +def save_files(files_to_save, save_dir: str): + try: + for file_path in files_to_save: + if os.path.exists(file_path): + Path(save_dir).mkdir(parents=True, exist_ok=True) + break + + for file_path in files_to_save: + if os.path.exists(file_path): + dst_file_path = os.path.join(save_dir, os.path.basename(file_path)) + shutil.move(file_path, dst_file_path) + except OSError as e: + pass diff --git a/autopts/config.py b/autopts/config.py index dacb17dbd0..c6582f0d5e 100644 --- a/autopts/config.py +++ b/autopts/config.py @@ -44,6 +44,7 @@ def generate_file_paths(file_paths=None, autopts_root_dir=AUTOPTS_ROOT_DIR): 'TC_STATS_JSON_FILE': os.path.join(FILE_PATHS['TMP_DIR'], 'tc_stats.json'), 'TEST_CASE_DB_FILE': os.path.join(FILE_PATHS['TMP_DIR'], 'TestCase.db'), 'BOT_STATE_JSON_FILE': os.path.join(FILE_PATHS['TMP_DIR'], 'bot_state.json'), + 'BOT_STATE_DIR': os.path.join(FILE_PATHS['TMP_DIR'], 'final_state'), 'REPORT_README_MD_FILE': os.path.join(FILE_PATHS['TMP_DIR'], 'README.md'), 'REPORT_DIR': os.path.join(FILE_PATHS['TMP_DIR'], 'autopts_report'), 'IUT_LOGS_DIR': os.path.join(autopts_root_dir, 'logs'),