diff --git a/cura/Backups/Backup.py b/cura/Backups/Backup.py index 2bf945ac64..3cdcfa8e23 100644 --- a/cura/Backups/Backup.py +++ b/cura/Backups/Backup.py @@ -2,6 +2,8 @@ # Cura is released under the terms of the LGPLv3 or higher. import io import os +import shutil + from typing import Optional from zipfile import ZipFile, ZIP_DEFLATED, BadZipfile @@ -17,8 +19,7 @@ class Backup: """ # These files should be ignored when making a backup. - # Cura.cfg might contain secret data, so we don't back it up for now. - IGNORED_FILES = {"cura.log", "cura.cfg"} + IGNORED_FILES = {"cura.log"} def __init__(self, zip_file: bytes = None, meta_data: dict = None): self.zip_file = zip_file # type: Optional[bytes] @@ -76,14 +77,31 @@ class Backup: Logger.log("e", "Could not create archive from user data directory: %s", error) return None - def restore(self) -> None: + def restore(self) -> bool: """ Restore this backup. """ if not self.zip_file or not self.meta_data or not self.meta_data.get("cura_release", None): # We can restore without the minimum required information. Logger.log("w", "Tried to restore a Cura backup without having proper data or meta data.") - return + return False # global_data_dir = os.path.dirname(version_data_dir) - # TODO: restore logic. + # TODO: handle restoring older data version. + + version_data_dir = Resources.getDataStoragePath() + archive = ZipFile(io.BytesIO(self.zip_file), "r") + extracted = self._extractArchive(archive, version_data_dir) + if not extracted: + return False + return True + + @staticmethod + def _extractArchive(archive: "ZipFile", target_path: str) -> bool: + Logger.log("d", "Removing current data in location: %s", target_path) + shutil.rmtree(target_path) + + Logger.log("d", "Extracting backup to location: %s", target_path) + archive.extractall(target_path) + + return True diff --git a/cura/Backups/BackupsManager.py b/cura/Backups/BackupsManager.py index bb52ad57ba..55430126dd 100644 --- a/cura/Backups/BackupsManager.py +++ b/cura/Backups/BackupsManager.py @@ -4,6 +4,7 @@ from typing import Optional from UM.Logger import Logger from cura.Backups.Backup import Backup +from cura.CuraApplication import CuraApplication class BackupsManager: @@ -40,7 +41,10 @@ class BackupsManager: self._disableAutoSave() backup = Backup(zip_file = zip_file, meta_data = meta_data) - backup.restore() # At this point, Cura will need to restart for the changes to take effect + restored = backup.restore() + if restored: + # At this point, Cura will need to restart for the changes to take effect. + CuraApplication.getInstance().windowClosed() def _disableAutoSave(self): """Here we try to disable the auto-save plugin as it might interfere with restoring a backup."""