Skip to content

Commit

Permalink
Merge pull request #204 from weddige/patch-1
Browse files Browse the repository at this point in the history
Remove hardcoded tmp dir
  • Loading branch information
yilei authored Dec 8, 2022
2 parents 9ac99c1 + afb0fbc commit 83adb26
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com).

## Unreleased

Nothing notable unreleased.
### Changed

* If no log dir is specified `logging.find_log_dir()` now falls back to `tempfile.gettempdir()` instead of `/tmp/`.

## 1.3.0 (2022-10-11)

Expand Down
18 changes: 9 additions & 9 deletions absl/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import struct
import sys
import threading
import tempfile
import time
import timeit
import traceback
Expand Down Expand Up @@ -706,23 +707,22 @@ def find_log_dir(log_dir=None):
FileNotFoundError: raised in Python 3 when it cannot find a log directory.
OSError: raised in Python 2 when it cannot find a log directory.
"""
# Get a list of possible log dirs (will try to use them in order).
# Get a possible log dir.
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)
"Can't find a writable directory for logs, tried %s" % log_dir_candidate)


def get_absl_log_prefix(record):
Expand Down
6 changes: 3 additions & 3 deletions absl/logging/tests/logging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,18 +706,18 @@ def test_find_log_dir_with_hda_tmp(self):
os.path.isdir.return_value = True
os.access.return_value = True
log_dir = logging.find_log_dir()
self.assertEqual('/tmp/', log_dir)
self.assertEqual(tempfile.gettempdir(), log_dir)

@flagsaver.flagsaver(log_dir='')
def test_find_log_dir_with_tmp(self):
with mock.patch.object(os, 'access'), \
mock.patch.object(os.path, 'exists'), \
mock.patch.object(os.path, 'isdir'):
os.path.exists.return_value = False
os.path.isdir.side_effect = lambda path: path == '/tmp/'
os.path.isdir.side_effect = lambda path: path == tempfile.gettempdir()
os.access.return_value = True
log_dir = logging.find_log_dir()
self.assertEqual('/tmp/', log_dir)
self.assertEqual(tempfile.gettempdir(), log_dir)

def test_find_log_dir_with_nothing(self):
with mock.patch.object(os.path, 'exists'), \
Expand Down

0 comments on commit 83adb26

Please sign in to comment.