Backup.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import io
  4. import os
  5. import re
  6. import shutil
  7. from copy import deepcopy
  8. from zipfile import ZipFile, ZIP_DEFLATED, BadZipfile
  9. from typing import Dict, Optional, TYPE_CHECKING, List
  10. from UM import i18nCatalog
  11. from UM.Logger import Logger
  12. from UM.Message import Message
  13. from UM.Platform import Platform
  14. from UM.Resources import Resources
  15. from UM.Version import Version
  16. if TYPE_CHECKING:
  17. from cura.CuraApplication import CuraApplication
  18. class Backup:
  19. """The back-up class holds all data about a back-up.
  20. It is also responsible for reading and writing the zip file to the user data folder.
  21. """
  22. IGNORED_FILES = [r"cura\.log", r"plugins\.json", r"cache", r"__pycache__", r"\.qmlc", r"\.pyc"]
  23. """These files should be ignored when making a backup."""
  24. IGNORED_FOLDERS = [] # type: List[str]
  25. SECRETS_SETTINGS = ["general/ultimaker_auth_data"]
  26. """Secret preferences that need to obfuscated when making a backup of Cura"""
  27. catalog = i18nCatalog("cura")
  28. """Re-use translation catalog"""
  29. def __init__(self, application: "CuraApplication", zip_file: bytes = None, meta_data: Dict[str, str] = None) -> None:
  30. self._application = application
  31. self.zip_file = zip_file # type: Optional[bytes]
  32. self.meta_data = meta_data # type: Optional[Dict[str, str]]
  33. def makeFromCurrent(self) -> None:
  34. """Create a back-up from the current user config folder."""
  35. cura_release = self._application.getVersion()
  36. version_data_dir = Resources.getDataStoragePath()
  37. Logger.log("d", "Creating backup for Cura %s, using folder %s", cura_release, version_data_dir)
  38. # obfuscate sensitive secrets
  39. secrets = self._obfuscate()
  40. # Ensure all current settings are saved.
  41. self._application.saveSettings()
  42. # We copy the preferences file to the user data directory in Linux as it's in a different location there.
  43. # When restoring a backup on Linux, we move it back.
  44. if Platform.isLinux(): #TODO: This should check for the config directory not being the same as the data directory, rather than hard-coding that to Linux systems.
  45. preferences_file_name = self._application.getApplicationName()
  46. preferences_file = Resources.getPath(Resources.Preferences, "{}.cfg".format(preferences_file_name))
  47. backup_preferences_file = os.path.join(version_data_dir, "{}.cfg".format(preferences_file_name))
  48. if os.path.exists(preferences_file) and (not os.path.exists(backup_preferences_file) or not os.path.samefile(preferences_file, backup_preferences_file)):
  49. Logger.log("d", "Copying preferences file from %s to %s", preferences_file, backup_preferences_file)
  50. shutil.copyfile(preferences_file, backup_preferences_file)
  51. # Create an empty buffer and write the archive to it.
  52. buffer = io.BytesIO()
  53. archive = self._makeArchive(buffer, version_data_dir)
  54. if archive is None:
  55. return
  56. files = archive.namelist()
  57. # Count the metadata items. We do this in a rather naive way at the moment.
  58. machine_count = max(len([s for s in files if "machine_instances/" in s]) - 1, 0) # If people delete their profiles but not their preferences, it can still make a backup, and report -1 profiles. Server crashes on this.
  59. material_count = max(len([s for s in files if "materials/" in s]) - 1, 0)
  60. profile_count = max(len([s for s in files if "quality_changes/" in s]) - 1, 0)
  61. # We don't store plugins anymore, since if you can make backups, you have an account (and the plugins are
  62. # on the marketplace anyway)
  63. plugin_count = 0
  64. # Store the archive and metadata so the BackupManager can fetch them when needed.
  65. self.zip_file = buffer.getvalue()
  66. self.meta_data = {
  67. "cura_release": cura_release,
  68. "machine_count": str(machine_count),
  69. "material_count": str(material_count),
  70. "profile_count": str(profile_count),
  71. "plugin_count": str(plugin_count)
  72. }
  73. # Restore the obfuscated settings
  74. self._illuminate(**secrets)
  75. def _makeArchive(self, buffer: "io.BytesIO", root_path: str) -> Optional[ZipFile]:
  76. """Make a full archive from the given root path with the given name.
  77. :param root_path: The root directory to archive recursively.
  78. :return: The archive as bytes.
  79. """
  80. ignore_string = re.compile("|".join(self.IGNORED_FILES + self.IGNORED_FOLDERS))
  81. try:
  82. archive = ZipFile(buffer, "w", ZIP_DEFLATED)
  83. for root, folders, files in os.walk(root_path):
  84. for item_name in folders + files:
  85. absolute_path = os.path.join(root, item_name)
  86. if ignore_string.search(absolute_path):
  87. continue
  88. archive.write(absolute_path, absolute_path[len(root_path) + len(os.sep):])
  89. archive.close()
  90. return archive
  91. except (IOError, OSError, BadZipfile) as error:
  92. Logger.log("e", "Could not create archive from user data directory: %s", error)
  93. self._showMessage(self.catalog.i18nc("@info:backup_failed",
  94. "Could not create archive from user data directory: {}".format(error)),
  95. message_type = Message.MessageType.ERROR)
  96. return None
  97. def _showMessage(self, message: str, message_type: Message.MessageType = Message.MessageType.NEUTRAL) -> None:
  98. """Show a UI message."""
  99. Message(message, title=self.catalog.i18nc("@info:title", "Backup"), message_type = message_type).show()
  100. def restore(self) -> bool:
  101. """Restore this back-up.
  102. :return: Whether we had success or not.
  103. """
  104. if not self.zip_file or not self.meta_data or not self.meta_data.get("cura_release", None):
  105. # We can restore without the minimum required information.
  106. Logger.log("w", "Tried to restore a Cura backup without having proper data or meta data.")
  107. self._showMessage(self.catalog.i18nc("@info:backup_failed",
  108. "Tried to restore a Cura backup without having proper data or meta data."),
  109. message_type = Message.MessageType.ERROR)
  110. return False
  111. current_version = Version(self._application.getVersion())
  112. version_to_restore = Version(self.meta_data.get("cura_release", "master"))
  113. if current_version < version_to_restore:
  114. # Cannot restore version newer than current because settings might have changed.
  115. Logger.log("d", "Tried to restore a Cura backup of version {version_to_restore} with cura version {current_version}".format(version_to_restore = version_to_restore, current_version = current_version))
  116. self._showMessage(self.catalog.i18nc("@info:backup_failed",
  117. "Tried to restore a Cura backup that is higher than the current version."),
  118. message_type = Message.MessageType.ERROR)
  119. return False
  120. # Get the current secrets and store since the back-up doesn't contain those
  121. secrets = self._obfuscate()
  122. version_data_dir = Resources.getDataStoragePath()
  123. try:
  124. archive = ZipFile(io.BytesIO(self.zip_file), "r")
  125. except LookupError as e:
  126. Logger.log("d", f"The following error occurred while trying to restore a Cura backup: {str(e)}")
  127. Message(self.catalog.i18nc("@info:backup_failed",
  128. "The following error occurred while trying to restore a Cura backup:") + str(e),
  129. title = self.catalog.i18nc("@info:title", "Backup"),
  130. message_type = Message.MessageType.ERROR).show()
  131. return False
  132. extracted = self._extractArchive(archive, version_data_dir)
  133. # Under Linux, preferences are stored elsewhere, so we copy the file to there.
  134. if Platform.isLinux():
  135. preferences_file_name = self._application.getApplicationName()
  136. preferences_file = Resources.getPath(Resources.Preferences, "{}.cfg".format(preferences_file_name))
  137. backup_preferences_file = os.path.join(version_data_dir, "{}.cfg".format(preferences_file_name))
  138. Logger.log("d", "Moving preferences file from %s to %s", backup_preferences_file, preferences_file)
  139. try:
  140. shutil.move(backup_preferences_file, preferences_file)
  141. except EnvironmentError as e:
  142. Logger.error(f"Unable to back-up preferences file: {type(e)} - {str(e)}")
  143. # Read the preferences from the newly restored configuration (or else the cached Preferences will override the restored ones)
  144. self._application.readPreferencesFromConfiguration()
  145. # Restore the obfuscated settings
  146. self._illuminate(**secrets)
  147. return extracted
  148. def _extractArchive(self, archive: "ZipFile", target_path: str) -> bool:
  149. """Extract the whole archive to the given target path.
  150. :param archive: The archive as ZipFile.
  151. :param target_path: The target path.
  152. :return: Whether we had success or not.
  153. """
  154. # Implement security recommendations: Sanity check on zip files will make it harder to spoof.
  155. from cura.CuraApplication import CuraApplication
  156. config_filename = CuraApplication.getInstance().getApplicationName() + ".cfg" # Should be there if valid.
  157. if config_filename not in [file.filename for file in archive.filelist]:
  158. Logger.logException("e", "Unable to extract the backup due to corruption of compressed file(s).")
  159. return False
  160. Logger.log("d", "Removing current data in location: %s", target_path)
  161. Resources.factoryReset()
  162. Logger.log("d", "Extracting backup to location: %s", target_path)
  163. name_list = archive.namelist()
  164. ignore_string = re.compile("|".join(self.IGNORED_FILES + self.IGNORED_FOLDERS))
  165. for archive_filename in name_list:
  166. if ignore_string.search(archive_filename):
  167. Logger.warning(f"File ({archive_filename}) in archive that doesn't fit current backup policy; ignored.")
  168. continue
  169. try:
  170. archive.extract(archive_filename, target_path)
  171. except (PermissionError, EnvironmentError):
  172. Logger.logException("e", f"Unable to extract the file {archive_filename} from the backup due to permission or file system errors.")
  173. except UnicodeEncodeError:
  174. Logger.error(f"Unable to extract the file {archive_filename} because of an encoding error.")
  175. CuraApplication.getInstance().processEvents()
  176. return True
  177. def _obfuscate(self) -> Dict[str, str]:
  178. """
  179. Obfuscate and remove the secret preferences that are specified in SECRETS_SETTINGS
  180. :return: a dictionary of the removed secrets. Note: the '/' is replaced by '__'
  181. """
  182. preferences = self._application.getPreferences()
  183. secrets = {}
  184. for secret in self.SECRETS_SETTINGS:
  185. secrets[secret.replace("/", "__")] = deepcopy(preferences.getValue(secret))
  186. preferences.setValue(secret, None)
  187. self._application.savePreferences()
  188. return secrets
  189. def _illuminate(self, **kwargs) -> None:
  190. """
  191. Restore the obfuscated settings
  192. :param kwargs: a dict of obscured preferences. Note: the '__' of the keys will be replaced by '/'
  193. """
  194. preferences = self._application.getPreferences()
  195. for key, value in kwargs.items():
  196. preferences.setValue(key.replace("__", "/"), value)
  197. self._application.savePreferences()