build_macos.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (c) 2023 UltiMaker
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. import argparse # Command line arguments parsing and help.
  5. import subprocess
  6. from pathlib import Path
  7. ULTIMAKER_CURA_DOMAIN = os.environ.get("ULTIMAKER_CURA_DOMAIN", "nl.ultimaker.cura")
  8. def build_dmg(source_path: str, dist_path: str, filename: str, app_name: str) -> None:
  9. create_dmg_executable = os.environ.get("CREATE_DMG_EXECUTABLE", "create-dmg")
  10. arguments = [create_dmg_executable,
  11. "--window-pos", "640", "360",
  12. "--window-size", "690", "503",
  13. "--app-drop-link", "520", "272",
  14. "--volicon", f"{source_path}/packaging/icons/VolumeIcons_Cura.icns",
  15. "--icon-size", "90",
  16. "--icon", app_name, "169", "272",
  17. "--eula", f"{source_path}/packaging/cura_license.txt",
  18. "--background", f"{source_path}/packaging/MacOs/cura_background_dmg.png",
  19. "--hdiutil-quiet",
  20. f"{dist_path}/{filename}",
  21. f"{dist_path}/{app_name}"]
  22. subprocess.run(arguments)
  23. def build_pkg(dist_path: str, app_filename: str, component_filename: str, cura_version: str, installer_filename: str) -> None:
  24. """ Builds and signs the pkg installer.
  25. @param dist_path: Path to put output pkg in
  26. @param app_filename: name of the .app file to bundle inside the pkg
  27. @param component_filename: Name of the pkg component package to bundle the app in
  28. @param cura_version: The version is used when automatically replacing existing versions with the installer.
  29. @param installer_filename: Name of the installer that contains the component package
  30. """
  31. pkg_build_executable = os.environ.get("PKG_BUILD_EXECUTABLE", "pkgbuild")
  32. product_build_executable = os.environ.get("PRODUCT_BUILD_EXECUTABLE", "productbuild")
  33. codesign_identity = os.environ.get("CODESIGN_IDENTITY")
  34. # This builds the component package that contains UltiMaker-Cura.app. This component package will be bundled in a distribution package.
  35. pkg_build_arguments = [
  36. pkg_build_executable,
  37. "--identifier", f"{ULTIMAKER_CURA_DOMAIN}_{cura_version}", # If we want to replace previous version automatically remove {cure_version}
  38. "--component",
  39. Path(dist_path, app_filename),
  40. Path(dist_path, component_filename),
  41. "--install-location", "/Applications",
  42. ]
  43. if codesign_identity:
  44. pkg_build_arguments.extend(["--sign", codesign_identity])
  45. else:
  46. print("CODESIGN_IDENTITY missing. The installer is not being signed")
  47. subprocess.run(pkg_build_arguments)
  48. # This automatically generates a distribution.xml file that is used to build the installer.
  49. # If you want to make any changes to how the installer functions, this file should be changed to do that.
  50. # TODO: Use --product {property_list_file} to pull keys out of file for distribution.xml. This can be used to set min requirements
  51. distribution_creation_arguments = [
  52. product_build_executable,
  53. "--synthesize",
  54. "--package", Path(dist_path, component_filename), # Package that will be inside installer
  55. Path(dist_path, "distribution.xml"), # Output location for sythesized distributions file
  56. ]
  57. subprocess.run(distribution_creation_arguments)
  58. # This creates the distributable package (Installer)
  59. installer_creation_arguments = [
  60. product_build_executable,
  61. "--distribution", Path(dist_path, "distribution.xml"),
  62. "--package-path", dist_path, # Where to find the component packages mentioned in distribution.xml (UltiMaker-Cura.pkg)
  63. Path(dist_path, installer_filename),
  64. ]
  65. if codesign_identity:
  66. installer_creation_arguments.extend(["--sign", codesign_identity])
  67. subprocess.run(installer_creation_arguments)
  68. def notarize_file(dist_path: str, filename: str) -> None:
  69. """ Notarize a file. This takes 5+ minutes, there is indication that this step is successful."""
  70. notarize_user = os.environ.get("MAC_NOTARIZE_USER")
  71. notarize_password = os.environ.get("MAC_NOTARIZE_PASS")
  72. altool_executable = os.environ.get("ALTOOL_EXECUTABLE", "altool")
  73. notarize_arguments = [
  74. "xcrun", altool_executable,
  75. "--notarize-app",
  76. "--primary-bundle-id", ULTIMAKER_CURA_DOMAIN,
  77. "--username", notarize_user,
  78. "--password", notarize_password,
  79. "--file", Path(dist_path, filename)
  80. ]
  81. subprocess.run(notarize_arguments)
  82. def create_pkg_installer(filename: str, dist_path: str, cura_version: str, app_name: str) -> None:
  83. """ Creates a pkg installer from {filename}.app called {filename}-Installer.pkg
  84. The final package structure is UltiMaker-Cura-XXX-Installer.pkg[UltiMaker-Cura.pkg[UltiMaker-Cura.app]]. The outer
  85. pkg file is a distributable pkg (Installer). Inside the distributable pkg there is a component pkg. The component
  86. pkg contains the .app file that will be installed in the users Applications folder.
  87. @param filename: The name of the app file and the app component package file without the extension
  88. @param dist_path: The location to read the app from and save the pkg to
  89. """
  90. filename_stem = Path(filename).stem
  91. cura_component_package_name = f"{filename_stem}-Component.pkg" # This is a component package that is nested inside the installer, it contains the UltiMaker-Cura.app file This is the app file that will end up in your applications folder
  92. build_pkg(dist_path, app_name, cura_component_package_name, cura_version, filename)
  93. notarize = bool(os.environ.get("NOTARIZE_INSTALLER", "FALSE"))
  94. if notarize:
  95. notarize_file(dist_path, filename)
  96. def create_dmg(filename: str, dist_path: str, source_path: str, app_name: str) -> None:
  97. """ Creates a dmg executable from UltiMaker-Cura.app named {filename}.dmg
  98. @param filename: The name of the app file and the output dmg file without the extension
  99. @param dist_path: The location to read the app from and save the dmg to
  100. @param source_path: The location of the project source files
  101. """
  102. build_dmg(source_path, dist_path, filename, app_name)
  103. notarize_dmg = bool(os.environ.get("NOTARIZE_DMG", "TRUE"))
  104. if notarize_dmg:
  105. notarize_file(dist_path, filename)
  106. if __name__ == "__main__":
  107. parser = argparse.ArgumentParser(description = "Create installer for Cura.")
  108. parser.add_argument("--source_path", required = True, type = str, help = "Path to Pyinstaller source folder")
  109. parser.add_argument("--dist_path", required = True, type = str, help = "Path to Pyinstaller dist folder")
  110. parser.add_argument("--cura_conan_version", required = True, type = str, help = "The version of cura")
  111. parser.add_argument("--filename", required = True, type = str, help = "Filename of the pkg/dmg (e.g. 'UltiMaker-Cura-5.5.0-Macos-X64' or 'UltiMaker-Cura-5.5.0-beta.1-Macos-ARM64')")
  112. parser.add_argument("--build_pkg", action="store_true", default = False, help = "build the pkg")
  113. parser.add_argument("--build_dmg", action="store_true", default = True, help = "build the dmg")
  114. parser.add_argument("--app_name", required = True, type = str, help = "Filename of the .app that will be contained within the dmg/pkg")
  115. args = parser.parse_args()
  116. cura_version = args.cura_conan_version.split("/")[-1]
  117. app_name = f"{args.app_name}.app"
  118. if args.build_pkg:
  119. create_pkg_installer(f"{args.filename}.pkg", args.dist_path, cura_version, app_name)
  120. if args.build_dmg:
  121. create_dmg(f"{args.filename}.dmg", args.dist_path, args.source_path, app_name)