diff --git a/daisy/logging.py b/daisy/logging.py index 9a5e644b..5067934a 100644 --- a/daisy/logging.py +++ b/daisy/logging.py @@ -22,6 +22,7 @@ def set_log_basedir(path): else: LOG_BASEDIR = None + def get_log_basedir(): """Get the base directory for logging.""" global LOG_BASEDIR @@ -30,12 +31,10 @@ def get_log_basedir(): def get_worker_log_basename(worker_id, task_id=None): """Get the basename of log files for individual workers.""" - global LOG_BASEDIR - - if LOG_BASEDIR is None: - return None - basename = LOG_BASEDIR + basename = get_log_basedir() + if basename is None: + return None if task_id is not None: basename /= task_id basename /= f"worker_{worker_id}" @@ -64,7 +63,6 @@ def redirect_stdouterr(basename, mode="w"): def _file_reopen(filename, mode, file_obj): - new = open(filename, mode) newfd = new.fileno() targetfd = file_obj.fileno() diff --git a/daisy/server.py b/daisy/server.py index 0d103e5e..7eaf389b 100644 --- a/daisy/server.py +++ b/daisy/server.py @@ -2,7 +2,6 @@ from .block import BlockStatus from .block_bookkeeper import BlockBookkeeper from .context import Context -from .logging import get_log_basedir from .messages import ( AcquireBlock, BlockFailed, diff --git a/daisy/task_worker_pools.py b/daisy/task_worker_pools.py index eb14f482..e4eb7ed6 100644 --- a/daisy/task_worker_pools.py +++ b/daisy/task_worker_pools.py @@ -1,5 +1,4 @@ from .context import Context -from .logging import get_log_basedir from .server_observer import ServerObserver from .worker_pool import WorkerPool import logging diff --git a/daisy/worker.py b/daisy/worker.py index 41d3054b..7ff56ec7 100644 --- a/daisy/worker.py +++ b/daisy/worker.py @@ -91,6 +91,8 @@ def _spawn_wrapper(self): try: os.environ[self.context.ENV_VARIABLE] = self.context.to_env() + log_base = self.context.get("logdir", daisy_logging.get_log_basedir()) + daisy_logging.set_log_basedir(log_base) log_base = daisy_logging.get_worker_log_basename( self.worker_id, self.context.get("task_id", None) diff --git a/tests/conf.py b/tests/conf.py deleted file mode 100644 index b6a70be2..00000000 --- a/tests/conf.py +++ /dev/null @@ -1,8 +0,0 @@ -import pytest - -from daisy.logging import set_log_basedir, get_log_basedir - - -@pytest.fixture(autouse=True) -def logdir(tmp_path): - set_log_basedir(tmp_path / "daisy_logs") \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..f670a927 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,8 @@ +import pytest + +from daisy.logging import set_log_basedir + + +@pytest.fixture(autouse=True) +def logdir(tmp_path): + set_log_basedir(tmp_path / "daisy_logs") diff --git a/tests/tmpdir_test.py b/tests/tmpdir_test.py deleted file mode 100644 index 9893689c..00000000 --- a/tests/tmpdir_test.py +++ /dev/null @@ -1,87 +0,0 @@ -from datetime import datetime -from tempfile import mkdtemp -from warnings import warn -import os -import shutil -import unittest - - -class TmpDirTestCase(unittest.TestCase): - """ - Usage: - - If your test case dumps out any files, use ``self.path_to("path", "to", - "my.file")`` to get the path to a directory in your temporary directory. - This will be namespaced by the test class, timestamp and test method, e.g. - - >>> self.path_to("path", "to", "my.file") - /tmp/daisy_MyTestCase_2018-03-08T18:32:18.967927_r4nd0m/my_test_method/path/to/my.file - - Each test method's data will be deleted after the test case is run - (regardless of pass, fail or error). To disable test method data deletion, - set ``self._cleanup = False`` anywhere in the test. - - The test case directory will be deleted after every test method is run, - unless there is data left in it. Any files written directly to the class - output directory (rather than the test output subdirectory) should - therefore be explicitly removed before tearDownClass is called. To disable - data deletion for the whole class (the test case directory and all tests), - set ``_cleanup = False`` in the class definition. N.B. doing this in a - method (``type(self)._cleanup = False``) will have unexpected results - depending on the order of test execution. - - Subclasses implementing their own setUp, setUpClass, tearDown and - tearDownClass should explicitly call the ``super`` method in the method - definition. - """ - - _output_root = "" - _cleanup = True - - def path_to(self, *args): - return type(self).path_to_cls(self._testMethodName, *args) - - @classmethod - def path_to_cls(cls, *args): - return os.path.join(cls._output_root, *args) - - @classmethod - def setUpClass(cls): - timestamp = datetime.now().isoformat() - cls._output_root = mkdtemp( - prefix="daisy_{}_{}_".format(cls.__name__, timestamp) - ) - - def setUp(self): - os.mkdir(self.path_to()) - - def tearDown(self): - path = self.path_to() - try: - if self._cleanup: - shutil.rmtree(path) - else: - warn("Directory {} was not deleted".format(path)) - except OSError as e: - if "[Errno 2]" in str(e): - pass - else: - raise - - @classmethod - def tearDownClass(cls): - try: - if cls._cleanup: - os.rmdir(cls.path_to_cls()) - else: - warn("Directory {} was not deleted".format(cls.path_to_cls())) - except OSError as e: - if "[Errno 39]" in str(e): - warn( - "Directory {} could not be deleted as it still had data " - "in it".format(cls.path_to_cls()) - ) - elif "[Errno 2]" in str(e): - pass - else: - raise