build_macos.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. print(f"Run create dmg command [{" ".join([str(arg) for arg in arguments])}]")
  23. subprocess.run(arguments, check=True)
  24. def build_pkg(dist_path: str, app_filename: str, component_filename: str, cura_version: str, installer_filename: str) -> None:
  25. """ Builds and signs the pkg installer.
  26. @param dist_path: Path to put output pkg in
  27. @param app_filename: name of the .app file to bundle inside the pkg
  28. @param component_filename: Name of the pkg component package to bundle the app in
  29. @param cura_version: The version is used when automatically replacing existing versions with the installer.
  30. @param installer_filename: Name of the installer that contains the component package
  31. """
  32. pkg_build_executable = os.environ.get("PKG_BUILD_EXECUTABLE", "pkgbuild")
  33. product_build_executable = os.environ.get("PRODUCT_BUILD_EXECUTABLE", "productbuild")
  34. codesign_identity = os.environ.get("CODESIGN_IDENTITY")
  35. # This builds the component package that contains UltiMaker-Cura.app. This component package will be bundled in a distribution package.
  36. pkg_build_arguments = [
  37. pkg_build_executable,
  38. "--identifier", f"{ULTIMAKER_CURA_DOMAIN}_{cura_version}", # If we want to replace previous version automatically remove {cure_version}
  39. "--component",
  40. Path(dist_path, app_filename),
  41. Path(dist_path, component_filename),
  42. "--install-location", "/Applications",
  43. ]
  44. if codesign_identity:
  45. pkg_build_arguments.extend(["--sign", codesign_identity])
  46. else:
  47. print("CODESIGN_IDENTITY missing. The installer is not being signed")
  48. print(f"Run package build command [{" ".join([str(arg) for arg in pkg_build_arguments])}]")
  49. subprocess.run(pkg_build_arguments, check=True)
  50. # This automatically generates a distribution.xml file that is used to build the installer.
  51. # If you want to make any changes to how the installer functions, this file should be changed to do that.
  52. # TODO: Use --product {property_list_file} to pull keys out of file for distribution.xml. This can be used to set min requirements
  53. distribution_creation_arguments = [
  54. product_build_executable,
  55. "--synthesize",
  56. "--package", Path(dist_path, component_filename), # Package that will be inside installer
  57. Path(dist_path, "distribution.xml"), # Output location for sythesized distributions file
  58. ]
  59. print(f"Run distribution creation command [{" ".join([str(arg) for arg in distribution_creation_arguments])}]")
  60. subprocess.run(distribution_creation_arguments, check=True)
  61. # This creates the distributable package (Installer)
  62. installer_creation_arguments = [
  63. product_build_executable,
  64. "--distribution", Path(dist_path, "distribution.xml"),
  65. "--package-path", dist_path, # Where to find the component packages mentioned in distribution.xml (UltiMaker-Cura.pkg)
  66. Path(dist_path, installer_filename),
  67. ]
  68. if codesign_identity:
  69. installer_creation_arguments.extend(["--sign", codesign_identity])
  70. print(f"Run installer creation command [{" ".join([str(arg) for arg in installer_creation_arguments])}]")
  71. subprocess.run(installer_creation_arguments, check=True)
  72. def notarize_file(dist_path: str, filename: str) -> None:
  73. """ Notarize a file. This takes 5+ minutes, there is indication that this step is successful."""
  74. notarize_user = os.environ.get("MAC_NOTARIZE_USER")
  75. notarize_password = os.environ.get("MAC_NOTARIZE_PASS")
  76. notarize_team = os.environ.get("MACOS_CERT_USER")
  77. notary_executable = os.environ.get("NOTARY_TOOL_EXECUTABLE", "notarytool")
  78. notarize_arguments = [
  79. "xcrun", notary_executable,
  80. "submit",
  81. "--apple-id", notarize_user,
  82. "--password", notarize_password,
  83. "--team-id", notarize_team,
  84. Path(dist_path, filename)
  85. ]
  86. print(f"Run notarize command [{" ".join([str(arg) for arg in notarize_arguments])}]")
  87. subprocess.run(notarize_arguments, check=True)
  88. def create_pkg_installer(filename: str, dist_path: str, cura_version: str, app_name: str) -> None:
  89. """ Creates a pkg installer from {filename}.app called {filename}-Installer.pkg
  90. The final package structure is UltiMaker-Cura-XXX-Installer.pkg[UltiMaker-Cura.pkg[UltiMaker-Cura.app]]. The outer
  91. pkg file is a distributable pkg (Installer). Inside the distributable pkg there is a component pkg. The component
  92. pkg contains the .app file that will be installed in the users Applications folder.
  93. @param filename: The name of the app file and the app component package file without the extension
  94. @param dist_path: The location to read the app from and save the pkg to
  95. """
  96. filename_stem = Path(filename).stem
  97. 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
  98. build_pkg(dist_path, app_name, cura_component_package_name, cura_version, filename)
  99. notarize = bool(os.environ.get("NOTARIZE_INSTALLER", "FALSE"))
  100. if notarize:
  101. notarize_file(dist_path, filename)
  102. def create_dmg(filename: str, dist_path: str, source_path: str, app_name: str) -> None:
  103. """ Creates a dmg executable from UltiMaker-Cura.app named {filename}.dmg
  104. @param filename: The name of the app file and the output dmg file without the extension
  105. @param dist_path: The location to read the app from and save the dmg to
  106. @param source_path: The location of the project source files
  107. """
  108. build_dmg(source_path, dist_path, filename, app_name)
  109. notarize_dmg = bool(os.environ.get("NOTARIZE_DMG", "TRUE"))
  110. if notarize_dmg:
  111. notarize_file(dist_path, filename)
  112. if __name__ == "__main__":
  113. parser = argparse.ArgumentParser(description = "Create installer for Cura.")
  114. parser.add_argument("--source_path", required = True, type = str, help = "Path to Pyinstaller source folder")
  115. parser.add_argument("--dist_path", required = True, type = str, help = "Path to Pyinstaller dist folder")
  116. parser.add_argument("--cura_conan_version", required = True, type = str, help = "The version of cura")
  117. 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')")
  118. parser.add_argument("--build_pkg", action="store_true", default = False, help = "build the pkg")
  119. parser.add_argument("--build_dmg", action="store_true", default = True, help = "build the dmg")
  120. parser.add_argument("--app_name", required = True, type = str, help = "Filename of the .app that will be contained within the dmg/pkg")
  121. args = parser.parse_args()
  122. cura_version = args.cura_conan_version.split("/")[-1]
  123. app_name = f"{args.app_name}.app"
  124. if args.build_pkg:
  125. create_pkg_installer(f"{args.filename}.pkg", args.dist_path, cura_version, app_name)
  126. if args.build_dmg:
  127. create_dmg(f"{args.filename}.dmg", args.dist_path, args.source_path, app_name)