From fb40ea13cc947f9f3d959e1d8c60ab46291eb834 Mon Sep 17 00:00:00 2001 From: ChrisTerBeke Date: Thu, 17 May 2018 09:25:03 +0200 Subject: [PATCH] Try onerror --- cura/Backups/Backup.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cura/Backups/Backup.py b/cura/Backups/Backup.py index 615fb45297..aeffcf80eb 100644 --- a/cura/Backups/Backup.py +++ b/cura/Backups/Backup.py @@ -138,8 +138,7 @@ class Backup: return extracted - @staticmethod - def _extractArchive(archive: "ZipFile", target_path: str) -> bool: + def _extractArchive(self, archive: "ZipFile", target_path: str) -> bool: """ Extract the whole archive to the given target path. :param archive: The archive as ZipFile. @@ -148,7 +147,7 @@ class Backup: """ Logger.log("d", "Removing current data in location: %s", target_path) try: - shutil.rmtree(target_path) + shutil.rmtree(target_path, onerror=self._onRemoveError) except PermissionError as error: # This happens if a file is already opened by another program, usually only the log file. # For now we just ignore this as it doesn't harm the restore process. @@ -157,3 +156,10 @@ class Backup: Logger.log("d", "Extracting backup to location: %s", target_path) archive.extractall(target_path) return True + + @staticmethod + def _onRemoveError(*args): + import stat # needed for file stat + func, path, _ = args # onerror returns a tuple containing function, path and exception info + os.chmod(path, stat.S_IWRITE) + os.remove(path)