CuraPackageManager.py 18 KB

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