From e884d606fa360e7437e08cde3ffba34826ed8bbc Mon Sep 17 00:00:00 2001 From: Chavithra Date: Tue, 29 Mar 2022 14:32:33 +0200 Subject: [PATCH] Logging : masOS+DiscordBot (#1580) * Log Generation : refactor log_dir handling * Data Collection : fix boto3 edge case * Logging : linting --- .../log/collection/log_sender.py | 2 +- .../log/collection/s3_sender.py | 7 +++-- .../log/generation/directories.py | 30 ++++++++----------- gamestonk_terminal/loggers.py | 13 +++++++- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/gamestonk_terminal/log/collection/log_sender.py b/gamestonk_terminal/log/collection/log_sender.py index e48db2e65ad..b5c3930d652 100644 --- a/gamestonk_terminal/log/collection/log_sender.py +++ b/gamestonk_terminal/log/collection/log_sender.py @@ -86,7 +86,7 @@ class LogSender(Thread): file=file, object_key=object_key, tmp_file=tmp_file, - copy=last, + last=last, ) except Exception: self.fails_increment() diff --git a/gamestonk_terminal/log/collection/s3_sender.py b/gamestonk_terminal/log/collection/s3_sender.py index b75be190eaa..5566ef8116e 100644 --- a/gamestonk_terminal/log/collection/s3_sender.py +++ b/gamestonk_terminal/log/collection/s3_sender.py @@ -83,7 +83,7 @@ def send_to_s3( file: Path, object_key: str, tmp_file: Path, - copy: bool = False, + last: bool = False, ): api_url = DEFAULT_API_URL bucket = DEFAULT_BUCKET @@ -94,13 +94,14 @@ def send_to_s3( tmp_file.parent.mkdir(exist_ok=True) - if copy: + if last: copyfile(file, tmp_file) else: file.rename(tmp_file) if ( - aws_settings.aws_access_key_id != "REPLACE_ME" + not last + and aws_settings.aws_access_key_id != "REPLACE_ME" and aws_settings.aws_secret_access_key != "REPLACE_ME" ): send_to_s3_directly( diff --git a/gamestonk_terminal/log/generation/directories.py b/gamestonk_terminal/log/generation/directories.py index d03b18672a2..342e2dee82d 100644 --- a/gamestonk_terminal/log/generation/directories.py +++ b/gamestonk_terminal/log/generation/directories.py @@ -1,5 +1,4 @@ # IMPORTATION STANDARD -import os import uuid from pathlib import Path @@ -12,28 +11,23 @@ from pathlib import Path def get_log_dir() -> Path: """Retrieve application's log directory.""" - file_path = Path(__file__) - log_dir = file_path.parent.parent.parent.parent.absolute().joinpath("logs") + log_dir = Path(__file__).parent.parent.parent.parent.joinpath("logs") + log_dir.mkdir(exist_ok=True) + logid_file = log_dir.joinpath(".logid") - if not os.path.isdir(log_dir.absolute()): - os.mkdir(log_dir.absolute()) + if not logid_file.is_file(): + logid = str(uuid.uuid4()) - log_id = log_dir.absolute().joinpath(".logid") - - if not os.path.isfile(log_id.absolute()): - logging_id = f"{uuid.uuid4()}" - with open(log_id.absolute(), "a") as a_file: - a_file.write(f"{logging_id}\n") + with open(logid_file, "w") as f: + f.write(f"{logid}\n") else: - with open(log_id.absolute()) as a_file: - logging_id = a_file.readline().rstrip() + with open(logid_file) as a_file: + logid = a_file.readline().rstrip() - uuid_log_dir = log_dir.absolute().joinpath(logging_id) + logid_dir = log_dir.joinpath(logid) + logid_dir.mkdir(exist_ok=True) - if not os.path.isdir(uuid_log_dir.absolute()): - os.mkdir(uuid_log_dir.absolute()) - - return uuid_log_dir + return logid_dir def get_log_sub_dir(name: str): diff --git a/gamestonk_terminal/loggers.py b/gamestonk_terminal/loggers.py index 019da5a0468..459b9f62bd9 100644 --- a/gamestonk_terminal/loggers.py +++ b/gamestonk_terminal/loggers.py @@ -51,7 +51,18 @@ START_TIMESTAMP = int(time.time()) def get_app_id() -> str: """UUID of the current installation.""" - app_id = get_log_dir().stem + try: + app_id = get_log_dir().stem + except OSError as e: + if e.errno == 30: + print("Please move the application into a writable location.") + print( + "Note for macOS users: copy `Gamestonk Terminal` folder outside the DMG." + ) + else: + raise e + except Exception as e: + raise e return app_id