1
0
Fork 0

deleting read-only files

This commit is contained in:
Christian Kühnel 2020-04-16 10:30:39 +02:00
parent b2c68fa9be
commit 7521aeeda1

View file

@ -20,9 +20,10 @@ import os
import platform import platform
import shutil import shutil
import subprocess import subprocess
import stat
from typing import List, Dict from typing import List, Dict
import yaml import yaml
import time
from choose_projects import ChooseProjects from choose_projects import ChooseProjects
@ -146,21 +147,16 @@ def run_cmake(projects: str, repo_path: str, config_file_path: str = None, *, dr
def secure_delete(path: str): def secure_delete(path: str):
"""Try do delete a local folder. """Try do delete a local folder.
Deleting folders on Windows can be tricky and frequently fails. Handle read-only files.
In most cases this can be recovered by waiting some time and then trying again.
""" """
error_limit = 5 if not os.path.exists(path):
while error_limit > 0: return
error_limit -= 1
if not os.path.exists(path): def del_rw(action, name, exc):
return os.chmod(name, stat.S_IWRITE)
try: os.remove(name)
shutil.rmtree(path)
except PermissionError: shutil.rmtree(path, onerror=del_rw)
if error_limit <= 0:
raise
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): def _link_compile_commands(config: Configuration, repo_path: str, build_dir: str):