|
3 | 3 |
|
4 | 4 | import json |
5 | 5 | import logging |
| 6 | +import sys |
6 | 7 | from contextlib import contextmanager |
7 | 8 | from pathlib import Path |
8 | 9 | from tempfile import TemporaryDirectory |
@@ -77,6 +78,27 @@ def override_name(self): |
77 | 78 | return self.store_override_name |
78 | 79 |
|
79 | 80 |
|
| 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 | + |
80 | 102 | class DbtHook(BaseHook): |
81 | 103 | """A hook to interact with dbt. |
82 | 104 |
|
@@ -293,11 +315,7 @@ def dbt_directory( |
293 | 315 | store_project_dir = config.project_dir |
294 | 316 |
|
295 | 317 | 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: |
301 | 319 | self.log.info("Initializing temporary directory: %s", tmp_dir) |
302 | 320 |
|
303 | 321 | try: |
|
0 commit comments