Esempio n. 1
0
    def test_factoryReset(self):
        # FIXME: This is a temporary workaround. A proper fix should be to make the home directory configurable so a
        #        unique temporary directory can be used for each test and it can removed afterwards.
        # HACK: Record the number of files and directories in the data storage directory before the factory reset,
        # so after the reset, we can compare if there's a new ZIP file being created. Note that this will not always
        # work, especially when there are multiple tests running on the same host at the same time.
        original_filenames = os.listdir(
            os.path.dirname(Resources.getDataStoragePath()))

        Resources.factoryReset()
        # Check if the data is deleted!
        assert len(os.listdir(Resources.getDataStoragePath())) == 0

        # The data folder should still be there, but it should also have created a zip with the data it deleted.
        new_filenames = os.listdir(
            os.path.dirname(Resources.getDataStoragePath()))
        assert len(new_filenames) - len(original_filenames) == 1

        # Clean up after our ass.
        folder = os.path.dirname(Resources.getDataStoragePath())
        for file in os.listdir(folder):
            file_path = os.path.join(folder, file)
            try:
                os.unlink(file_path)
            except:
                pass
        folder = os.path.dirname(Resources.getDataStoragePath())
        for file in os.listdir(folder):
            file_path = os.path.join(folder, file)
            try:
                os.unlink(file_path)
            except:
                pass
Esempio n. 2
0
    def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
        """Extract the whole archive to the given target path.

        :param archive: The archive as ZipFile.
        :param target_path: The target path.
        :return: Whether we had success or not.
        """

        # Implement security recommendations: Sanity check on zip files will make it harder to spoof.
        from cura.CuraApplication import CuraApplication
        config_filename = CuraApplication.getInstance().getApplicationName(
        ) + ".cfg"  # Should be there if valid.
        if config_filename not in [file.filename for file in archive.filelist]:
            Logger.logException(
                "e",
                "Unable to extract the backup due to corruption of compressed file(s)."
            )
            return False

        Logger.log("d", "Removing current data in location: %s", target_path)
        Resources.factoryReset()
        Logger.log("d", "Extracting backup to location: %s", target_path)
        try:
            archive.extractall(target_path)
        except (PermissionError, EnvironmentError):
            Logger.logException(
                "e",
                "Unable to extract the backup due to permission or file system errors."
            )
            return False
        return True
 def _actionTriggered(self, _, action_id):
     if action_id == "reset":
         result = QMessageBox.question(None, i18n_catalog.i18nc("@title:window", "Reset to factory"),
                                       i18n_catalog.i18nc("@label",
                                                     "Reset will remove all your current printers and profiles! Are you sure you want to reset?"))
         if result == QMessageBox.Yes:
             Resources.factoryReset()
             sys.exit(1)
 def _actionTriggered(self, _, action_id):
     if action_id == "reset":
         result = QMessageBox.question(None, i18n_catalog.i18nc("@title:window", "Reset to factory"),
                                       i18n_catalog.i18nc("@label",
                                                     "Reset will remove all your current printers and profiles! Are you sure you want to reset?"))
         if result == QMessageBox.Yes:
             Resources.factoryReset()
             sys.exit(1)
Esempio n. 5
0
 def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
     Logger.log("d", "Removing current data in location: %s", target_path)
     Resources.factoryReset()
     Logger.log("d", "Extracting backup to location: %s", target_path)
     try:
         archive.extractall(target_path)
     except PermissionError:
         Logger.logException("e", "Unable to extract the backup due to permission errors")
         return False
     return True
Esempio n. 6
0
 def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
     Logger.log("d", "Removing current data in location: %s", target_path)
     Resources.factoryReset()
     Logger.log("d", "Extracting backup to location: %s", target_path)
     try:
         archive.extractall(target_path)
     except PermissionError:
         Logger.logException(
             "e", "Unable to extract the backup due to permission errors")
         return False
     return True
