Skip to content

Commit 8378957

Browse files
committed
Relocate the Python wrapper as entrypoint
1 parent b7cd6d0 commit 8378957

4 files changed

Lines changed: 37 additions & 62 deletions

File tree

‎python_appimage/appimage/relocate.py‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..utils.fs import make_tree, copy_file, copy_tree, remove_file, remove_tree
1010
from ..utils.log import debug, log
1111
from ..utils.system import ldd, system
12-
from ..utils.template import copy_template
12+
from ..utils.template import copy_template, load_template
1313

1414

1515
__all__ = ["patch_binary", "relocate_python"]
@@ -122,6 +122,10 @@ def relocate_python(python=None, appdir=None):
122122
target = PYTHON_BIN + '/' + PYTHON_X_Y
123123
copy_file(source, target, update=True)
124124

125+
relpath = os.path.relpath(target, APPDIR_BIN)
126+
make_tree(APPDIR_BIN)
127+
os.symlink(relpath, APPDIR_BIN + '/' + PYTHON_X_Y)
128+
125129
copy_tree(HOST_PKG, PYTHON_PKG)
126130
copy_tree(HOST_INC, PYTHON_INC)
127131

@@ -146,7 +150,6 @@ def relocate_python(python=None, appdir=None):
146150
shutil.copymode(pip_source, target)
147151

148152
relpath = os.path.relpath(target, APPDIR_BIN)
149-
make_tree(APPDIR_BIN)
150153
os.symlink(relpath, APPDIR_BIN + '/' + PIP_X_Y)
151154

152155

@@ -161,20 +164,6 @@ def relocate_python(python=None, appdir=None):
161164
remove_tree(path)
162165

163166

164-
# Wrap the Python executable
165-
log('WRAP', '%s executable', PYTHON_X_Y)
166-
167-
with open(PREFIX + '/data/python-wrapper.sh') as f:
168-
text = f.read()
169-
text = text.replace('{{PYTHON}}', PYTHON_X_Y)
170-
171-
make_tree(APPDIR_BIN)
172-
target = APPDIR_BIN + '/' + PYTHON_X_Y
173-
with open(target, 'w') as f:
174-
f.write(text)
175-
shutil.copymode(PYTHON_BIN + '/' + PYTHON_X_Y, target)
176-
177-
178167
# Set or update symlinks to python
179168
pythons = glob.glob(APPDIR_BIN + '/python?.*')
180169
versions = [os.path.basename(python)[6:] for python in pythons]
@@ -285,7 +274,8 @@ def relocate_python(python=None, appdir=None):
285274
apprun = APPDIR + '/AppRun'
286275
if not os.path.exists(apprun):
287276
log('INSTALL', 'AppRun')
288-
entrypoint = '"${{APPDIR}}/usr/bin/python{:}" "$@"'.format(VERSION)
277+
entrypoint_path = PREFIX + '/data/entrypoint.sh'
278+
entrypoint = load_template(entrypoint_path, python=PYTHON_X_Y)
289279
_copy_template('apprun.sh', apprun, entrypoint=entrypoint)
290280

291281

‎python_appimage/data/apprun.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ self="$(readlink -f -- $0)"
55
here="${self%/*}"
66
APPDIR="${APPDIR:-${here}}"
77

8+
# Export TCl/Tk
9+
export TCL_LIBRARY="${APPDIR}/usr/share/tcltk/tcl{{ tcl-version }}"
10+
export TK_LIBRARY="${APPDIR}/usr/share/tcltk/tk{{ tk-version }}"
11+
export TKPATH="${TK_LIBRARY}"
12+
813
# Call the entry point
914
{{ entrypoint }}

‎python_appimage/data/entrypoint.sh‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
for opt in "$@"
2+
do
3+
[ "${opt:0:1}" != "-" ] && break
4+
if [[ "${opt}" =~ "I" ]] || [[ "${opt}" =~ "E" ]]; then
5+
# Environment variables are disabled ($PYTHONHOME). Let's run in a safe
6+
# mode from the raw Python binary inside the AppImage
7+
"$APPDIR/opt/{{ python }}/bin/{{ python }}" "$@"
8+
exit "$?"
9+
fi
10+
done
11+
12+
# Get the executable name, i.e. the AppImage or the python binary if running from an
13+
# extracted image
14+
executable="${APPDIR}/opt/{{ python }}/{{ python }}"
15+
if [[ "${ARGV0}" =~ "/" ]]; then
16+
executable="$(cd $(dirname ${ARGV0}) && pwd)/$(basename ${ARGV0})"
17+
elif [[ "${ARGV0}" != "" ]]; then
18+
executable=$(which "${ARGV0}")
19+
fi
20+
21+
# Wrap the call to Python in order to mimic a call from the source
22+
# executable ($ARGV0), but potentially located outside of the Python
23+
# install ($PYTHONHOME)
24+
(PYTHONHOME="${APPDIR}/opt/{{ python }}" exec -a "${executable}" "$APPDIR/opt/{{ python }}/bin/{{ python }}" "$@")
25+
exit "$?"

‎python_appimage/data/python-wrapper.sh‎

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)