Skip to content

Commit eb99356

Browse files
committed
fix: Wrap TemporaryDirectory to support older Python versions
1 parent ab6a146 commit eb99356

File tree

1 file changed

+23
-5
lines changed
  • airflow_dbt_python/hooks

1 file changed

+23
-5
lines changed

airflow_dbt_python/hooks/dbt.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import logging
6+
import sys
67
from contextlib import contextmanager
78
from pathlib import Path
89
from tempfile import TemporaryDirectory
@@ -77,6 +78,27 @@ def override_name(self):
7778
return self.store_override_name
7879

7980

81+
class DbtTemporaryDirectory(TemporaryDirectory):
82+
"""A wrapper on TemporaryDirectory for older versions of Python.
83+
84+
Support for ignore_cleanup_errors was added in Python 3.10. There is a very obscure
85+
error that can happen when cleaning up a directory, even though everything should
86+
be cleaned. We would like to use ignore_cleanup_errors to provide clean up on a
87+
best-effort basis. For the time being, we are addressing this only for Python>=3.10.
88+
"""
89+
90+
def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=True):
91+
if sys.version_info.minor < 10 and sys.version_info.major == 3:
92+
super().__init__(suffix=suffix, prefix=prefix, dir=dir)
93+
else:
94+
super().__init__(
95+
suffix=suffix,
96+
prefix=prefix,
97+
dir=dir,
98+
ignore_cleanup_errors=ignore_cleanup_errors,
99+
)
100+
101+
80102
class DbtHook(BaseHook):
81103
"""A hook to interact with dbt.
82104
@@ -293,11 +315,7 @@ def dbt_directory(
293315
store_project_dir = config.project_dir
294316

295317
with update_environment(env_vars):
296-
with TemporaryDirectory(
297-
# Cleanup things on a best-effort basis
298-
prefix="airflow_tmp",
299-
ignore_cleanup_errors=True,
300-
) as tmp_dir:
318+
with DbtTemporaryDirectory(prefix="airflow_tmp") as tmp_dir:
301319
self.log.info("Initializing temporary directory: %s", tmp_dir)
302320

303321
try:

0 commit comments

Comments
 (0)