Skip to content

Commit

Permalink
Use binary exacutable for launching Thonny in macOS app bundle.
Browse files Browse the repository at this point in the history
Allows getting rid of postinstall script, makes the bundle relocatable, prepares way for publishing dmg instead of pkg, thonny#3283, thonny#2296
  • Loading branch information
aivarannamaa committed Sep 8, 2024
1 parent 50e3581 commit 32bfac3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
Binary file modified packaging/mac/Thonny.app.initial_template/Contents/MacOS/thonny
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# inspired by IDLE.app/Contents/IDLE

import sys
import os.path

execdir = os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), "MacOS")
executable = os.path.join(execdir, "Python")
resdir = os.path.join(os.path.dirname(execdir), "Resources")
libdir = os.path.join(os.path.dirname(execdir), "Frameworks/Python.framework/Versions/3.12/lib")
ssl_cert_dir = libdir + "/python3.12/site-packages/certifi"
mainprogram = os.path.join(resdir, "thonnymain.py")

sys.argv.insert(1, mainprogram)
sys.argv.insert(1, "-I")

os.environ["PYTHON_SYS_EXECUTABLE"] = executable
os.environ["SSL_CERT_DIR"] = ssl_cert_dir
os.environ["SSL_CERT_FILE"] = ssl_cert_dir + "/cacert.pem"
os.environ["TCL_LIBRARY"] = libdir + "/tcl8.6"
os.environ["TK_LIBRARY"] = libdir + "/tk8.6"

os.execve(executable, sys.argv, os.environ)
3 changes: 3 additions & 0 deletions packaging/mac/launcher/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/zsh

swiftc -o ../Thonny.app.initial_template/Contents/MacOS/thonny launcher.swift
31 changes: 31 additions & 0 deletions packaging/mac/launcher/launcher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

// Get the main bundle path
let bundlePath = Bundle.main.bundlePath

// Path to the Python interpreter inside the bundle
let pythonPath = "\(bundlePath)/Contents/Frameworks/Python.framework/Versions/Current/Resources/Python.app/Contents/MacOS/Python"

// Path to the main Python script inside the bundle
let mainScriptPath = "\(bundlePath)/Contents/Resources/launch.py"

// Ensure Python interpreter and script exist
let fileManager = FileManager.default
if fileManager.fileExists(atPath: pythonPath) && fileManager.fileExists(atPath: mainScriptPath) {

// Prepare arguments (execv expects C-style array of arguments)
let arguments = [pythonPath, mainScriptPath]
let args = arguments.map { $0.withCString(strdup) }

// Append nil to the arguments array
let argv = args + [nil]

// Use execv to replace the current process with the Python interpreter
execv(argv[0]!, argv)

// If execv returns, it means there was an error
perror("execv failed")

} else {
print("Python interpreter or main script not found!")
}
43 changes: 0 additions & 43 deletions packaging/mac/scripts/postinstall

This file was deleted.

0 comments on commit 32bfac3

Please sign in to comment.