Skip to content

Commit 6009b69

Browse files
authored
fix: auto-create specified project or install directory if they do not yet exist (#796)
1 parent 7560c51 commit 6009b69

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

airbyte/constants.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
from __future__ import annotations
55

6+
import logging
67
import os
78
from pathlib import Path
89

910

10-
DEBUG_MODE = False # Set to True to enable additional debug logging.
11+
logger = logging.getLogger("airbyte")
12+
1113

14+
DEBUG_MODE = False # Set to True to enable additional debug logging.
1215

1316
AB_EXTRACTED_AT_COLUMN = "_airbyte_extracted_at"
1417
"""A column that stores the timestamp when the record was extracted."""
@@ -36,8 +39,28 @@
3639
}
3740
"""A set of internal columns that are reserved for PyAirbyte's internal use."""
3841

39-
DEFAULT_PROJECT_DIR: Path = (
40-
Path(os.getenv("AIRBYTE_PROJECT_DIR", "") or Path.cwd()).expanduser().absolute()
42+
43+
def _try_create_dir_if_missing(path: Path, desc: str = "specified") -> Path:
44+
"""Try to create a directory if it does not exist."""
45+
resolved_path = path.expanduser().resolve()
46+
try:
47+
if resolved_path.exists():
48+
if not resolved_path.is_dir():
49+
logger.warning(
50+
"The %s path exists but is not a directory: '%s'", desc, resolved_path
51+
)
52+
return resolved_path
53+
resolved_path.mkdir(parents=True, exist_ok=True)
54+
except Exception as ex:
55+
logger.warning(
56+
"Could not auto-create missing %s directory at '%s': %s", desc, resolved_path, ex
57+
)
58+
return resolved_path
59+
60+
61+
DEFAULT_PROJECT_DIR: Path = _try_create_dir_if_missing(
62+
Path(os.getenv("AIRBYTE_PROJECT_DIR", "") or Path.cwd()).expanduser().absolute(),
63+
desc="project",
4164
)
4265
"""Default project directory.
4366
@@ -47,8 +70,24 @@
4770
4871
This serves as the parent directory for both cache and install directories when not explicitly
4972
configured.
73+
74+
If a path is specified that does not yet exist, PyAirbyte will attempt to create it.
5075
"""
5176

77+
78+
DEFAULT_INSTALL_DIR: Path = _try_create_dir_if_missing(
79+
Path(os.getenv("AIRBYTE_INSTALL_DIR", "") or DEFAULT_PROJECT_DIR).expanduser().absolute(),
80+
desc="install",
81+
)
82+
"""Default install directory for connectors.
83+
84+
If not set, defaults to `DEFAULT_PROJECT_DIR` (`AIRBYTE_PROJECT_DIR` env var) or the current
85+
working directory if neither is set.
86+
87+
If a path is specified that does not yet exist, PyAirbyte will attempt to create it.
88+
"""
89+
90+
5291
DEFAULT_CACHE_ROOT: Path = (
5392
(Path(os.getenv("AIRBYTE_CACHE_ROOT", "") or (DEFAULT_PROJECT_DIR / ".cache")))
5493
.expanduser()
@@ -69,16 +108,6 @@
69108
Specific caches may override this value with a different schema name.
70109
"""
71110

72-
DEFAULT_INSTALL_DIR: Path = (
73-
Path(os.getenv("AIRBYTE_INSTALL_DIR", "") or DEFAULT_PROJECT_DIR).expanduser().absolute()
74-
)
75-
"""Default install directory for connectors.
76-
77-
If not set, defaults to `DEFAULT_PROJECT_DIR` (`AIRBYTE_PROJECT_DIR` env var) or the current
78-
working directory if neither is set.
79-
"""
80-
81-
82111
DEFAULT_GOOGLE_DRIVE_MOUNT_PATH = "/content/drive"
83112
"""Default path to mount Google Drive in Google Colab environments."""
84113

0 commit comments

Comments
 (0)