|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import tempfile |
|
|
|
|
|
class TempDirMixin: |
|
"""Mixin to provide easy access to temp dir. |
|
""" |
|
|
|
temp_dir_ = None |
|
|
|
@classmethod |
|
def get_base_temp_dir(cls): |
|
|
|
|
|
key = "AUDIOCRAFT_TEST_DIR" |
|
if key in os.environ: |
|
return os.environ[key] |
|
if cls.temp_dir_ is None: |
|
cls.temp_dir_ = tempfile.TemporaryDirectory() |
|
return cls.temp_dir_.name |
|
|
|
@classmethod |
|
def tearDownClass(cls): |
|
if cls.temp_dir_ is not None: |
|
try: |
|
cls.temp_dir_.cleanup() |
|
cls.temp_dir_ = None |
|
except PermissionError: |
|
|
|
|
|
|
|
|
|
pass |
|
super().tearDownClass() |
|
|
|
@property |
|
def id(self): |
|
return self.__class__.__name__ |
|
|
|
def get_temp_path(self, *paths): |
|
temp_dir = os.path.join(self.get_base_temp_dir(), self.id) |
|
path = os.path.join(temp_dir, *paths) |
|
os.makedirs(os.path.dirname(path), exist_ok=True) |
|
return path |
|
|
|
def get_temp_dir(self, *paths): |
|
temp_dir = os.path.join(self.get_base_temp_dir(), self.id) |
|
path = os.path.join(temp_dir, *paths) |
|
os.makedirs(path, exist_ok=True) |
|
return path |
|
|