mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-10 15:28:34 +01:00
exp: subclass tempfile.TemporaryDirectory to handle cleanup (#5598)
* exp: subclass tempfile.TemporaryDirectory to handle cleanup * override cleanup() since cls._rmtree does not exist in <3.8
This commit is contained in:
@@ -3,13 +3,29 @@ import os
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import Optional
|
||||
|
||||
from dvc.utils.fs import remove
|
||||
|
||||
from .base import BaseExecutor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ExpTemporaryDirectory(TemporaryDirectory):
|
||||
# Python's TemporaryDirectory cleanup shutil.rmtree wrapper does not handle
|
||||
# git read-only dirs cleanly in Windows on Python <3.8, so we use our own
|
||||
# remove(). See:
|
||||
# https://github.com/iterative/dvc/pull/5425
|
||||
# https://bugs.python.org/issue26660
|
||||
|
||||
@classmethod
|
||||
def _rmtree(cls, name):
|
||||
from dvc.utils.fs import remove
|
||||
|
||||
remove(name)
|
||||
|
||||
def cleanup(self):
|
||||
if self._finalizer.detach():
|
||||
self._rmtree(self.name)
|
||||
|
||||
|
||||
class BaseLocalExecutor(BaseExecutor):
|
||||
"""Base local machine executor."""
|
||||
|
||||
@@ -37,7 +53,7 @@ class TempDirExecutor(BaseLocalExecutor):
|
||||
cache_dir: Optional[str] = None,
|
||||
**kwargs,
|
||||
):
|
||||
self._tmp_dir = TemporaryDirectory(dir=tmp_dir)
|
||||
self._tmp_dir = ExpTemporaryDirectory(dir=tmp_dir)
|
||||
kwargs["root_dir"] = self._tmp_dir.name
|
||||
super().__init__(*args, **kwargs)
|
||||
if cache_dir:
|
||||
@@ -55,4 +71,4 @@ class TempDirExecutor(BaseLocalExecutor):
|
||||
def cleanup(self):
|
||||
super().cleanup()
|
||||
logger.debug("Removing tmpdir '%s'", self._tmp_dir)
|
||||
remove(self._tmp_dir.name)
|
||||
self._tmp_dir.cleanup()
|
||||
|
||||
Reference in New Issue
Block a user