Try onerror

This commit is contained in:
ChrisTerBeke 2018-05-17 09:25:03 +02:00
parent eb436a8b0e
commit fb40ea13cc

View File

@ -138,8 +138,7 @@ class Backup:
return extracted return extracted
@staticmethod def _extractArchive(self, archive: "ZipFile", target_path: str) -> bool:
def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
""" """
Extract the whole archive to the given target path. Extract the whole archive to the given target path.
:param archive: The archive as ZipFile. :param archive: The archive as ZipFile.
@ -148,7 +147,7 @@ class Backup:
""" """
Logger.log("d", "Removing current data in location: %s", target_path) Logger.log("d", "Removing current data in location: %s", target_path)
try: try:
shutil.rmtree(target_path) shutil.rmtree(target_path, onerror=self._onRemoveError)
except PermissionError as error: except PermissionError as error:
# This happens if a file is already opened by another program, usually only the log file. # 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. # 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) Logger.log("d", "Extracting backup to location: %s", target_path)
archive.extractall(target_path) archive.extractall(target_path)
return True 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)