Ultimaker-Cura.spec.jinja 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- mode: python ; coding: utf-8 -*-
  2. import os
  3. from pathlib import Path
  4. from PyInstaller.utils.hooks import collect_all
  5. datas = {{ datas }}
  6. binaries = {{ binaries }}
  7. hiddenimports = {{ hiddenimports }}
  8. {% for value in collect_all %}tmp_ret = collect_all('{{ value }}')
  9. datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
  10. {% endfor %}
  11. # Add dynamic libs in the venv bin/Script Path. This is needed because we might copy some additional libs
  12. # e.q.: OpenSSL 1.1.1l in that directory with a separate:
  13. # `conan install openssl@1.1.1l -g deploy && cp openssl/bin/*.so cura_inst/bin`
  14. binaries.extend([(str(bin), ".") for bin in Path("{{ venv_script_path }}").glob("*.so*")])
  15. binaries.extend([(str(bin), ".") for bin in Path("{{ venv_script_path }}").glob("*.dll")])
  16. binaries.extend([(str(bin), ".") for bin in Path("{{ venv_script_path }}").glob("*.dylib")])
  17. block_cipher = None
  18. a = Analysis(
  19. [{{ entrypoint }}],
  20. pathex=[],
  21. binaries=binaries,
  22. datas=datas,
  23. hiddenimports=hiddenimports,
  24. hookspath=[],
  25. hooksconfig={},
  26. runtime_hooks=[],
  27. excludes=[],
  28. win_no_prefer_redirects=False,
  29. win_private_assemblies=False,
  30. cipher=block_cipher,
  31. noarchive=False
  32. )
  33. pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
  34. exe = EXE(
  35. pyz,
  36. a.scripts,
  37. [],
  38. exclude_binaries=True,
  39. name=r'{{ name }}',
  40. debug=False,
  41. bootloader_ignore_signals=False,
  42. strip={{ strip }},
  43. upx={{ upx }},
  44. console=False,
  45. disable_windowed_traceback=False,
  46. argv_emulation=False,
  47. target_arch={{ target_arch }},
  48. codesign_identity=os.getenv('CODESIGN_IDENTITY', None),
  49. entitlements_file={{ entitlements_file }},
  50. icon={{ icon }}
  51. )
  52. coll = COLLECT(
  53. exe,
  54. a.binaries,
  55. a.zipfiles,
  56. a.datas,
  57. strip=False,
  58. upx=True,
  59. upx_exclude=[],
  60. name=r'{{ name }}'
  61. )
  62. {% if macos == true %}
  63. # PyInstaller seems to copy everything in the resource folder for the MacOS, this causes issues with codesigning and notarizing
  64. # The folder structure should adhere to the one specified in Table 2-5
  65. # https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1
  66. # The class below is basically ducktyping the BUNDLE class of PyInstaller and using our own `assemble` method for more fine-grain and specific
  67. # control. Some code of the method below is copied from:
  68. # https://github.com/pyinstaller/pyinstaller/blob/22d1d2a5378228744cc95f14904dae1664df32c4/PyInstaller/building/osx.py#L115
  69. #-----------------------------------------------------------------------------
  70. # Copyright (c) 2005-2022, PyInstaller Development Team.
  71. #
  72. # Distributed under the terms of the GNU General Public License (version 2
  73. # or later) with exception for distributing the bootloader.
  74. #
  75. # The full license is in the file COPYING.txt, distributed with this software.
  76. #
  77. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  78. #-----------------------------------------------------------------------------
  79. import plistlib
  80. import shutil
  81. import PyInstaller.utils.osx as osxutils
  82. from pathlib import Path
  83. from PyInstaller.building.osx import BUNDLE
  84. from PyInstaller.building.utils import (_check_path_overlap, _rmtree, add_suffix_to_extension, checkCache)
  85. from PyInstaller.building.datastruct import logger
  86. from PyInstaller.building.icon import normalize_icon_type
  87. class UMBUNDLE(BUNDLE):
  88. def assemble(self):
  89. from PyInstaller.config import CONF
  90. if _check_path_overlap(self.name) and os.path.isdir(self.name):
  91. _rmtree(self.name)
  92. logger.info("Building BUNDLE %s", self.tocbasename)
  93. # Create a minimal Mac bundle structure.
  94. macos_path = Path(self.name, "Contents", "MacOS")
  95. resources_path = Path(self.name, "Contents", "Resources")
  96. frameworks_path = Path(self.name, "Contents", "Frameworks")
  97. os.makedirs(macos_path)
  98. os.makedirs(resources_path)
  99. os.makedirs(frameworks_path)
  100. # Makes sure the icon exists and attempts to convert to the proper format if applicable
  101. self.icon = normalize_icon_type(self.icon, ("icns",), "icns", CONF["workpath"])
  102. # Ensure icon path is absolute
  103. self.icon = os.path.abspath(self.icon)
  104. # Copy icns icon to Resources directory.
  105. shutil.copy(self.icon, os.path.join(self.name, 'Contents', 'Resources'))
  106. # Key/values for a minimal Info.plist file
  107. info_plist_dict = {
  108. "CFBundleDisplayName": self.appname,
  109. "CFBundleName": self.appname,
  110. # Required by 'codesign' utility.
  111. # The value for CFBundleIdentifier is used as the default unique name of your program for Code Signing
  112. # purposes. It even identifies the APP for access to restricted OS X areas like Keychain.
  113. #
  114. # The identifier used for signing must be globally unique. The usual form for this identifier is a
  115. # hierarchical name in reverse DNS notation, starting with the toplevel domain, followed by the company
  116. # name, followed by the department within the company, and ending with the product name. Usually in the
  117. # form: com.mycompany.department.appname
  118. # CLI option --osx-bundle-identifier sets this value.
  119. "CFBundleIdentifier": self.bundle_identifier,
  120. "CFBundleExecutable": os.path.basename(self.exename),
  121. "CFBundleIconFile": os.path.basename(self.icon),
  122. "CFBundleInfoDictionaryVersion": "6.0",
  123. "CFBundlePackageType": "APPL",
  124. "CFBundleShortVersionString": self.version,
  125. }
  126. # Set some default values. But they still can be overwritten by the user.
  127. if self.console:
  128. # Setting EXE console=True implies LSBackgroundOnly=True.
  129. info_plist_dict['LSBackgroundOnly'] = True
  130. else:
  131. # Let's use high resolution by default.
  132. info_plist_dict['NSHighResolutionCapable'] = True
  133. # Merge info_plist settings from spec file
  134. if isinstance(self.info_plist, dict) and self.info_plist:
  135. info_plist_dict.update(self.info_plist)
  136. plist_filename = os.path.join(self.name, "Contents", "Info.plist")
  137. with open(plist_filename, "wb") as plist_fh:
  138. plistlib.dump(info_plist_dict, plist_fh)
  139. links = []
  140. _QT_BASE_PATH = {'PySide2', 'PySide6', 'PyQt5', 'PyQt6', 'PySide6'}
  141. for inm, fnm, typ in self.toc:
  142. # Adjust name for extensions, if applicable
  143. inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
  144. inm = Path(inm)
  145. fnm = Path(fnm)
  146. # Copy files from cache. This ensures that are used files with relative paths to dynamic library
  147. # dependencies (@executable_path)
  148. if typ in ('EXTENSION', 'BINARY') or (typ == 'DATA' and inm.suffix == '.so'):
  149. if any(['.' in p for p in inm.parent.parts]):
  150. inm = Path(inm.name)
  151. fnm = Path(checkCache(
  152. str(fnm),
  153. strip = self.strip,
  154. upx = self.upx,
  155. upx_exclude = self.upx_exclude,
  156. dist_nm = str(inm),
  157. target_arch = self.target_arch,
  158. codesign_identity = self.codesign_identity,
  159. entitlements_file = self.entitlements_file,
  160. strict_arch_validation = (typ == 'EXTENSION'),
  161. ))
  162. frame_dst = frameworks_path.joinpath(inm)
  163. if not frame_dst.exists():
  164. if frame_dst.is_dir():
  165. os.makedirs(frame_dst, exist_ok = True)
  166. else:
  167. os.makedirs(frame_dst.parent, exist_ok = True)
  168. shutil.copy(fnm, frame_dst, follow_symlinks = True)
  169. macos_dst = macos_path.joinpath(inm)
  170. if not macos_dst.exists():
  171. if macos_dst.is_dir():
  172. os.makedirs(macos_dst, exist_ok = True)
  173. else:
  174. os.makedirs(macos_dst.parent, exist_ok = True)
  175. # Create relative symlink to the framework
  176. symlink_to = Path(*[".." for p in macos_dst.relative_to(macos_path).parts], "Frameworks").joinpath(
  177. frame_dst.relative_to(frameworks_path))
  178. try:
  179. macos_dst.symlink_to(symlink_to)
  180. except FileExistsError:
  181. pass
  182. else:
  183. if typ == 'DATA':
  184. if any(['.' in p for p in inm.parent.parts]) or inm.suffix == '.so':
  185. # Skip info dist egg and some not needed folders in tcl and tk, since they all contain dots in their files
  186. logger.warning(f"Skipping DATA file {inm}")
  187. continue
  188. res_dst = resources_path.joinpath(inm)
  189. if not res_dst.exists():
  190. if res_dst.is_dir():
  191. os.makedirs(res_dst, exist_ok = True)
  192. else:
  193. os.makedirs(res_dst.parent, exist_ok = True)
  194. shutil.copy(fnm, res_dst, follow_symlinks = True)
  195. macos_dst = macos_path.joinpath(inm)
  196. if not macos_dst.exists():
  197. if macos_dst.is_dir():
  198. os.makedirs(macos_dst, exist_ok = True)
  199. else:
  200. os.makedirs(macos_dst.parent, exist_ok = True)
  201. # Create relative symlink to the resource
  202. symlink_to = Path(*[".." for p in macos_dst.relative_to(macos_path).parts], "Resources").joinpath(
  203. res_dst.relative_to(resources_path))
  204. try:
  205. macos_dst.symlink_to(symlink_to)
  206. except FileExistsError:
  207. pass
  208. else:
  209. macos_dst = macos_path.joinpath(inm)
  210. if not macos_dst.exists():
  211. if macos_dst.is_dir():
  212. os.makedirs(macos_dst, exist_ok = True)
  213. else:
  214. os.makedirs(macos_dst.parent, exist_ok = True)
  215. shutil.copy(fnm, macos_dst, follow_symlinks = True)
  216. # Sign the bundle
  217. logger.info('Signing the BUNDLE...')
  218. try:
  219. osxutils.sign_binary(self.name, self.codesign_identity, self.entitlements_file, deep = True)
  220. except Exception as e:
  221. logger.warning(f"Error while signing the bundle: {e}")
  222. logger.warning("You will need to sign the bundle manually!")
  223. logger.info(f"Building BUNDLE {self.tocbasename} completed successfully.")
  224. app = UMBUNDLE(
  225. coll,
  226. name='{{ name }}.app',
  227. icon={{ icon }},
  228. bundle_identifier={{ osx_bundle_identifier }},
  229. version={{ version }},
  230. info_plist={
  231. 'CFBundleDisplayName': '{{ display_name }}',
  232. 'NSPrincipalClass': 'NSApplication',
  233. 'CFBundleDevelopmentRegion': 'English',
  234. 'CFBundleExecutable': '{{ name }}',
  235. 'CFBundleInfoDictionaryVersion': '6.0',
  236. 'CFBundlePackageType': 'APPL',
  237. 'CFBundleShortVersionString': {{ short_version }},
  238. 'CFBundleDocumentTypes': [{
  239. 'CFBundleTypeRole': 'Viewer',
  240. 'CFBundleTypeExtensions': ['*'],
  241. 'CFBundleTypeName': 'Model Files',
  242. }]
  243. },
  244. ){% endif %}