From 7ab8d0e825fb63972951d5f8ecde57ca979c43a3 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Thu, 16 Jun 2022 19:58:26 +0200 Subject: [PATCH] tmp folder randomly created in folder --- .gitignore | 2 +- auto_archive.py | 10 +++++----- auto_auto_archive.py | 17 ++++++++--------- configs/config.py | 2 -- example.config.yaml | 2 -- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 8501c5d..2885782 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -tmp/ +tmp*/ temp/ .env* .DS_Store diff --git a/auto_archive.py b/auto_archive.py index 2b8a9e9..163668d 100644 --- a/auto_archive.py +++ b/auto_archive.py @@ -1,4 +1,4 @@ -import os, datetime, shutil, traceback, random +import os, datetime, shutil, traceback, random, tempfile from loguru import logger from slugify import slugify @@ -141,10 +141,10 @@ def main(): c = Config() c.parse() logger.info(f'Opening document {c.sheet} for header {c.header}') - mkdir_if_not_exists(Storage.TMP_FOLDER) - process_sheet(c) - c.destroy_webdriver() - shutil.rmtree(Storage.TMP_FOLDER) + with tempfile.TemporaryDirectory(dir="./") as tmpdir: + Storage.TMP_FOLDER = tmpdir + process_sheet(c) + c.destroy_webdriver() if __name__ == '__main__': diff --git a/auto_auto_archive.py b/auto_auto_archive.py index 14bb751..8c794c5 100644 --- a/auto_auto_archive.py +++ b/auto_auto_archive.py @@ -1,9 +1,8 @@ -import shutil +import tempfile import auto_archive from loguru import logger from configs import Config from storages import Storage -from utils import mkdir_if_not_exists def main(): @@ -17,13 +16,13 @@ def main(): wks = sh.get_worksheet(0) values = wks.get_all_values() - mkdir_if_not_exists(Storage.TMP_FOLDER) - for i in range(11, len(values)): - c.sheet = values[i][0] - logger.info(f"Processing {c.sheet}") - auto_archive.process_sheet(c) - c.destroy_webdriver() - shutil.rmtree(Storage.TMP_FOLDER) + with tempfile.TemporaryDirectory(dir="./") as tmpdir: + Storage.TMP_FOLDER = tmpdir + for i in range(11, len(values)): + c.sheet = values[i][0] + logger.info(f"Processing {c.sheet}") + auto_archive.process_sheet(c) + c.destroy_webdriver() if __name__ == "__main__": diff --git a/configs/config.py b/configs/config.py index bb4ca9b..61928b2 100644 --- a/configs/config.py +++ b/configs/config.py @@ -50,7 +50,6 @@ class Config: self.sheet = getattr_or(self.args, "sheet", execution.get("sheet")) assert self.sheet is not None, "'sheet' must be provided either through command line or configuration file" self.header = int(getattr_or(self.args, "header", execution.get("header", 1))) - Storage.TMP_FOLDER = execution.get("tmp_folder", Storage.TMP_FOLDER) self.storage = getattr_or(self.args, "storage", execution.get("storage", "s3")) self.save_logs = getattr(self.args, "save_logs") or execution.get("save_logs", False) if self.save_logs: @@ -238,7 +237,6 @@ class Config: "header": self.header, "check_if_exists": self.check_if_exists, "save_logs": self.save_logs, - "tmp_folder": Storage.TMP_FOLDER, "selenium_config": asdict(self.selenium_config), "selenium_webdriver": self.webdriver != None, "s3_config": hasattr(self, "s3_config"), diff --git a/example.config.yaml b/example.config.yaml index 9026ad4..3381486 100644 --- a/example.config.yaml +++ b/example.config.yaml @@ -66,8 +66,6 @@ execution: timeout_seconds: 120 window_width: 1400 window_height: 2000 - # local tmp folder to save files before uploading to storage - tmp_folder: tmp/ # puts execution logs into /logs folder, defaults to false save_logs: true # custom column names, only needed if different from default, can be overwritten with CMD --col-NAME="VALUE"