diff --git a/cadetrdm/tools/process_example.py b/cadetrdm/tools/process_example.py index f716e93..09d5b8a 100644 --- a/cadetrdm/tools/process_example.py +++ b/cadetrdm/tools/process_example.py @@ -4,11 +4,32 @@ from abc import abstractmethod from pathlib import Path import subprocess +import stat +import time from cadetrdm import ProjectRepo, Options from cadetrdm.wrapper import tracks_results +def _on_rm_error(func, path, exc_info): + try: + os.chmod(path, stat.S_IWRITE) + except Exception: + pass + func(path) + +def rmtree_with_retries(path: Path, retries: int = 8, sleep_s: float = 0.25): + last_err = None + for _ in range(retries): + try: + shutil.rmtree(path, onerror=_on_rm_error) + return + except PermissionError as e: + last_err = e + time.sleep(sleep_s) + raise last_err + + class ParallelizationBase: def __init__(self, n_cores=1): self.n_cores = n_cores @@ -136,7 +157,9 @@ def create_output(root_path: Path, output_path: Path, n_cores=1): None """ if os.path.exists(output_path): - shutil.rmtree(output_path) + rmtree_with_retries(output_path) + + shutil.copytree(root_path, output_path) # Find all myst files recursively