create_appimage.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2022 UltiMaker
  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, appimage_filename):
  11. """
  12. Creates an AppImage file from the build artefacts created so far.
  13. """
  14. copy_metadata_files(dist_path, version)
  15. try:
  16. os.remove(os.path.join(dist_path, appimage_filename)) # Ensure any old file is removed, if it exists.
  17. except FileNotFoundError:
  18. pass # If it didn't exist, that's even better.
  19. generate_appimage(dist_path, appimage_filename)
  20. sign_appimage(dist_path, appimage_filename)
  21. def copy_metadata_files(dist_path, version):
  22. """
  23. Copy metadata files for the metadata of the AppImage.
  24. """
  25. copied_files = {
  26. os.path.join("..", "icons", "cura-icon.svg"): os.path.join("usr", "share", "icons", "hicolor", "scalable", "apps", "cura-icon.svg"),
  27. os.path.join("..", "icons", "cura-icon_64x64.png"): os.path.join("usr", "share", "icons", "hicolor", "64x64", "apps", "cura-icon.png"),
  28. os.path.join("..", "icons", "cura-icon_128x128.png"): os.path.join("usr", "share", "icons", "hicolor", "128x128", "apps", "cura-icon.png"),
  29. os.path.join("..", "icons", "cura-icon_256x256.png"): os.path.join("usr", "share", "icons", "hicolor", "256x256", "apps", "cura-icon.png"),
  30. os.path.join("..", "icons", "cura-icon_256x256.png"): "cura-icon.png",
  31. "cura.appdata.xml": "cura.appdata.xml",
  32. "AppRun": "AppRun"
  33. }
  34. packaging_dir = os.path.dirname(__file__)
  35. for source, dest in copied_files.items():
  36. dest_file_path = os.path.join(dist_path, dest)
  37. os.makedirs(os.path.dirname(dest_file_path), exist_ok=True)
  38. shutil.copyfile(os.path.join(packaging_dir, source), dest_file_path)
  39. # Ensure that AppRun has the proper permissions: 755 (user reads, writes and executes, group reads and executes, world reads and executes).
  40. print("Changing permissions for AppRun")
  41. 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)
  42. # Provision the Desktop file with the correct version number.
  43. template_path = os.path.join(packaging_dir, "cura.desktop.jinja")
  44. desktop_path = os.path.join(dist_path, "cura.desktop")
  45. print("Provisioning desktop file from", template_path, "to", desktop_path)
  46. with open(template_path, "r") as f:
  47. desktop_file = Template(f.read())
  48. with open(desktop_path, "w") as f:
  49. f.write(desktop_file.render(cura_version = version))
  50. def generate_appimage(dist_path, appimage_filename):
  51. appimage_path = os.path.join(dist_path, "..", appimage_filename)
  52. appimagetool = os.getenv("APPIMAGETOOL_LOCATION", "appimagetool")
  53. command = [appimagetool, "--appimage-extract-and-run", f"{dist_path}/", appimage_path]
  54. result = subprocess.call(command)
  55. if result != 0:
  56. raise RuntimeError(f"The AppImageTool command returned non-zero: {result}")
  57. def sign_appimage(dist_path, appimage_filename):
  58. appimage_path = os.path.join(dist_path, "..", appimage_filename)
  59. command = ["gpg", "--yes", "--armor", "--detach-sig", appimage_path]
  60. result = subprocess.call(command)
  61. if result != 0:
  62. raise RuntimeError(f"The GPG command returned non-zero: {result}")
  63. if __name__ == "__main__":
  64. parser = argparse.ArgumentParser(description = "Create AppImages of Cura.")
  65. parser.add_argument("dist_path", type=str, help="Path to where PyInstaller installed the distribution of Cura.")
  66. parser.add_argument("version", type=str, help="Full version number of Cura (e.g. '5.1.0-beta')")
  67. parser.add_argument("filename", type = str, help = "Filename of the AppImage (e.g. 'UltiMaker-Cura-5.1.0-beta-Linux-X64.AppImage')")
  68. args = parser.parse_args()
  69. build_appimage(args.dist_path, args.version, args.filename)