conanfile.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 conan import ConanFile
  8. from conan.tools import files
  9. from conan.tools.env import VirtualRunEnv
  10. from conans import tools
  11. from conan.errors import ConanInvalidConfiguration, ConanException
  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", "requirements.txt", "requirements-dev.txt", "requirements-ultimaker.txt"
  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.1@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
  43. }
  44. scm = {
  45. "type": "git",
  46. "subfolder": ".",
  47. "url": "auto",
  48. "revision": "auto"
  49. }
  50. @property
  51. def _venv_path(self):
  52. if self.settings.os == "Windows":
  53. return "Scripts"
  54. return "bin"
  55. @property
  56. def _staging(self):
  57. return self.options.staging in ["True", 'true']
  58. @property
  59. def _enterprise(self):
  60. return self.options.enterprise in ["True", 'true']
  61. @property
  62. def _cloud_api_root(self):
  63. return "https://api-staging.ultimaker.com" if self._staging else "https://api.ultimaker.com"
  64. @property
  65. def _cloud_account_api_root(self):
  66. return "https://account-staging.ultimaker.com" if self._staging else "https://account.ultimaker.com"
  67. @property
  68. def _marketplace_root(self):
  69. return "https://marketplace-staging.ultimaker.com" if self._staging else "https://marketplace.ultimaker.com"
  70. @property
  71. def _digital_factory_url(self):
  72. return "https://digitalfactory-staging.ultimaker.com" if self._staging else "https://digitalfactory.ultimaker.com"
  73. @property
  74. def requirements_txts(self):
  75. if self.options.devtools:
  76. return ["requirements.txt", "requirements-ultimaker.txt", "requirements-dev.txt"]
  77. return ["requirements.txt", "requirements-ultimaker.txt"]
  78. def source(self):
  79. with open(Path(self.source_folder, "CuraVersion.py.jinja"), "r") as f:
  80. cura_version_py = Template(f.read())
  81. with open(Path(self.source_folder, "cura", "CuraVersion.py"), "w") as f:
  82. f.write(cura_version_py.render(
  83. cura_app_name = self.name,
  84. cura_app_display_name = self.options.display_name,
  85. cura_version = self.version,
  86. cura_build_type = "Enterprise" if self._enterprise else "",
  87. cura_debug_mode = self.options.cura_debug_mode,
  88. cura_cloud_api_root = self._cloud_api_root,
  89. cura_cloud_api_version = self.options.cloud_api_version,
  90. cura_cloud_account_api_root = self._cloud_account_api_root,
  91. cura_marketplace_root = self._marketplace_root,
  92. cura_digital_factory_url = self._digital_factory_url))
  93. def configure(self):
  94. self.options["arcus"].shared = True
  95. self.options["savitar"].shared = True
  96. self.options["pynest2d"].shared = True
  97. self.options["cpython"].shared = True
  98. def validate(self):
  99. if self.version and tools.Version(self.version) <= tools.Version("4"):
  100. raise ConanInvalidConfiguration("Only versions 5+ are support")
  101. def requirements(self):
  102. for req in self._um_data(self.version)["requirements"]:
  103. self.requires(req)
  104. def layout(self):
  105. self.folders.source = "."
  106. self.folders.build = "venv"
  107. self.folders.generators = os.path.join(self.folders.build, "conan")
  108. self.cpp.package.libdirs = ["site-packages"]
  109. self.cpp.package.resdirs = ["res"]
  110. def generate(self):
  111. if self.options.devtools:
  112. with open(Path(self.source_folder, "Ultimaker-Cura.spec.jinja"), "r") as f:
  113. pyinstaller = Template(f.read())
  114. pyinstaller_metadata = self._um_data(self.version)["pyinstaller"]
  115. datas = []
  116. for data in pyinstaller_metadata["datas"].values():
  117. if "package" in data: # get the paths from conan package
  118. if data["package"] == self.name:
  119. src_path = Path(self.package_folder, data["src"])
  120. else:
  121. src_path = Path(self.deps_cpp_info[data["package"]].rootpath, data["src"])
  122. elif "root" in data: # get the paths relative from the sourcefolder
  123. src_path = Path(self.source_folder, data["root"], data["src"])
  124. else:
  125. continue
  126. if src_path.exists():
  127. datas.append((str(src_path), data["dst"]))
  128. binaries = []
  129. for binary in pyinstaller_metadata["binaries"].values():
  130. if "package" in binary: # get the paths from conan package
  131. src_path = Path(self.deps_cpp_info[binary["package"]].rootpath, binary["src"])
  132. elif "root" in binary: # get the paths relative from the sourcefolder
  133. src_path = Path(self.source_folder, binary["root"], binary["src"])
  134. else:
  135. continue
  136. if not src_path.exists():
  137. continue
  138. for bin in src_path.glob(binary["binary"] + ".*[exe|dll|so|dylib]"):
  139. binaries.append((str(bin), binary["dst"]))
  140. for bin in src_path.glob(binary["binary"]):
  141. binaries.append((str(bin), binary["dst"]))
  142. with open(Path(self.generators_folder, "Ultimaker-Cura.spec"), "w") as f:
  143. f.write(pyinstaller.render(
  144. name = str(self.options.display_name).replace(" ", "-"),
  145. entrypoint = os.path.join("..", "..", self._um_data(self.version)["runinfo"]["entrypoint"]),
  146. datas = datas,
  147. binaries = binaries,
  148. hiddenimports = pyinstaller_metadata["hiddenimports"],
  149. collect_all = pyinstaller_metadata["collect_all"],
  150. icon = os.path.join("..", "..", pyinstaller_metadata["icon"][str(self.settings.os)])
  151. ))
  152. def imports(self):
  153. self.copy("CuraEngine.exe", root_package = "curaengine", src = "@bindirs", dst = "", keep_path = False)
  154. self.copy("CuraEngine", root_package = "curaengine", src = "@bindirs", dst = "", keep_path = False)
  155. files.rmdir(self, "resources/materials")
  156. self.copy("*.fdm_material", root_package = "fdm_materials", src = "@resdirs", dst = "resources/materials", keep_path = False)
  157. self.copy("*.sig", root_package = "fdm_materials", src = "@resdirs", dst = "resources/materials", keep_path = False)
  158. self.copy("*.dll", src = "@bindirs", dst = "venv/Lib/site-packages")
  159. self.copy("*.pyd", src = "@libdirs", dst = "venv/Lib/site-packages")
  160. self.copy("*.pyi", src = "@libdirs", dst = "venv/Lib/site-packages")
  161. self.copy("*.dylib", src = "@libdirs", dst = "venv/bin")
  162. def deploy(self):
  163. # Setup the Virtual Python Environment in the user space
  164. python_interpreter = Path(self.deps_user_info["cpython"].python)
  165. # When on Windows execute as Windows Path
  166. if self.settings.os == "Windows":
  167. python_interpreter = Path(*[f'"{p}"' if " " in p else p for p in python_interpreter.parts])
  168. # Create the virtual environment
  169. self.run(f"""{python_interpreter} -m venv {self.install_folder}""", run_environment = True, env = "conanrun")
  170. # Make sure there executable is named the same on all three OSes this allows it to be called with `python`
  171. # simplifying GH Actions steps
  172. if self.settings.os != "Windows":
  173. python_venv_interpreter = Path(self.install_folder, self._venv_path, "python")
  174. if not python_venv_interpreter.exists():
  175. python_venv_interpreter.hardlink_to(Path(self.install_folder, self._venv_path, Path(sys.executable).stem + Path(sys.executable).suffix))
  176. else:
  177. python_venv_interpreter = Path(self.install_folder, self._venv_path, Path(sys.executable).stem + Path(sys.executable).suffix)
  178. if not python_venv_interpreter.exists():
  179. raise ConanException(f"Virtual environment Python interpreter not found at: {python_venv_interpreter}")
  180. if self.settings.os == "Windows":
  181. python_venv_interpreter = Path(*[f'"{p}"' if " " in p else p for p in python_venv_interpreter.parts])
  182. buffer = StringIO()
  183. outer = '"' if self.settings.os == "Windows" else "'"
  184. inner = "'" if self.settings.os == "Windows" else '"'
  185. self.run(f"{python_venv_interpreter} -c {outer}import sysconfig; print(sysconfig.get_path({inner}purelib{inner})){outer}",
  186. env = "conanrun",
  187. output = buffer)
  188. pythonpath = buffer.getvalue().splitlines()[-1]
  189. run_env = VirtualRunEnv(self)
  190. env = run_env.environment()
  191. env.define_path("VIRTUAL_ENV", self.install_folder)
  192. env.prepend_path("PATH", os.path.join(self.install_folder, self._venv_path))
  193. env.prepend_path("PYTHONPATH", pythonpath)
  194. env.unset("PYTHONHOME")
  195. envvars = env.vars(self.conanfile, scope = "run")
  196. # Install some base_packages
  197. self.run(f"""{python_venv_interpreter} -m pip install wheel setuptools""", run_environment = True, env = "conanrun")
  198. # Install the requirements*.text
  199. # TODO: loop through dependencies and go over each requirement file per dependency (maybe we should add this to the conandata, or
  200. # define cpp_user_info in dependencies
  201. # Copy CuraEngine.exe to bindirs of Virtual Python Environment
  202. # TODO: Fix source such that it will get the curaengine relative from the executable (Python bindir in this case)
  203. self.copy_deps("CuraEngine.exe", root_package = "curaengine", src = "@bindirs",
  204. dst = os.path.join(self.install_folder, self._venv_path), keep_path = False)
  205. self.copy_deps("CuraEngine", root_package = "curaengine", src = "@bindirs",
  206. dst = os.path.join(self.install_folder, self._venv_path), keep_path = False)
  207. # Copy resources of Cura (keep folder structure)
  208. self.copy_deps("*", root_package = "cura", src = "@resdirs", dst = os.path.join(self.install_folder, "share", "cura", "resources"),
  209. keep_path = True)
  210. # Copy materials (flat)
  211. self.copy_deps("*.fdm_material", root_package = "fdm_materials", src = "@resdirs",
  212. dst = os.path.join(self.install_folder, "share", "cura", "resources", "materials"), keep_path = False)
  213. self.copy_deps("*.sig", root_package = "fdm_materials", src = "@resdirs",
  214. dst = os.path.join(self.install_folder, "share", "cura", "resources", "materials"), keep_path = False)
  215. # Copy resources of Uranium (keep folder structure)
  216. self.copy_deps("*", root_package = "uranium", src = "@resdirs",
  217. dst = os.path.join(self.install_folder, "share", "cura", "resources"), keep_path = True)
  218. # Copy dynamic libs to site-packages
  219. self.copy_deps("*.dll", src = "@bindirs", dst = "venv/Lib/site-packages")
  220. self.copy_deps("*.pyd", src = "@libdirs", dst = "venv/Lib/site-packages")
  221. self.copy_deps("*.pyi", src = "@libdirs", dst = "venv/Lib/site-packages")
  222. self.copy_deps("*.dylib", src = "@libdirs", dst = "venv/bin")
  223. # Make sure the CuraVersion.py is up to date with the correct settings
  224. with open(Path(Path(__file__).parent, "CuraVersion.py.jinja"), "r") as f:
  225. cura_version_py = Template(f.read())
  226. # TODO: Extend
  227. def package(self):
  228. self.copy("*", src = "cura", dst = os.path.join(self.cpp.package.libdirs[0], "cura"))
  229. self.copy("*", src = "plugins", dst = os.path.join(self.cpp.package.libdirs[0], "plugins"))
  230. self.copy("*", src = "resources", dst = os.path.join(self.cpp.package.resdirs[0], "resources"))
  231. def package_info(self):
  232. if self.in_local_cache:
  233. self.runenv_info.append_path("PYTHONPATH", self.cpp_info.libdirs[0])
  234. else:
  235. self.runenv_info.append_path("PYTHONPATH", self.source_folder)
  236. def package_id(self):
  237. del self.info.settings.os
  238. del self.info.settings.compiler
  239. del self.info.settings.build_type
  240. del self.info.settings.arch