Esempio n. 7
0
 def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
     """
     Extract the whole archive to the given target path.
     :param archive: The archive as ZipFile.
     :param target_path: The target path.
     :return: A boolean whether we had success or not.
     """
     Logger.log("d", "Removing current data in location: %s", target_path)
     Resources.factoryReset()
     Logger.log("d", "Extracting backup to location: %s", target_path)
     archive.extractall(target_path)
     return True
Esempio n. 8
0
    def _extractArchive(self, archive: "ZipFile", target_path: str) -> bool:
        """Extract the whole archive to the given target path.

        :param archive: The archive as ZipFile.
        :param target_path: The target path.
        :return: Whether we had success or not.
        """

        # Implement security recommendations: Sanity check on zip files will make it harder to spoof.
        from cura.CuraApplication import CuraApplication
        config_filename = CuraApplication.getInstance().getApplicationName(
        ) + ".cfg"  # Should be there if valid.
        if config_filename not in [file.filename for file in archive.filelist]:
            Logger.logException(
                "e",
                "Unable to extract the backup due to corruption of compressed file(s)."
            )
            return False

        Logger.log("d", "Removing current data in location: %s", target_path)
        Resources.factoryReset()
        Logger.log("d", "Extracting backup to location: %s", target_path)
        name_list = archive.namelist()
        ignore_string = re.compile("|".join(self.IGNORED_FILES +
                                            self.IGNORED_FOLDERS))
        for archive_filename in name_list:
            if ignore_string.search(archive_filename):
                Logger.warning(
                    f"File ({archive_filename}) in archive that doesn't fit current backup policy; ignored."
                )
                continue
            try:
                archive.extract(archive_filename, target_path)
            except (PermissionError, EnvironmentError):
                Logger.logException(
                    "e",
                    f"Unable to extract the file {archive_filename} from the backup due to permission or file system errors."
                )
            except UnicodeEncodeError:
                Logger.error(
                    f"Unable to extract the file {archive_filename} because of an encoding error."
                )
            CuraApplication.getInstance().processEvents()
        return True
Esempio n. 9
0
    def test_factoryReset(self):
        Resources.factoryReset()
        # Check if the data is deleted!
        assert len(os.listdir(Resources.getDataStoragePath())) == 0

        # The data folder should still be there, but it should also have created a zip with the data it deleted.
        assert len(os.listdir(os.path.dirname(
            Resources.getDataStoragePath()))) == 2

        # Clean up after our ass.
        folder = os.path.dirname(Resources.getDataStoragePath())
        for file in os.listdir(folder):
            file_path = os.path.join(folder, file)
            try:
                os.unlink(file_path)
            except:
                pass
        folder = os.path.dirname(Resources.getDataStoragePath())
        for file in os.listdir(folder):
            file_path = os.path.join(folder, file)
            try:
                os.unlink(file_path)
            except:
                pass
Esempio n. 10
0
 def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
     Logger.log("d", "Removing current data in location: %s", target_path)
     Resources.factoryReset()
     Logger.log("d", "Extracting backup to location: %s", target_path)
     archive.extractall(target_path)
     return True
Esempio n. 11
0
    def _backupAndStartClean(self):
        """Backup the current resource directories and create clean ones."""

        Resources.factoryReset()
        self.early_crash_dialog.close()
Esempio n. 12
0
 def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
     Logger.log("d", "Removing current data in location: %s", target_path)
     Resources.factoryReset()
     Logger.log("d", "Extracting backup to location: %s", target_path)
     archive.extractall(target_path)
     return True
 def _actionTriggered(self, _, action_id):
     if action_id == "reset":
         Resources.factoryReset()
         sys.exit(1)
Esempio n. 14
0
 def _backupAndStartClean(self):
     Resources.factoryReset()
     self.early_crash_dialog.close()
Esempio n. 15
0
 def _backupAndStartClean(self):
     Resources.factoryReset()
     self.early_crash_dialog.close()