From c435cd059e70da5608b204531dbeeb2cbbc9c16f Mon Sep 17 00:00:00 2001 From: Fredrik Corneliusson Date: Mon, 18 Oct 2021 00:14:06 +0200 Subject: [PATCH] Refactored duplicated code --- click_web/resources/cmd_exec.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/click_web/resources/cmd_exec.py b/click_web/resources/cmd_exec.py index 8894a35..573191f 100644 --- a/click_web/resources/cmd_exec.py +++ b/click_web/resources/cmd_exec.py @@ -424,13 +424,7 @@ def save(self): def after_script_executed(self): super().after_script_executed() - fd, filename = tempfile.mkstemp(dir=self.temp_dir(), prefix=self.key) - folder_path = self.file_path - self.file_path = filename - - logger.info(f'Zipping {self.key} to {filename}') - self.file_path = shutil.make_archive(self.file_path, 'zip', folder_path) - logger.info(f'Zip file created {self.file_path}') + self.file_path = zip_folder(self.file_path, self.temp_dir(), out_prefix=self.key) self.generate_download_link = True @@ -447,10 +441,13 @@ def save(self): def after_script_executed(self): super().after_script_executed() - fd, filename = tempfile.mkstemp(dir=self.temp_dir(), prefix=self.key) - folder_path = self.file_path - self.file_path = filename - logger.info(f'Zipping {self.key} to {filename}') - self.file_path = shutil.make_archive(self.file_path, 'zip', folder_path) - logger.info(f'Zip file created {self.file_path}') + self.file_path = zip_folder(self.file_path, self.temp_dir(), out_prefix=self.key) self.generate_download_link = True + + +def zip_folder(folder_path, out_folder, out_prefix): + fd, out_base_name = tempfile.mkstemp(dir=out_folder, prefix=out_prefix) + logger.info(f'Zipping {folder_path}') + zip_file_path = shutil.make_archive(out_base_name, 'zip', folder_path) + logger.info(f'Zip file created {zip_file_path}') + return zip_file_path