conanfile.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import os
  2. import sys
  3. from pathlib import Path
  4. from io import StringIO
  5. from platform import python_version
  6. from jinja2 import Template
  7. from conans import tools
  8. from conan import ConanFile
  9. from conan.tools import files
  10. from conan.tools.env import VirtualRunEnv
  11. from conan.errors import ConanInvalidConfiguration
  12. required_conan_version = ">=1.47.0"
  13. class CuraConan(ConanFile):
  14. name = "cura"
  15. license = "LGPL-3.0"
  16. author = "Ultimaker B.V."
  17. url = "https://github.com/Ultimaker/cura"
  18. description = "3D printer / slicing GUI built on top of the Uranium framework"
  19. topics = ("conan", "python", "pyqt5", "qt", "qml", "3d-printing", "slicer")
  20. build_policy = "missing"
  21. exports = "LICENSE*", "Ultimaker-Cura.spec.jinja", "CuraVersion.py.jinja"
  22. settings = "os", "compiler", "build_type", "arch"
  23. no_copy_source = True # We won't build so no need to copy sources to the build folder
  24. # FIXME: Remove specific branch once merged to main
  25. # Extending the conanfile with the UMBaseConanfile https://github.com/Ultimaker/conan-ultimaker-index/tree/CURA-9177_Fix_CI_CD/recipes/umbase
  26. python_requires = "umbase/0.1.2@ultimaker/testing"
  27. python_requires_extend = "umbase.UMBaseConanfile"
  28. options = {
  29. "enterprise": ["True", "False", "true", "false"], # Workaround for GH Action passing boolean as lowercase string
  30. "staging": ["True", "False", "true", "false"], # Workaround for GH Action passing boolean as lowercase string
  31. "devtools": [True, False], # FIXME: Split this up in testing and (development / build (pyinstaller) / system installer) tools
  32. "cloud_api_version": "ANY",
  33. "display_name": "ANY", # TODO: should this be an option??
  34. "cura_debug_mode": [True, False] # FIXME: Use profiles
  35. }
  36. default_options = {
  37. "enterprise": "False",
  38. "staging": "False",
  39. "devtools": False,
  40. "cloud_api_version": "1",
  41. "display_name": "Ultimaker Cura",
  42. "cura_debug_mode": False # Not yet implemented
  43. }
  44. scm = {
  45. "type": "git",
  46. "subfolder": ".",
  47. "url": "auto",
  48. "revision": "auto"
  49. }
  50. @property
  51. def _staging(self):
  52. return self.options.staging in ["True", 'true']
  53. @property
  54. def _enterprise(self):
  55. return self.options.enterprise in ["True", 'true']
  56. @property
  57. def _cloud_api_root(self):
  58. return "https://api-staging.ultimaker.com" if self._staging else "https://api.ultimaker.com"
  59. @property
  60. def _cloud_account_api_root(self):
  61. return "https://account-staging.ultimaker.com" if self._staging else "https://account.ultimaker.com"
  62. @property
  63. def _marketplace_root(self):
  64. return "https://marketplace-staging.ultimaker.com" if self._staging else "https://marketplace.ultimaker.com"
  65. @property
  66. def _digital_factory_url(self):
  67. return "https://digitalfactory-staging.ultimaker.com" if self._staging else "https://digitalfactory.ultimaker.com"
  68. @property
  69. def requirements_txts(self):
  70. if self.options.devtools:
  71. return ["requirements.txt", "requirements-ultimaker.txt", "requirements-dev.txt"]
  72. return ["requirements.txt", "requirements-ultimaker.txt"]
  73. @property
  74. def _base_dir(self):
  75. if self.install_folder is None:
  76. if self.build_folder is not None:
  77. return Path(self.build_folder)
  78. return Path(os.getcwd(), "venv")
  79. return Path(self.install_folder) # TODO: add base dir for running from source
  80. @property
  81. def _share_dir(self):
  82. return self._base_dir.joinpath("share")
  83. @property
  84. def _script_dir(self):
  85. if self.settings.os == "Windows":
  86. return self._base_dir.joinpath("Scripts")
  87. return self._base_dir.joinpath("bin")
  88. @property
  89. def _site_packages(self):
  90. if self.settings.os == "Windows":
  91. return self._base_dir.joinpath("Lib", "site-packages")
  92. py_version = tools.Version(self.deps_cpp_info["cpython"].version)
  93. return self._base_dir.joinpath("lib", f"python{py_version.major}.{py_version.minor}", "site-packages")
  94. @property
  95. def _py_interp(self):
  96. py_interp = self._script_dir.joinpath(Path(self.deps_user_info["cpython"].python).name)
  97. if self.settings.os == "Windows":
  98. py_interp = Path(*[f'"{p}"' if " " in p else p for p in py_interp.parts])
  99. return py_interp
  100. def _generate_cura_version(self, location):
  101. with open(Path(__file__).parent.joinpath("CuraVersion.py.jinja"), "r") as f:
  102. cura_version_py = Template(f.read())
  103. with open(Path(location, "CuraVersion.py"), "w") as f:
  104. f.write(cura_version_py.render(
  105. cura_app_name = self.name,
  106. cura_app_display_name = self.options.display_name,
  107. cura_version = self.version,
  108. cura_build_type = "Enterprise" if self._enterprise else "",
  109. cura_debug_mode = self.options.cura_debug_mode,
  110. cura_cloud_api_root = self._cloud_api_root,
  111. cura_cloud_api_version = self.options.cloud_api_version,
  112. cura_cloud_account_api_root = self._cloud_account_api_root,
  113. cura_marketplace_root = self._marketplace_root,
  114. cura_digital_factory_url = self._digital_factory_url))
  115. def _generate_pyinstaller_spec(self, location, entrypoint_location, icon_path):
  116. pyinstaller_metadata = self._um_data(self.version)["pyinstaller"]
  117. datas = []
  118. for data in pyinstaller_metadata["datas"].values():
  119. if "package" in data: # get the paths from conan package
  120. if data["package"] == self.name:
  121. src_path = Path(self.package_folder, data["src"])
  122. else:
  123. src_path = Path(self.deps_cpp_info[data["package"]].rootpath, data["src"])
  124. elif "root" in data: # get the paths relative from the sourcefolder
  125. src_path = Path(self.source_folder, data["root"], data["src"])
  126. else:
  127. continue
  128. if src_path.exists():
  129. datas.append((str(src_path), data["dst"]))
  130. binaries = []
  131. for binary in pyinstaller_metadata["binaries"].values():
  132. if "package" in binary: # get the paths from conan package
  133. src_path = Path(self.deps_cpp_info[binary["package"]].rootpath, binary["src"])
  134. elif "root" in binary: # get the paths relative from the sourcefolder
  135. src_path = Path(self.source_folder, binary["root"], binary["src"])
  136. else:
  137. continue
  138. if not src_path.exists():
  139. continue
  140. for bin in src_path.glob(binary["binary"] + ".*[exe|dll|so|dylib]"):
  141. binaries.append((str(bin), binary["dst"]))
  142. for bin in src_path.glob(binary["binary"]):
  143. binaries.append((str(bin), binary["dst"]))
  144. with open(Path(__file__).parent.joinpath("Ultimaker-Cura.spec.jinja"), "r") as f:
  145. pyinstaller = Template(f.read())
  146. with open(Path(location, "Ultimaker-Cura.spec"), "w") as f:
  147. f.write(pyinstaller.render(
  148. name = str(self.options.display_name).replace(" ", "-"),
  149. entrypoint = entrypoint_location,
  150. datas = datas,
  151. binaries = binaries,
  152. hiddenimports = pyinstaller_metadata["hiddenimports"],
  153. collect_all = pyinstaller_metadata["collect_all"],
  154. icon = icon_path
  155. ))
  156. def source(self):
  157. self._generate_cura_version(Path(self.source_folder, "cura"))
  158. def configure(self):
  159. self.options["arcus"].shared = True
  160. self.options["savitar"].shared = True
  161. self.options["pynest2d"].shared = True
  162. self.options["cpython"].shared = True
  163. def validate(self):
  164. if self.version and tools.Version(self.version) <= tools.Version("4"):
  165. raise ConanInvalidConfiguration("Only versions 5+ are support")
  166. def requirements(self):
  167. for req in self._um_data(self.version)["requirements"]:
  168. self.requires(req)
  169. def layout(self):
  170. self.folders.source = "."
  171. self.folders.build = "venv"
  172. self.folders.generators = Path(self.folders.build, "conan")
  173. self.cpp.package.libdirs = [os.path.join("site-packages", "cura")]
  174. self.cpp.package.bindirs = ["bin"]
  175. self.cpp.package.resdirs = ["resources", "plugins", "packaging", "pip_requirements"] # pip_requirements should be the last item in the list
  176. def generate(self):
  177. vr = VirtualRunEnv(self)
  178. vr.generate()
  179. if self.options.devtools:
  180. self._generate_pyinstaller_spec(self.generators_folder,
  181. os.path.join(self.source_folder, self._um_data(self.version)["runinfo"]["entrypoint"]),
  182. os.path.join(self.source_folder, "packaging", self._um_data(self.version)["pyinstaller"]["icon"][str(self.settings.os)]))
  183. def imports(self):
  184. self.copy("CuraEngine.exe", root_package = "curaengine", src = "@bindirs", dst = "", keep_path = False)
  185. self.copy("CuraEngine", root_package = "curaengine", src = "@bindirs", dst = "", keep_path = False)
  186. files.rmdir(self, "resources/materials")
  187. self.copy("*.fdm_material", root_package = "fdm_materials", src = "@resdirs", dst = "resources/materials", keep_path = False)
  188. self.copy("*.sig", root_package = "fdm_materials", src = "@resdirs", dst = "resources/materials", keep_path = False)
  189. # Copy resources of cura_binary_data
  190. self.copy("*", root_package = "cura_binary_data", src = self.deps_cpp_info["cura_binary_data"].resdirs[0],
  191. dst = "venv/share/cura", keep_path = True)
  192. self.copy("*", root_package = "cura_binary_data", src = self.deps_cpp_info["cura_binary_data"].resdirs[1],
  193. dst = "venv/share/uranium", keep_path = True)
  194. self.copy_deps("*.dll", src = "@bindirs", dst = self._site_packages)
  195. self.copy_deps("*.pyd", src = "@libdirs", dst = self._site_packages)
  196. self.copy_deps("*.pyi", src = "@libdirs", dst = self._site_packages)
  197. self.copy_deps("*.dylib", src = "@libdirs", dst = self._script_dir)
  198. def deploy(self):
  199. # Copy CuraEngine.exe to bindirs of Virtual Python Environment
  200. # TODO: Fix source such that it will get the curaengine relative from the executable (Python bindir in this case)
  201. self.copy_deps("CuraEngine.exe", root_package = "curaengine", src = self.deps_cpp_info["curaengine"].bindirs[0],
  202. dst = self._base_dir,
  203. keep_path = False)
  204. self.copy_deps("CuraEngine", root_package = "curaengine", src = self.deps_cpp_info["curaengine"].bindirs[0], dst = self._base_dir,
  205. keep_path = False)
  206. # Copy resources of Cura (keep folder structure)
  207. self.copy("*", src = self.cpp_info.bindirs[0], dst = self._base_dir, keep_path = False)
  208. self.copy("*", src = self.cpp_info.libdirs[0], dst = self._site_packages.joinpath("cura"), keep_path = True)
  209. self.copy("*", src = self.cpp_info.resdirs[0], dst = self._share_dir.joinpath("cura", "resources"), keep_path = True)
  210. self.copy("*", src = self.cpp_info.resdirs[1], dst = self._share_dir.joinpath("cura", "plugins"), keep_path = True)
  211. # Copy materials (flat)
  212. self.copy_deps("*.fdm_material", root_package = "fdm_materials", src = self.deps_cpp_info["fdm_materials"].resdirs[0],
  213. dst = self._share_dir.joinpath("cura", "resources", "materials"), keep_path = False)
  214. self.copy_deps("*.sig", root_package = "fdm_materials", src = self.deps_cpp_info["fdm_materials"].resdirs[0],
  215. dst = self._share_dir.joinpath("cura", "resources", "materials"), keep_path = False)
  216. # Copy resources of Uranium (keep folder structure)
  217. self.copy_deps("*", root_package = "uranium", src = self.deps_cpp_info["uranium"].resdirs[0],
  218. dst = self._share_dir.joinpath("uranium", "resources"), keep_path = True)
  219. self.copy_deps("*", root_package = "uranium", src = self.deps_cpp_info["uranium"].resdirs[1],
  220. dst = self._share_dir.joinpath("uranium", "plugins"), keep_path = True)
  221. self.copy_deps("*", root_package = "uranium", src = self.deps_cpp_info["uranium"].libdirs[0],
  222. dst = self._site_packages.joinpath("UM"),
  223. keep_path = True)
  224. self.copy_deps("*", root_package = "uranium", src = str(Path(self.deps_cpp_info["uranium"].libdirs[0], "Qt", "qml", "UM")),
  225. dst = self._site_packages.joinpath("PyQt6", "Qt6", "qml", "UM"),
  226. keep_path = True)
  227. # Copy resources of cura_binary_data
  228. self.copy_deps("*", root_package = "cura_binary_data", src = self.deps_cpp_info["cura_binary_data"].resdirs[0],
  229. dst = self._share_dir.joinpath("cura"), keep_path = True)
  230. self.copy_deps("*", root_package = "cura_binary_data", src = self.deps_cpp_info["cura_binary_data"].resdirs[1],
  231. dst = self._share_dir.joinpath("uranium"), keep_path = True)
  232. if self.settings.os == "Windows":
  233. self.copy_deps("*", root_package = "cura_binary_data", src = self.deps_cpp_info["cura_binary_data"].resdirs[2],
  234. dst = self._share_dir.joinpath("windows"), keep_path = True)
  235. for dep in ["arcus", "savitar", "pynest2d"]:
  236. self.copy_deps("*.dll", root_package = dep, src = "@bindirs", dst = self._site_packages)
  237. self.copy_deps("*.pyd", root_package = dep, src = "@libdirs", dst = self._site_packages)
  238. self.copy_deps("*.pyi", root_package = dep, src = "@libdirs", dst = self._site_packages)
  239. self.copy_deps("*.dylib", root_package = dep, src = "@libdirs", dst = self._script_dir)
  240. # Copy requirements.txt's
  241. self.copy("*.txt", src = self.cpp_info.resdirs[-1], dst = self._base_dir.joinpath("pip_requirements"))
  242. self._generate_cura_version(Path(self._site_packages, "cura"))
  243. self._generate_pyinstaller_spec(self._base_dir,
  244. os.path.join(self.cpp_info.bindirs[0], self._um_data(self.version)["runinfo"]["entrypoint"]),
  245. os.path.join(self.cpp_info.resdirs[2], self._um_data(self.version)["pyinstaller"]["icon"][str(self.settings.os)]))
  246. def package(self):
  247. self.copy("cura_app.py", src = ".", dst = self.cpp.package.bindirs[0])
  248. self.copy("*", src = "cura", dst = self.cpp.package.libdirs[0])
  249. self.copy("*", src = "resources", dst = self.cpp.package.resdirs[0])
  250. self.copy("*", src = "plugins", dst = self.cpp.package.resdirs[1])
  251. self.copy("requirement*.txt", src = ".", dst = self.cpp.package.resdirs[-1])
  252. self.copy("*", src = "packaging", dst = self.cpp.package.resdirs[2])
  253. def package_info(self):
  254. self.user_info.pip_requirements = "requirements.txt"
  255. self.user_info.pip_requirements_git = "requirements-ultimaker.txt"
  256. self.user_info.pip_requirements_build = "requirements-dev.txt"
  257. if self.in_local_cache:
  258. self.runenv_info.append_path("PYTHONPATH", str(Path(self.cpp_info.lib_paths[0]).parent))
  259. self.runenv_info.append_path("PYTHONPATH", self.cpp_info.res_paths[1]) # Add plugins to PYTHONPATH
  260. else:
  261. self.runenv_info.append_path("PYTHONPATH", self.source_folder)
  262. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.source_folder, "plugins"))
  263. def package_id(self):
  264. del self.info.settings.os
  265. del self.info.settings.compiler
  266. del self.info.settings.build_type
  267. del self.info.settings.arch
  268. # The following options shouldn't be used to determine the hash, since these are only used to set the CuraVersion.py
  269. # which will als be generated by the deploy method during the `conan install cura/5.1.0@_/_`
  270. del self.info.options.enterprise
  271. del self.info.options.staging
  272. del self.info.options.devtools
  273. del self.info.options.cloud_api_version
  274. del self.info.options.display_name
  275. del self.info.options.cura_debug_mode
  276. # TODO: Use the hash of requirements.txt and requirements-ultimaker.txt, Because changing these will actually result in a different
  277. # Cura. This is needed because the requirements.txt aren't managed by Conan and therefor not resolved in the package_id. This isn't
  278. # ideal but an acceptable solution for now.