From eaa931da8c9255cc608e31f3d571b12499c028f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=BChnel?= Date: Wed, 15 Apr 2020 18:16:41 +0200 Subject: [PATCH] added secure delete function --- scripts/run_cmake.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/scripts/run_cmake.py b/scripts/run_cmake.py index 3199a74..c09e4c5 100755 --- a/scripts/run_cmake.py +++ b/scripts/run_cmake.py @@ -121,11 +121,7 @@ def run_cmake(projects: str, repo_path: str, config_file_path: str = None, *, dr build_dir = os.path.abspath(os.path.join(repo_path, 'build')) if not dryrun: - if os.path.exists(build_dir): - shutil.rmtree(build_dir) - while os.path.exists(build_dir): - print('Waiting for folder to really be deleted...') - time.sleep(1) + secure_delete(build_dir) os.makedirs(build_dir) env = _create_env(config) @@ -147,6 +143,25 @@ def run_cmake(projects: str, repo_path: str, config_file_path: str = None, *, dr _link_compile_commands(config, repo_path, build_dir) +def secure_delete(path: str): + """Try do delete a local folder. + + Deleting folders on Windows can be tricky and frequently fails. + In most cases this can be recovered by waiting some time and then trying again. + """ + error_limit = 5 + while error_limit > 0: + error_limit -= 1 + if not os.path.exists(path): + return + try: + shutil.rmtree(path) + except PermissionError: + pass + time.sleep(3) + raise IOError('Could not delete build folder after several tries: {}'.format(path)) + + def _link_compile_commands(config: Configuration, repo_path: str, build_dir: str): """Link compile_commands.json from build to root dir""" if config.operating_system != OperatingSystem.Linux: