-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* allow multiple cache files urlapaths * change env var * rename * expand env vars * use subfolders * test now * handle depth * cast to int * fix chmod only on s3 * allow cads-adaptors to directly write on the cache filesystem --------- Co-authored-by: Francesco Nazzaro <[email protected]>
- Loading branch information
1 parent
5372e00
commit 929eea1
Showing
5 changed files
with
135 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import contextlib | ||
import os | ||
import pathlib | ||
import tempfile | ||
from collections.abc import Iterator | ||
|
||
|
||
def parse_data_volumes_config(path: str | None = None) -> list[str]: | ||
if path is None: | ||
path = os.environ["DATA_VOLUMES_CONFIG"] | ||
|
||
with open(path) as fp: | ||
return [os.path.expandvars(line.rstrip("\n")) for line in fp] | ||
|
||
|
||
@contextlib.contextmanager | ||
def enter_tmp_working_dir() -> Iterator[str]: | ||
old_cwd = os.getcwd() | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
os.chdir(tmpdir) | ||
try: | ||
yield os.getcwd() | ||
finally: | ||
os.chdir(old_cwd) | ||
|
||
|
||
@contextlib.contextmanager | ||
def make_cache_tmp_path(base_dir: str) -> Iterator[pathlib.Path]: | ||
with tempfile.TemporaryDirectory(dir=base_dir) as tmpdir: | ||
cache_tmp_path = pathlib.Path(tmpdir) | ||
cache_tmp_path.with_suffix(".lock").touch() | ||
try: | ||
yield cache_tmp_path | ||
finally: | ||
cache_tmp_path.with_suffix(".lock").unlink(missing_ok=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import pathlib | ||
import tempfile | ||
|
||
import pytest | ||
|
||
from cads_worker import utils | ||
|
||
|
||
def test_utils_parse_data_volumes_config( | ||
tmp_path: pathlib.Path, | ||
monkeypatch: pytest.MonkeyPatch, | ||
) -> None: | ||
monkeypatch.setenv("FOO", "foo") | ||
monkeypatch.setenv("BAR", "bar") | ||
data_volumes_config = tmp_path / "data-volumes.config" | ||
data_volumes_config.write_text("$FOO\n${BAR}") | ||
assert utils.parse_data_volumes_config(str(data_volumes_config)) == ["foo", "bar"] | ||
|
||
monkeypatch.setenv("DATA_VOLUMES_CONFIG", str(data_volumes_config)) | ||
assert utils.parse_data_volumes_config(None) == ["foo", "bar"] | ||
|
||
|
||
def test_utils_enter_tmp_working_dir() -> None: | ||
with utils.enter_tmp_working_dir() as tmp_working_dir: | ||
assert os.getcwd() == tmp_working_dir | ||
assert os.path.dirname(tmp_working_dir) == os.path.realpath( | ||
tempfile.gettempdir() | ||
) | ||
assert not os.path.exists(tmp_working_dir) | ||
|
||
|
||
def test_utils_make_cache_tmp_path(tmp_path: pathlib.Path) -> None: | ||
with utils.make_cache_tmp_path(str(tmp_path)) as cache_tmp_path: | ||
assert cache_tmp_path.parent == tmp_path | ||
assert cache_tmp_path.with_suffix(".lock").exists() | ||
assert not cache_tmp_path.exists() | ||
assert not cache_tmp_path.with_suffix(".lock").exists() |