Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1825673 use tmdir in case of Permission error #2111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/snowflake/connector/sf_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import os
import pathlib
from functools import cached_property
from tempfile import TemporaryDirectory
from typing import Protocol
from warnings import warn

from platformdirs import PlatformDirs

Expand All @@ -34,17 +36,30 @@ def _resolve_platform_dirs() -> PlatformDirsProto:
snowflake_home = pathlib.Path(
os.environ.get("SNOWFLAKE_HOME", "~/.snowflake/"),
).expanduser()
if snowflake_home.exists():
try:
if snowflake_home.exists():
return SFPlatformDirs(
str(snowflake_home),
**platformdir_kwargs,
)
else:
# In case SNOWFLAKE_HOME does not exist we fall back to using
# platformdirs to determine where system files should be placed. Please
# see docs for all the directories defined in the module at
# https://platformdirs.readthedocs.io/
return PlatformDirs(**platformdir_kwargs)
except PermissionError as pe:
tmp_dir = TemporaryDirectory(
suffix="_snowflake", ignore_cleanup_errors=True
)
warn(
f"Received permission error while checking if {snowflake_home} exists. Continue with temporary direcoty `{tmp_dir.name}`.\n"
f"Original Error: {pe}"
)
return SFPlatformDirs(
str(snowflake_home),
str(tmp_dir.name),
**platformdir_kwargs,
)
else:
# In case SNOWFLAKE_HOME does not exist we fall back to using
# platformdirs to determine where system files should be placed. Please
# see docs for all the directories defined in the module at
# https://platformdirs.readthedocs.io/
return PlatformDirs(**platformdir_kwargs)


class SFPlatformDirs:
Expand Down
11 changes: 11 additions & 0 deletions test/unit/test_connector_sf_dirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from unittest import mock

from snowflake.connector.sf_dirs import SFPlatformDirs, _resolve_platform_dirs


@mock.patch("pathlib.Path.exists", side_effect=PermissionError)
def test_snowflake_home_permission_error(self):
platform_dirs = _resolve_platform_dirs()
assert isinstance(platform_dirs, SFPlatformDirs)
assert platform_dirs.user_config_path.name.endswith("_snowflake")
assert platform_dirs.user_config_path.name.startswith("tmp")
Loading