Skip to content

Commit a86ada3

Browse files
committed
Refactor compute base dir related code to be uniform in precedence --config_dir > ENV var > ~/.globus_compute
1 parent 0b68ff0 commit a86ada3

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

compute_endpoint/globus_compute_endpoint/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,17 @@ def ensure(cls) -> CommandState:
104104
return click.get_current_context().ensure_object(CommandState)
105105

106106

107-
def init_config_dir() -> pathlib.Path:
107+
def init_config_dir(custom_dir: str) -> pathlib.Path:
108+
"""
109+
:param custom_dir: An optional user-specified directory from --config-dir
110+
111+
Fetches the base Compute dir, which defaults to ~/.globus-compute but
112+
can be overridden via the ENV var GLOBUS_COMPUTE_USER_DIR.
113+
114+
If the directory does not exist, it will be created.
115+
"""
108116
try:
109-
return ensure_compute_dir()
117+
return ensure_compute_dir(custom_dir)
110118
except (FileExistsError, PermissionError) as e:
111119
raise ClickException(str(e))
112120

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
from __future__ import annotations
22

33
import os
4-
import pathlib
4+
from pathlib import Path
55

66

7-
def ensure_compute_dir() -> pathlib.Path:
8-
dirname = pathlib.Path.home() / ".globus_compute"
7+
def ensure_compute_dir(custom_dir: str | None = None) -> Path:
8+
if custom_dir:
9+
# Highest priority is the --config-dir argument
10+
compute_dir = Path(custom_dir)
11+
else:
12+
# Secondly, we check the ENV var
13+
env_dir = os.getenv("GLOBUS_COMPUTE_USER_DIR")
14+
if env_dir:
15+
compute_dir = Path(env_dir)
16+
else:
17+
# Finally, default to ~/.globus_compute
18+
compute_dir = Path.home() / ".globus_compute"
919

10-
user_dir = os.getenv("GLOBUS_COMPUTE_USER_DIR")
11-
if user_dir:
12-
dirname = pathlib.Path(user_dir)
1320

14-
if dirname.is_dir():
15-
pass
16-
elif dirname.is_file():
21+
if compute_dir.is_file():
1722
raise FileExistsError(
18-
f"Error creating directory {dirname}, "
23+
f"Error creating directory {compute_dir}, "
1924
"please remove or rename the conflicting file"
2025
)
21-
else:
22-
dirname.mkdir(mode=0o700, parents=True, exist_ok=True)
26+
elif not compute_dir.exists():
27+
compute_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
2328

24-
return dirname
29+
return compute_dir

compute_sdk/tests/unit/test_compute_dir.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,35 @@
99

1010

1111
@pytest.mark.parametrize("dir_exists", [True, False])
12-
@pytest.mark.parametrize("user_dir", ["/my/dir", None, ""])
12+
@pytest.mark.parametrize("env_dir", ["/my/dir", None, ""])
13+
@pytest.mark.parametrize("config_dir", ["/my/config_dir", None])
1314
def test_ensure_compute_dir(
1415
dir_exists: bool,
15-
user_dir: str | None,
16+
env_dir: str | None,
17+
config_dir: str | None,
1618
fs: FakeFilesystem,
1719
monkeypatch: pytest.MonkeyPatch,
1820
):
1921
home = pathlib.Path.home()
2022

21-
dirname = home / ".globus_compute"
23+
if env_dir:
24+
monkeypatch.setenv("GLOBUS_COMPUTE_USER_DIR", str(env_dir))
25+
26+
if config_dir:
27+
dirname = pathlib.Path(config_dir)
28+
elif env_dir:
29+
dirname = pathlib.Path(env_dir)
30+
monkeypatch.setenv("GLOBUS_COMPUTE_USER_DIR", str(env_dir))
31+
else:
32+
dirname = home / ".globus_compute"
2233

2334
if dir_exists:
2435
fs.create_dir(dirname)
2536

26-
if user_dir is not None:
27-
dirname = pathlib.Path(user_dir)
28-
monkeypatch.setenv("GLOBUS_COMPUTE_USER_DIR", str(dirname))
29-
30-
compute_dirname = ensure_compute_dir()
37+
compute_base_path = ensure_compute_dir(config_dir)
3138

32-
assert compute_dirname.is_dir()
33-
assert compute_dirname == dirname
39+
assert compute_base_path.is_dir()
40+
assert compute_base_path.samefile(dirname)
3441

3542

3643
@pytest.mark.parametrize("user_dir_defined", [True, False])

0 commit comments

Comments
 (0)