-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlinuxdeploy-plugin-python.sh
executable file
·290 lines (250 loc) · 8.8 KB
/
linuxdeploy-plugin-python.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
if [[ -z "$DEBUG" ]] || [[ "$DEBUG" -eq "0" ]]; then
set -e
else
set -ex
fi
# Configuration variables
NPROC="${NPROC:-$(nproc)}"
PIP_OPTIONS="${PIP_OPTIONS:---upgrade}"
PIP_REQUIREMENTS="${PIP_REQUIREMENTS:-}"
PYTHON_BUILD_DIR="${PYTHON_BUILD_DIR:-}"
PYTHON_CONFIG="${PYTHON_CONFIG:-}"
version="3.8.2"
PYTHON_SOURCE="${PYTHON_SOURCE:-https://www.python.org/ftp/python/${version}/Python-${version}.tgz}"
script=$(readlink -f $0)
exe_name="$(basename ${APPIMAGE:-$script})"
BASEDIR="${APPDIR:-$(readlink -m $(dirname $script))}"
prefix="usr/python"
# Parse the CLI
show_usage () {
echo "Usage: ${exe_name} --appdir <path to AppDir>"
echo
echo "Bundle Python into an AppDir under \${APPDIR}/${prefix}. In addition"
echo "extra packages can be bundled as well using \$PIP_REQUIREMENTS."
echo "Note that if an existing install of Python already exists then no new"
echo "install of Python is done, i.e. \${PYTHON_SOURCE} and its related"
echo "arguments are ignored. Yet, extra \$PIP_REQUIREMENTS are installed."
echo
echo "Variables:"
echo " NPROC=\"${NPROC}\""
echo " The number of processors to use for building Python from a"
echo " source distribution"
echo
echo " PIP_OPTIONS=\"${PIP_OPTIONS}\""
echo " Options for pip when bundling extra site-packages"
echo
echo " PIP_REQUIREMENTS=\"${PIP_REQUIREMENTS}\""
echo " Specify extra site-packages to embed in the AppImage. Those are"
echo " installed with pip as requirements"
echo
echo " PYTHON_BUILD_DIR=\"\""
echo " Set the build directory for Python. A temporary one will be"
echo " created otherwise"
echo
echo " PYTHON_CONFIG=\"${PYTHON_CONFIG}\""
echo " Provide extra configuration flags for the Python build. Note"
echo " that the install prefix will be overwritten"
echo
echo " PYTHON_SOURCE=\"${PYTHON_SOURCE}\""
echo " The source to use for Python. Can be a directory, an url or/and"
echo " an archive"
echo
}
APPDIR=
while [[ ! -z "$1" ]]; do
case "$1" in
--plugin-api-version)
echo "0"
exit 0
;;
--appdir)
APPDIR="$2"
shift
shift
;;
--help)
show_usage
exit 0
;;
*)
echo "Invalid argument: $1"
echo
show_usage
exit 1
;;
esac
done
if [[ -z "$APPDIR" ]]; then
show_usage
exit 1
else
APPDIR=$(readlink -m "$APPDIR")
mkdir -p "$APPDIR"
fi
# Setup a temporary work space
if [[ -z "${PYTHON_BUILD_DIR}" ]]; then
PYTHON_BUILD_DIR=$(mktemp -d)
atexit() {
rm -rf "${PYTHON_BUILD_DIR}"
}
trap atexit EXIT
else
PYTHON_BUILD_DIR=$(readlink -m "${PYTHON_BUILD_DIR}")
mkdir -p "${PYTHON_BUILD_DIR}"
fi
# Install Python from source, if not already in the AppDir
set +e
python=$(ls "${APPDIR}/${prefix}/bin/python"?"."?)
set -e
if [[ -x "${python}" ]]; then
echo "Found existing install under ${APPDIR}/${prefix}. Skipping Python build."
else
# Check if the given sources are a local file or directory; if yes,
# "save" the full path. It might've been given relative to the current
# directory.
if [[ -e "${PYTHON_SOURCE}" ]]; then
PYTHON_SOURCE=$(readlink -f "${PYTHON_SOURCE}")
fi
cd "${PYTHON_BUILD_DIR}"
source_file=$(basename "${PYTHON_SOURCE}")
if [[ "${PYTHON_SOURCE}" == http* ]] || [[ "${PYTHON_SOURCE}" == ftp* ]]; then
wget -c --no-check-certificate "${PYTHON_SOURCE}"
else
cp -r "${PYTHON_SOURCE}" "."
fi
if [[ "${source_file}" == *.tgz ]] || [[ "${source_file}" == *.tar.gz ]]; then
if [[ "${source_file}" == *.tgz ]]; then
dirname="${source_file%.*}"
else
dirname="${source_file%.*.*}"
fi
[[ -f $dirname ]] || tar -xzf "${source_file}"
source_dir="$dirname"
else
source_dir="$source_file"
fi
cd "${source_dir}"
./configure ${PYTHON_CONFIG} "--with-ensurepip=install" "--prefix=/${prefix}" LDFLAGS="${LDFLAGS} -Wl,-rpath='"'$$ORIGIN'"/../../lib'"
HOME="${PYTHON_BUILD_DIR}" make -j"$NPROC" DESTDIR="$APPDIR" install
fi
cd "${APPDIR}/${prefix}/bin"
PYTHON_X_Y=$(ls "python"?"."?)
# Install any extra requirements with pip
if [[ ! -z "${PIP_REQUIREMENTS}" ]]; then
HOME="${PYTHON_BUILD_DIR}" PYTHONHOME=$(readlink -f ${PWD}/..) ./${PYTHON_X_Y} -m pip install ${PIP_OPTIONS} --upgrade pip
HOME="${PYTHON_BUILD_DIR}" PYTHONHOME=$(readlink -f ${PWD}/..) ./${PYTHON_X_Y} -m pip install ${PIP_OPTIONS} ${PIP_REQUIREMENTS}
fi
# Prune the install
cd "$APPDIR/${prefix}"
rm -rf "bin/python"*"-config" "bin/idle"* "lib/pkgconfig" \
"share/doc" "share/man" "lib/libpython"*".a" "lib/python"*"/test" \
"lib/python"*"/config-"*"-x86_64-linux-gnu"
# Wrap the Python executables
cd "$APPDIR/${prefix}/bin"
set +e
pythons=$(ls "python" "python"? "python"?"."? "python"?"."?"m" 2>/dev/null)
set -e
mkdir -p "$APPDIR/usr/bin"
cd "$APPDIR/usr/bin"
for python in $pythons
do
if [[ ! -L "$python" ]]; then
strip "$APPDIR/${prefix}/bin/${python}"
cp "${BASEDIR}/share/python-wrapper.sh" "$python"
sed -i "s|[{][{]PYTHON[}][}]|$python|g" "$python"
sed -i "s|[{][{]PREFIX[}][}]|$prefix|g" "$python"
fi
done
# Sanitize the shebangs of local Python scripts
cd "$APPDIR/${prefix}/bin"
for exe in $(ls "${APPDIR}/${prefix}/bin"*)
do
if [[ -x "$exe" ]] && [[ ! -d "$exe" ]]; then
sed -i '1s|^#!.*\(python[0-9.]*\).*|#!/bin/sh\n"exec" "$(dirname $(readlink -f $\{0\}))/../../bin/\1" "$0" "$@"|' "$exe"
fi
done
# Set a hook in Python for cleaning the path detection
cp "$BASEDIR/share/sitecustomize.py" "$APPDIR"/${prefix}/lib/python*/site-packages
# Patch binaries and install dependencies
excludelist="${BASEDIR}/share/excludelist"
if [[ ! -f "${excludelist}" ]]; then
pushd "${BASEDIR}"
wget -cq --no-check-certificate "https://raw.githubusercontent.com/probonopd/AppImages/master/excludelist"
excludelist="$(pwd)/excludelist"
popd
fi
excludelist=$(cat "${excludelist}" | sed 's|#.*||g' | sed -r '/^\s*$/d')
is_excluded () {
local e
for e in ${excludelist}; do
[[ "$e" == "$1" ]] && echo "true" && return 0
done
return 0
}
set +e
patchelf=$(command -v patchelf)
set -e
patchelf="${patchelf:-${BASEDIR}/usr/bin/patchelf}"
if [[ ! -x "${patchelf}" ]]; then
ARCH="${ARCH:-x86_64}"
pushd "${BASEDIR}"
wget -cq https://github.com/niess/patchelf.appimage/releases/download/${ARCH}/patchelf-${ARCH}.AppImage
patchelf="$(pwd)/patchelf-${ARCH}.AppImage"
chmod u+x "${patchelf}"
popd
fi
patch_binary() {
local name="$(basename $1)"
if [[ "${name::3}" == "lib" ]]; then
if [[ ! -f "${APPDIR}/usr/lib/${name}" ]] && [[ ! -L "${APPDIR}/usr/lib/${name}" ]]; then
echo "Patching dependency ${name}"
"${patchelf}" --set-rpath '$ORIGIN' "$1"
ln -s "$2"/"$1" "${APPDIR}/usr/lib/${name}"
fi
else
echo "Patching C-extension module ${name}"
local rpath="$(${patchelf} --print-rpath $1)"
local rel="$(dirname $(readlink -f $1))"
rel=${rel#${APPDIR}/usr}
rel=$(echo $rel | sed 's|/[_a-zA-Z0-9.-]*|/..|g')
if grep -qv '$ORIGIN'"${rel}/lib" <<< "${rpath}" ; then
[[ ! -z "${rpath}" ]] && rpath="${rpath}:"
"${patchelf}" --set-rpath "${rpath}"'$ORIGIN'"${rel}/lib" "$1"
fi
fi
local deps
for deps in $(ldd $1); do
if [[ "${deps::1}" == "/" ]] && [[ "${deps}" != "${APPDIR}"* ]]; then
local lib="$(basename ${deps})"
if [[ ! -f "${APPDIR}/usr/lib/${lib}" ]]; then
if [[ ! "$(is_excluded ${lib})" ]]; then
echo "Installing dependency ${lib}"
cp "${deps}" "${APPDIR}/usr/lib"
"${patchelf}" --set-rpath '$ORIGIN' "${APPDIR}/usr/lib/${lib}"
fi
fi
fi
done
return 0
}
cd "$APPDIR/${prefix}/bin"
[[ -f python3 ]] && ln -fs python3 python
mkdir -p "${APPDIR}/usr/lib"
cd "${APPDIR}/${prefix}/lib/${PYTHON_X_Y}"
relpath="../../${prefix}/lib/${PYTHON_X_Y}"
find "lib-dynload" -name '*.so' -type f | while read file; do patch_binary "${file}" "${relpath}"; done
# Copy any TCl/Tk shared data
if [[ ! -d "${APPDIR}/${prefix}/share/tcltk" ]]; then
if [[ -d "/usr/share/tcltk" ]]; then
mkdir -p "${APPDIR}/${prefix}/share"
cp -r "/usr/share/tcltk" "${APPDIR}/${prefix}/share"
else
mkdir -p "${APPDIR}/${prefix}/share/tcltk"
tclpath="$(ls -d /usr/share/tcl* | tail -1)"
tkpath="$(ls -d /usr/share/tk* | tail -1)"
for path in "${tclpath}" "${tkpath}"; do
cp -r "${path}" "${APPDIR}/${prefix}/share/tcltk"
done
fi
fi