Skip to content

Commit 83de2bd

Browse files
committed
feat: add wait_for_directory function to handle directory creation with timeout
1 parent 95bc3b7 commit 83de2bd

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

bec_lib/bec_lib/file_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import os
6+
import time
67
import warnings
78
from typing import TYPE_CHECKING
89

@@ -204,6 +205,27 @@ def get_full_path(
204205
return full_path
205206

206207

208+
def wait_for_directory(path: str, timeout: float = 10.0, interval: float = 0.1) -> None:
209+
"""
210+
Wait for a directory to be created.
211+
212+
Args:
213+
path (str): Path to the directory to wait for.
214+
timeout (float, optional): Maximum time to wait in seconds. Defaults to 10.
215+
interval (float, optional): Time to wait between checks in seconds. Defaults to 0.1.
216+
217+
Raises:
218+
FileWriterError: If the timeout is reached before the directory is created.
219+
220+
"""
221+
deadline = time.monotonic() + timeout
222+
while time.monotonic() < deadline:
223+
if os.path.isdir(path):
224+
return
225+
time.sleep(interval)
226+
raise FileWriterError(f"Timeout reached while waiting for directory {path} to be created.")
227+
228+
207229
class FileWriter:
208230
"""FileWriter for creating file paths and directories for services and devices."""
209231

bec_lib/tests/test_file_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# pylint: skip-file
44
import os
5+
import threading
6+
import time
57
from unittest import mock
68

79
import pytest
@@ -15,6 +17,7 @@
1517
LogWriter,
1618
compile_file_components,
1719
get_full_path,
20+
wait_for_directory,
1821
)
1922
from bec_lib.messages import ScanStatusMessage
2023
from bec_lib.tests.utils import ConnectorMock
@@ -259,6 +262,30 @@ def test_compile_file_components_valid_paths(kwargs, expected_path, description)
259262
assert file_path == expected_path, description
260263

261264

265+
def test_wait_for_directory_returns_when_directory_appears(tmpdir):
266+
"""wait_for_directory should stop polling once the directory exists."""
267+
dir_path = tmpdir.join("created-later")
268+
269+
def _create_directory():
270+
time.sleep(0.02)
271+
dir_path.mkdir()
272+
273+
creator = threading.Thread(target=_create_directory)
274+
creator.start()
275+
try:
276+
wait_for_directory(str(dir_path), timeout=1.0, interval=0.01)
277+
finally:
278+
creator.join()
279+
280+
281+
def test_wait_for_directory_raises_on_timeout(tmpdir):
282+
"""wait_for_directory should raise when the directory never appears."""
283+
dir_path = tmpdir.join("never-created")
284+
285+
with pytest.raises(FileWriterError, match="Timeout reached while waiting for directory"):
286+
wait_for_directory(str(dir_path), timeout=0.05, interval=0.01)
287+
288+
262289
@pytest.mark.parametrize(
263290
"scan_info",
264291
[

0 commit comments

Comments
 (0)