CuraPackageManager.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. import json
  5. import os
  6. import shutil
  7. import zipfile
  8. import tempfile
  9. from PyQt5.QtCore import pyqtSlot, QObject, pyqtSignal
  10. from UM.Application import Application
  11. from UM.Logger import Logger
  12. from UM.Resources import Resources
  13. from UM.Version import Version
  14. class CuraPackageManager(QObject):
  15. # The prefix that's added to all files for an installed package to avoid naming conflicts with user created
  16. # files.
  17. PREFIX_PLACE_HOLDER = "-CP;"
  18. def __init__(self, parent = None):
  19. super().__init__(parent)
  20. self._application = parent
  21. self._container_registry = self._application.getContainerRegistry()
  22. self._plugin_registry = self._application.getPluginRegistry()
  23. # JSON file that keeps track of all installed packages.
  24. self._package_management_file_path = os.path.join(os.path.abspath(Resources.getDataStoragePath()),
  25. "packages.json")
  26. self._installed_package_dict = {} # a dict of all installed packages
  27. self._to_remove_package_set = set() # a set of packages that need to be removed at the next start
  28. self._to_install_package_dict = {} # a dict of packages that need to be installed at the next start
  29. installedPackagesChanged = pyqtSignal() # Emitted whenever the installed packages collection have been changed.
  30. def initialize(self):
  31. self._loadManagementData()
  32. self._removeAllScheduledPackages()
  33. self._installAllScheduledPackages()
  34. # (for initialize) Loads the package management file if exists
  35. def _loadManagementData(self) -> None:
  36. if not os.path.exists(self._package_management_file_path):
  37. Logger.log("i", "Package management file %s doesn't exist, do nothing", self._package_management_file_path)
  38. return
  39. # Need to use the file lock here to prevent concurrent I/O from other processes/threads
  40. container_registry = self._application.getContainerRegistry()
  41. with container_registry.lockFile():
  42. with open(self._package_management_file_path, "r", encoding = "utf-8") as f:
  43. management_dict = json.load(f, encoding = "utf-8")
  44. self._installed_package_dict = management_dict.get("installed", {})
  45. self._to_remove_package_set = set(management_dict.get("to_remove", []))
  46. self._to_install_package_dict = management_dict.get("to_install", {})
  47. Logger.log("i", "Package management file %s is loaded", self._package_management_file_path)
  48. def _saveManagementData(self) -> None:
  49. # Need to use the file lock here to prevent concurrent I/O from other processes/threads
  50. container_registry = self._application.getContainerRegistry()
  51. with container_registry.lockFile():
  52. with open(self._package_management_file_path, "w", encoding = "utf-8") as f:
  53. data_dict = {"installed": self._installed_package_dict,
  54. "to_remove": list(self._to_remove_package_set),
  55. "to_install": self._to_install_package_dict}
  56. data_dict["to_remove"] = list(data_dict["to_remove"])
  57. json.dump(data_dict, f)
  58. Logger.log("i", "Package management file %s is saved", self._package_management_file_path)
  59. # (for initialize) Removes all packages that have been scheduled to be removed.
  60. def _removeAllScheduledPackages(self) -> None:
  61. for package_id in self._to_remove_package_set:
  62. self._purgePackage(package_id)
  63. self._to_remove_package_set.clear()
  64. self._saveManagementData()
  65. # (for initialize) Installs all packages that have been scheduled to be installed.
  66. def _installAllScheduledPackages(self) -> None:
  67. for package_id, installation_package_data in self._to_install_package_dict.items():
  68. self._installPackage(installation_package_data)
  69. self._to_install_package_dict.clear()
  70. self._saveManagementData()
  71. # Checks the given package is installed. If so, return a dictionary that contains the package's information.
  72. def getInstalledPackageInfo(self, package_id: str) -> Optional[dict]:
  73. if package_id in self._to_remove_package_set:
  74. return None
  75. if package_id in self._to_install_package_dict:
  76. package_info = self._to_install_package_dict[package_id]["package_info"]
  77. package_info["is_bundled"] = False
  78. return package_info
  79. if package_id in self._installed_package_dict:
  80. package_info = self._installed_package_dict.get(package_id)
  81. package_info["is_bundled"] = False
  82. return package_info
  83. for section, packages in self.getAllInstalledPackagesInfo().items():
  84. for package in packages:
  85. if package["package_id"] == package_id:
  86. package_info = package
  87. return package_info
  88. return None
  89. def getAllInstalledPackagesInfo(self, includeRequired: bool = False) -> dict:
  90. installed_package_id_set = set(self._installed_package_dict.keys()) | set(self._to_install_package_dict.keys())
  91. installed_package_id_set = installed_package_id_set.difference(self._to_remove_package_set)
  92. managed_package_id_set = set(installed_package_id_set) | self._to_remove_package_set
  93. # TODO: For absolutely no reason, this function seems to run in a loop
  94. # even though no loop is ever called with it.
  95. # map of <package_type> -> <package_id> -> <package_info>
  96. installed_packages_dict = {}
  97. for package_id in installed_package_id_set:
  98. if package_id in Application.getInstance().getRequiredPlugins():
  99. continue
  100. if package_id in self._to_install_package_dict:
  101. package_info = self._to_install_package_dict[package_id]["package_info"]
  102. else:
  103. package_info = self._installed_package_dict[package_id]
  104. package_info["is_bundled"] = False
  105. package_type = package_info["package_type"]
  106. if package_type not in installed_packages_dict:
  107. installed_packages_dict[package_type] = []
  108. installed_packages_dict[package_type].append( package_info )
  109. # We also need to get information from the plugin registry such as if a plugin is active
  110. package_info["is_active"] = self._plugin_registry.isActivePlugin(package_id)
  111. # Also get all bundled plugins
  112. all_metadata = self._plugin_registry.getAllMetaData()
  113. for item in all_metadata:
  114. plugin_package_info = self.__convertPluginMetadataToPackageMetadata(item)
  115. # Only gather the bundled plugins here.
  116. package_id = plugin_package_info["package_id"]
  117. if package_id in managed_package_id_set:
  118. continue
  119. if package_id in Application.getInstance().getRequiredPlugins():
  120. continue
  121. plugin_package_info["is_bundled"] = True if plugin_package_info["author"]["display_name"] == "Ultimaker B.V." else False
  122. plugin_package_info["is_active"] = self._plugin_registry.isActivePlugin(package_id)
  123. package_type = "plugin"
  124. if package_type not in installed_packages_dict:
  125. installed_packages_dict[package_type] = []
  126. installed_packages_dict[package_type].append( plugin_package_info )
  127. return installed_packages_dict
  128. def __convertPluginMetadataToPackageMetadata(self, plugin_metadata: dict) -> dict:
  129. package_metadata = {"package_id": plugin_metadata["id"],
  130. "package_type": "plugin",
  131. "display_name": plugin_metadata["plugin"]["name"],
  132. "description": plugin_metadata["plugin"].get("description"),
  133. "package_version": plugin_metadata["plugin"]["version"],
  134. "cura_version": int(plugin_metadata["plugin"]["api"]),
  135. "website": "",
  136. "author": {
  137. "author_id": plugin_metadata["plugin"].get("author", ""),
  138. "display_name": plugin_metadata["plugin"].get("author", ""),
  139. "email": "",
  140. "website": "",
  141. },
  142. "tags": ["plugin"],
  143. }
  144. return package_metadata
  145. # Checks if the given package is installed.
  146. def isPackageInstalled(self, package_id: str) -> bool:
  147. return self.getInstalledPackageInfo(package_id) is not None
  148. # Schedules the given package file to be installed upon the next start.
  149. @pyqtSlot(str)
  150. def installPackage(self, filename: str) -> None:
  151. # Get package information
  152. package_info = self.getPackageInfo(filename)
  153. package_id = package_info["package_id"]
  154. has_changes = False
  155. # Check the delayed installation and removal lists first
  156. if package_id in self._to_remove_package_set:
  157. self._to_remove_package_set.remove(package_id)
  158. has_changes = True
  159. # Check if it is installed
  160. installed_package_info = self.getInstalledPackageInfo(package_info["package_id"])
  161. to_install_package = installed_package_info is None # Install if the package has not been installed
  162. if installed_package_info is not None:
  163. # Compare versions and only schedule the installation if the given package is newer
  164. new_version = package_info["package_version"]
  165. installed_version = installed_package_info["package_version"]
  166. if Version(new_version) > Version(installed_version):
  167. Logger.log("i", "Package [%s] version [%s] is newer than the installed version [%s], update it.",
  168. package_id, new_version, installed_version)
  169. to_install_package = True
  170. if to_install_package:
  171. Logger.log("i", "Package [%s] version [%s] is scheduled to be installed.",
  172. package_id, package_info["package_version"])
  173. # Copy the file to cache dir so we don't need to rely on the original file to be present
  174. package_cache_dir = os.path.join(os.path.abspath(Resources.getCacheStoragePath()), "cura_packages")
  175. if not os.path.exists(package_cache_dir):
  176. os.makedirs(package_cache_dir, exist_ok=True)
  177. target_file_path = os.path.join(package_cache_dir, package_id + ".curapackage")
  178. shutil.copy2(filename, target_file_path)
  179. self._to_install_package_dict[package_id] = {"package_info": package_info,
  180. "filename": target_file_path}
  181. has_changes = True
  182. self._saveManagementData()
  183. if has_changes:
  184. self.installedPackagesChanged.emit()
  185. # Schedules the given package to be removed upon the next start.
  186. @pyqtSlot(str)
  187. def removePackage(self, package_id: str) -> None:
  188. # Check the delayed installation and removal lists first
  189. if not self.isPackageInstalled(package_id):
  190. Logger.log("i", "Attempt to remove package [%s] that is not installed, do nothing.", package_id)
  191. return
  192. # Remove from the delayed installation list if present
  193. if package_id in self._to_install_package_dict:
  194. del self._to_install_package_dict[package_id]
  195. # Schedule for a delayed removal:
  196. self._to_remove_package_set.add(package_id)
  197. self._saveManagementData()
  198. self.installedPackagesChanged.emit()
  199. # Removes everything associated with the given package ID.
  200. def _purgePackage(self, package_id: str) -> None:
  201. # Get all folders that need to be checked for installed packages, including:
  202. # - materials
  203. # - qualities
  204. # - plugins
  205. from cura.CuraApplication import CuraApplication
  206. dirs_to_check = [
  207. Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer),
  208. Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer),
  209. os.path.join(os.path.abspath(Resources.getDataStoragePath()), "plugins"),
  210. ]
  211. for root_dir in dirs_to_check:
  212. package_dir = os.path.join(root_dir, package_id)
  213. if os.path.exists(package_dir):
  214. Logger.log("i", "Removing '%s' for package [%s]", package_dir, package_id)
  215. shutil.rmtree(package_dir)
  216. # Installs all files associated with the given package.
  217. def _installPackage(self, installation_package_data: dict):
  218. package_info = installation_package_data["package_info"]
  219. filename = installation_package_data["filename"]
  220. package_id = package_info["package_id"]
  221. if not os.path.exists(filename):
  222. Logger.log("w", "Package [%s] file '%s' is missing, cannot install this package", package_id, filename)
  223. return
  224. Logger.log("i", "Installing package [%s] from file [%s]", package_id, filename)
  225. # If it's installed, remove it first and then install
  226. if package_id in self._installed_package_dict:
  227. self._purgePackage(package_id)
  228. # Install the package
  229. archive = zipfile.ZipFile(filename, "r")
  230. temp_dir = tempfile.TemporaryDirectory()
  231. archive.extractall(temp_dir.name)
  232. from cura.CuraApplication import CuraApplication
  233. installation_dirs_dict = {
  234. "materials": Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer),
  235. "quality": Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer),
  236. "plugins": os.path.join(os.path.abspath(Resources.getDataStoragePath()), "plugins"),
  237. }
  238. for sub_dir_name, installation_root_dir in installation_dirs_dict.items():
  239. src_dir_path = os.path.join(temp_dir.name, "files", sub_dir_name)
  240. dst_dir_path = os.path.join(installation_root_dir, package_id)
  241. if not os.path.exists(src_dir_path):
  242. continue
  243. # Need to rename the container files so they don't get ID conflicts
  244. to_rename_files = sub_dir_name not in ("plugins",)
  245. self.__installPackageFiles(package_id, src_dir_path, dst_dir_path, need_to_rename_files= to_rename_files)
  246. archive.close()
  247. # Remove the file
  248. os.remove(filename)
  249. def __installPackageFiles(self, package_id: str, src_dir: str, dst_dir: str, need_to_rename_files: bool = True) -> None:
  250. shutil.move(src_dir, dst_dir)
  251. # Rename files if needed
  252. if not need_to_rename_files:
  253. return
  254. for root, _, file_names in os.walk(dst_dir):
  255. for filename in file_names:
  256. new_filename = self.PREFIX_PLACE_HOLDER + package_id + "-" + filename
  257. old_file_path = os.path.join(root, filename)
  258. new_file_path = os.path.join(root, new_filename)
  259. os.rename(old_file_path, new_file_path)
  260. # Gets package information from the given file.
  261. def getPackageInfo(self, filename: str) -> dict:
  262. archive = zipfile.ZipFile(filename, "r")
  263. try:
  264. # All information is in package.json
  265. with archive.open("package.json", "r") as f:
  266. package_info_dict = json.loads(f.read().decode("utf-8"))
  267. return package_info_dict
  268. except Exception as e:
  269. raise RuntimeError("Could not get package information from file '%s': %s" % (filename, e))
  270. finally:
  271. archive.close()
  272. # Gets the license file content if present in the given package file.
  273. # Returns None if there is no license file found.
  274. def getPackageLicense(self, filename: str) -> Optional[str]:
  275. license_string = None
  276. archive = zipfile.ZipFile(filename)
  277. try:
  278. # Go through all the files and use the first successful read as the result
  279. for file_info in archive.infolist():
  280. if file_info.is_dir() or not file_info.filename.startswith("files/"):
  281. continue
  282. filename_parts = os.path.basename(file_info.filename.lower()).split(".")
  283. stripped_filename = filename_parts[0]
  284. if stripped_filename in ("license", "licence"):
  285. Logger.log("i", "Found potential license file '%s'", file_info.filename)
  286. try:
  287. with archive.open(file_info.filename, "r") as f:
  288. data = f.read()
  289. license_string = data.decode("utf-8")
  290. break
  291. except:
  292. Logger.logException("e", "Failed to load potential license file '%s' as text file.",
  293. file_info.filename)
  294. license_string = None
  295. except Exception as e:
  296. raise RuntimeError("Could not get package license from file '%s': %s" % (filename, e))
  297. finally:
  298. archive.close()
  299. return license_string