diff options
author | Konstantin Weddige <kontakt@weddige.eu> | 2022-11-30 11:17:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-30 11:17:52 +0100 |
commit | e0d5aee4008fca8854e059934d49c4f7f40673ad (patch) | |
tree | 8116f5ebfe991a36a63e7e01d300b687b450f426 | |
parent | 9ac99c1b5699c11e4759a9955b74f3d07bcf3a34 (diff) | |
download | absl-py-e0d5aee4008fca8854e059934d49c4f7f40673ad.tar.gz |
Remove hardcoded tmp dir
the tmp dir might not be at /tmp/ on every system. tempfile.gettempdir() returns the correct folder and falls back to the CWD if there is no usable directory. As tempfile.gettempdir() ensures write access to the returned directory, testing './' separately is not necessary.
-rw-r--r-- | absl/logging/__init__.py | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/absl/logging/__init__.py b/absl/logging/__init__.py index c0ba4b0..8326155 100644 --- a/absl/logging/__init__.py +++ b/absl/logging/__init__.py @@ -87,6 +87,7 @@ import socket import struct import sys import threading +import tempfile import time import timeit import traceback @@ -709,18 +710,17 @@ def find_log_dir(log_dir=None): # Get a list of possible log dirs (will try to use them in order). if log_dir: # log_dir was explicitly specified as an arg, so use it and it alone. - dirs = [log_dir] + log_dir_candidate = log_dir elif FLAGS['log_dir'].value: # log_dir flag was provided, so use it and it alone (this mimics the # behavior of the same flag in logging.cc). - dirs = [FLAGS['log_dir'].value] + log_dir_candidate = FLAGS['log_dir'].value else: - dirs = ['/tmp/', './'] + log_dir_candidate = tempfile.gettempdir() - # Find the first usable log dir. - for d in dirs: - if os.path.isdir(d) and os.access(d, os.W_OK): - return d + # Test if log dir candidate is usable. + if os.path.isdir(log_dir_candidate) and os.access(log_dir_candidate, os.W_OK): + return log_dir_candidate raise FileNotFoundError( "Can't find a writable directory for logs, tried %s" % dirs) |