create_appimage.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import argparse # Command line arguments parsing and help.
  4. from jinja2 import Template
  5. import os # Finding installation directory.
  6. import os.path # Finding files.
  7. import shutil # Copying files.
  8. import stat # For setting file permissions.
  9. import subprocess # For calling system commands.
  10. def build_appimage(dist_path, version):
  11. """
  12. Creates an AppImage file from the build artefacts created so far.
  13. """
  14. copy_metadata_files(dist_path, version)
  15. appimage_filename = f"Ultimaker-Cura_{version}.AppImage"
  16. try:
  17. os.remove(os.path.join(dist_path, appimage_filename)) # Ensure any old file is removed, if it exists.
  18. except FileNotFoundError:
  19. pass # If it didn't exist, that's even better.
  20. generate_appimage(dist_path, appimage_filename)
  21. sign_appimage(dist_path, appimage_filename)
  22. def copy_metadata_files(dist_path, version):
  23. """
  24. Copy metadata files for the metadata of the AppImage.
  25. """
  26. copied_files = {
  27. os.path.join("..", "icons", "cura-icon_256x256.png"): "cura-icon.png",
  28. "cura.appdata.xml": "cura.appdata.xml",
  29. "AppRun": "AppRun"
  30. }
  31. packaging_dir = os.path.dirname(__file__)
  32. for source, dest in copied_files.items():
  33. print("Copying", os.path.join(packaging_dir, source), "to", os.path.join(dist_path, dest))
  34. shutil.copyfile(os.path.join(packaging_dir, source), os.path.join(dist_path, dest))
  35. # Ensure that AppRun has the proper permissions: 755 (user reads, writes and executes, group reads and executes, world reads and executes).
  36. print("Changing permissions for AppRun")
  37. os.chmod(os.path.join(dist_path, "AppRun"), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
  38. # Provision the Desktop file with the correct version number.
  39. template_path = os.path.join(packaging_dir, "cura.desktop.jinja")
  40. desktop_path = os.path.join(dist_path, "cura.desktop")
  41. print("Provisioning desktop file from", template_path, "to", desktop_path)
  42. with open(template_path, "r") as f:
  43. desktop_file = Template(f.read())
  44. with open(desktop_path, "w") as f:
  45. f.write(desktop_file.render(cura_version = version))
  46. def generate_appimage(dist_path, appimage_filename):
  47. appimage_path = os.path.join(dist_path, "..", appimage_filename)
  48. appimagetool = os.getenv("APPIMAGETOOL_LOCATION", "appimagetool")
  49. command = [appimagetool, "--appimage-extract-and-run", f"{dist_path}/", appimage_path]
  50. result = subprocess.call(command)
  51. if result != 0:
  52. raise RuntimeError(f"The AppImageTool command returned non-zero: {result}")
  53. def sign_appimage(dist_path, appimage_filename):
  54. appimage_path = os.path.join(dist_path, "..", appimage_filename)
  55. command = ["gpg", "--yes", "--armor", "--detach-sig", appimage_path]
  56. result = subprocess.call(command)
  57. if result != 0:
  58. raise RuntimeError(f"The GPG command returned non-zero: {result}")
  59. if __name__ == "__main__":
  60. parser = argparse.ArgumentParser(description = "Create AppImages of Cura.")
  61. parser.add_argument("dist_path", type=str, help="Path to where PyInstaller installed the distribution of Cura.")
  62. parser.add_argument("version", type=str, help="Full version number of Cura (e.g. '5.1.0-beta')")
  63. args = parser.parse_args()
  64. build_appimage(args.dist_path, args.version)