Skip to content

Commit

Permalink
create PREFECT_HOME in root_settings_context (#15696)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Streed <[email protected]>
  • Loading branch information
zzstoatzz and desertaxle authored Oct 15, 2024
1 parent 042ceb2 commit 64919c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
34 changes: 13 additions & 21 deletions src/prefect/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,23 +453,6 @@ class SettingsContext(ContextModel):
def __hash__(self) -> int:
return hash(self.settings)

def __enter__(self):
"""
Upon entrance, we ensure the home directory for the profile exists.
"""
return_value = super().__enter__()

try:
prefect_home = self.settings.home
prefect_home.mkdir(mode=0o0700, exist_ok=True)
except OSError:
warnings.warn(
(f"Failed to create the Prefect home directory at {prefect_home}"),
stacklevel=2,
)

return return_value

@classmethod
def get(cls) -> "SettingsContext":
# Return the global context instead of `None` if no context exists
Expand Down Expand Up @@ -567,9 +550,9 @@ def tags(*new_tags: str) -> Generator[Set[str], None, None]:
{"a", "b", "c", "d", "e", "f"}
"""
current_tags = TagsContext.get().current_tags
new_tags = current_tags.union(new_tags)
with TagsContext(current_tags=new_tags):
yield new_tags
_new_tags = current_tags.union(new_tags)
with TagsContext(current_tags=_new_tags):
yield _new_tags


@contextmanager
Expand Down Expand Up @@ -659,7 +642,16 @@ def root_settings_context():
)
active_name = "ephemeral"

return SettingsContext(profile=profiles[active_name], settings=Settings())
if not (settings := Settings()).home.exists():
try:
settings.home.mkdir(mode=0o0700, exist_ok=True)
except OSError:
warnings.warn(
(f"Failed to create the Prefect home directory at {settings.home}"),
stacklevel=2,
)

return SettingsContext(profile=profiles[active_name], settings=settings)

# Note the above context is exited and the global settings context is used by
# an override in the `SettingsContext.get` method.
Expand Down
2 changes: 1 addition & 1 deletion src/prefect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ def post_hoc_settings(self) -> Self:
return self

@model_validator(mode="after")
def emit_warnings(self):
def emit_warnings(self) -> Self:
"""More post-hoc validation of settings, including warnings for misconfigurations."""
values = self.model_dump()
values = max_log_size_smaller_than_batch_size(values)
Expand Down
14 changes: 6 additions & 8 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,6 @@ def test_get_settings_context_missing(self, monkeypatch):
with pytest.raises(MissingContextError, match="No settings context found"):
get_settings_context()

def test_creates_home(self, tmp_path):
home = tmp_path / "home"
assert not home.exists()
with temporary_settings(updates={PREFECT_HOME: home}):
pass

assert home.exists()

def test_settings_context_uses_settings(self, temporary_profiles_path):
temporary_profiles_path.write_text(
textwrap.dedent(
Expand All @@ -239,6 +231,12 @@ def test_settings_context_uses_settings(self, temporary_profiles_path):
source=temporary_profiles_path,
)

def test_root_settings_context_creates_home(self, tmpdir, monkeypatch):
monkeypatch.setenv("PREFECT_HOME", str(tmpdir / "testing"))
with root_settings_context() as ctx:
assert ctx.settings.home == tmpdir / "testing"
assert ctx.settings.home.exists()

def test_settings_context_does_not_setup_logging(self, monkeypatch):
setup_logging = MagicMock()
monkeypatch.setattr(
Expand Down

0 comments on commit 64919c3

Please sign in to comment.