CuraPackageManager.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import glob
  4. import os
  5. from pathlib import Path
  6. from typing import Any, cast, Dict, List, Set, Tuple, TYPE_CHECKING, Optional
  7. from UM.Logger import Logger
  8. from UM.PluginRegistry import PluginRegistry
  9. from cura.CuraApplication import CuraApplication # To find some resource types.
  10. from cura.Settings.GlobalStack import GlobalStack
  11. from UM.PackageManager import PackageManager # The class we're extending.
  12. from UM.Resources import Resources # To find storage paths for some resource types.
  13. from UM.i18n import i18nCatalog
  14. from urllib.parse import unquote_plus
  15. catalog = i18nCatalog("cura")
  16. if TYPE_CHECKING:
  17. from UM.Qt.QtApplication import QtApplication
  18. from PyQt6.QtCore import QObject
  19. class CuraPackageManager(PackageManager):
  20. def __init__(self, application: "QtApplication", parent: Optional["QObject"] = None) -> None:
  21. super().__init__(application, parent)
  22. self._local_packages: Optional[List[Dict[str, Any]]] = None
  23. self._local_packages_ids: Optional[Set[str]] = None
  24. self.installedPackagesChanged.connect(self._updateLocalPackages)
  25. def _updateLocalPackages(self) -> None:
  26. self._local_packages = self.getAllLocalPackages()
  27. self._local_packages_ids = set(pkg["package_id"] for pkg in self._local_packages)
  28. @property
  29. def local_packages(self) -> List[Dict[str, Any]]:
  30. """locally installed packages, lazy execution"""
  31. if self._local_packages is None:
  32. self._updateLocalPackages()
  33. # _updateLocalPackages always results in a list of packages, not None.
  34. # It's guaranteed to be a list now.
  35. return cast(List[Dict[str, Any]], self._local_packages)
  36. @property
  37. def local_packages_ids(self) -> Set[str]:
  38. """locally installed packages, lazy execution"""
  39. if self._local_packages_ids is None:
  40. self._updateLocalPackages()
  41. # _updateLocalPackages always results in a list of packages, not None.
  42. # It's guaranteed to be a list now.
  43. return cast(Set[str], self._local_packages_ids)
  44. def initialize(self) -> None:
  45. self._installation_dirs_dict["materials"] = Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer)
  46. self._installation_dirs_dict["qualities"] = Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  47. self._installation_dirs_dict["variants"] = Resources.getStoragePath(CuraApplication.ResourceTypes.VariantInstanceContainer)
  48. # Due to a bug in Cura 5.1.0 we needed to change the directory structure of the curapackage on the server side (See SD-3871).
  49. # Although the material intent profiles will be installed in the `intent` folder, the curapackage from the server side will
  50. # have an `intents` folder. For completeness, we will look in both locations of in the curapackage and map them both to the
  51. # `intent` folder.
  52. self._installation_dirs_dict["intents"] = Resources.getStoragePath(CuraApplication.ResourceTypes.IntentInstanceContainer)
  53. self._installation_dirs_dict["intent"] = Resources.getStoragePath(CuraApplication.ResourceTypes.IntentInstanceContainer)
  54. super().initialize()
  55. def isMaterialBundled(self, file_name: str, guid: str):
  56. """ Check if there is a bundled material name with file_name and guid """
  57. for path in Resources.getSecureSearchPaths():
  58. # Secure search paths are install directory paths, if a material is in here it must be bundled.
  59. paths = [Path(p) for p in glob.glob(path + '/**/*.xml.fdm_material', recursive=True)]
  60. for material in paths:
  61. if material.name == file_name:
  62. Logger.info(f"Found bundled material: {material.name}. Located in path: {str(material)}")
  63. with open(material, encoding="utf-8") as f:
  64. # Make sure the file we found has the same guid as our material
  65. # Parsing this xml would be better but the namespace is needed to search it.
  66. parsed_guid = PluginRegistry.getInstance().getPluginObject(
  67. "XmlMaterialProfile").getMetadataFromSerialized(
  68. f.read(), "GUID")
  69. if guid == parsed_guid:
  70. # The material we found matches both filename and GUID
  71. return True
  72. return False
  73. def getMaterialFilePackageId(self, file_name: str, guid: str) -> str:
  74. """Get the id of the installed material package that contains file_name"""
  75. file_name = unquote_plus(file_name)
  76. for material_package in [f for f in os.scandir(self._installation_dirs_dict["materials"]) if f.is_dir()]:
  77. package_id = material_package.name
  78. for root, _, file_names in os.walk(material_package.path):
  79. if file_name not in file_names:
  80. # File with the name we are looking for is not in this directory
  81. continue
  82. with open(os.path.join(root, file_name), encoding="utf-8") as f:
  83. # Make sure the file we found has the same guid as our material
  84. # Parsing this xml would be better but the namespace is needed to search it.
  85. parsed_guid = PluginRegistry.getInstance().getPluginObject("XmlMaterialProfile").getMetadataFromSerialized(
  86. f.read(), "GUID")
  87. if guid == parsed_guid:
  88. return package_id
  89. Logger.error("Could not find package_id for file: {} with GUID: {} ".format(file_name, guid))
  90. Logger.error(f"Bundled paths searched: {list(Resources.getSecureSearchPaths())}")
  91. return ""
  92. def getMachinesUsingPackage(self, package_id: str) -> Tuple[List[Tuple[GlobalStack, str, str]], List[Tuple[GlobalStack, str, str]]]:
  93. """Returns a list of where the package is used
  94. It loops through all the package contents and see if some of the ids are used.
  95. :param package_id: package id to search for
  96. :return: empty if it is never used, otherwise a list consisting of 3-tuples
  97. """
  98. ids = self.getPackageContainerIds(package_id)
  99. container_stacks = self._application.getContainerRegistry().findContainerStacks()
  100. global_stacks = [container_stack for container_stack in container_stacks if isinstance(container_stack, GlobalStack)]
  101. machine_with_materials = []
  102. machine_with_qualities = []
  103. for container_id in ids:
  104. for global_stack in global_stacks:
  105. for extruder_nr, extruder_stack in enumerate(global_stack.extruderList):
  106. if container_id in (extruder_stack.material.getId(), extruder_stack.material.getMetaData().get("base_file")):
  107. machine_with_materials.append((global_stack, str(extruder_nr), container_id))
  108. if container_id == extruder_stack.quality.getId():
  109. machine_with_qualities.append((global_stack, str(extruder_nr), container_id))
  110. return machine_with_materials, machine_with_qualities
  111. def getAllLocalPackages(self) -> List[Dict[str, Any]]:
  112. """ Returns an unordered list of all the package_info of installed, to be installed, or bundled packages"""
  113. packages: List[Dict[str, Any]] = []
  114. for packages_to_add in self.getAllInstalledPackagesInfo().values():
  115. packages.extend(packages_to_add)
  116. return packages