tridactyl/scripts/wine-pyinstaller.sh

168 lines
3.9 KiB
Bash
Raw Normal View History

2018-08-31 15:52:34 +01:00
#!/usr/bin/env bash
set -e
2018-05-28 11:30:06 +01:00
# This script must be run from the root Tridactyl directory
2018-05-28 13:12:07 +01:00
TRIDIR="$(pwd)"
2018-05-28 11:30:06 +01:00
2018-05-28 13:12:07 +01:00
BUILDROOT="${TRIDIR}/.wine-pyinstaller"
OUTDIR="${BUILDROOT}/dist"
DLDIR="${BUILDROOT}/downloads"
PYVER="3.5.4"
2018-05-28 13:12:07 +01:00
PYDIR="${BUILDROOT}/python-${PYVER}"
WINPY_HASH="b5d90c5252a624117ccec8678862d6144710219737f06cd01deb1df963f639fd"
WINPY_EXE="${DLDIR}/winpython-${PYVER}.exe"
2018-05-28 13:12:07 +01:00
WINEDIR="${BUILDROOT}/wine"
WINEARCH="win32"
2018-05-28 13:12:07 +01:00
export WINEDEBUG="fixme-all"
2018-05-28 13:12:07 +01:00
# stop wine whining
export DISPLAY=
PREREQUISITES="tput printf 7z wine"
MIN_WINE_VER="4"
MIN_7ZIP_VER="16"
checkRequiredVersions() {
2019-03-24 17:29:42 +01:00
if ! 7z | awk '/Version/{print $3}' | grep -q "${MIN_7ZIP_VER}"; then
colorEcho \
2019-03-24 17:29:42 +01:00
'[-] p7zip minimum version '"${MIN_7ZIP_VER}"' required\n' \
"alert"
2019-05-29 21:09:58 +01:00
exit 1
fi
2019-03-24 17:29:42 +01:00
if ! wine --version 2> /dev/null | grep -q "wine-${MIN_WINE_VER}"; then
colorecho \
2019-03-24 17:29:42 +01:00
'[-] wine minimum version '"${MIN_WINE_VER}"' required\n' \
"alert"
2019-05-29 21:09:58 +01:00
exit 1
fi
}
stripWhitespace() {
2019-03-24 17:29:42 +01:00
local input="$*"
printf '%s\n' "${input}" | tr -d "[:space:]"
}
colorEcho() {
2019-03-24 17:29:42 +01:00
local COLOR_RESET;
COLOR_RESET="$(tput sgr0 2>/dev/null)"
local COLOR_BOLD;
COLOR_BOLD="$(tput bold 2>/dev/null)"
local COLOR_BAD;
COLOR_BAD="$(tput setaf 1 2>/dev/null)"
local COLOR_GOOD;
COLOR_GOOD="$(tput setaf 2 2>/dev/null)"
2018-05-28 13:12:07 +01:00
local str="$1"
local color="${COLOR_GOOD}${COLOR_BOLD}"
2019-05-29 21:09:58 +01:00
if [ -n "$2" ] \
&& [ "$(stripWhitespace "$2")" = "alert" ]; then
color="${COLOR_BAD}${COLOR_BOLD}"
fi
2019-03-24 17:29:42 +01:00
printf '%s' "${color}${str}${COLOR_RESET}"
}
checkPrerequisite() {
local bin_name="$1"
if command -v "${bin_name}" 1>/dev/null 2>/dev/null; then
printf '%s\n' " - '${bin_name}' found."
else
2019-03-24 17:29:42 +01:00
printf '%s\n' " - '$1' not found, quitting ..."
2019-05-29 21:09:58 +01:00
exit 1
fi
}
mainFunction() {
export "WINEARCH=${WINEARCH}"
export "WINEPREFIX=${WINEDIR}"
## Check prerequisites
2019-03-24 17:29:42 +01:00
colorEcho '[+] Checking prerequisites ...\n'
for bin in ${PREREQUISITES}; do
checkPrerequisite "${bin}"
done
checkRequiredVersions
## Create required directories
2018-05-28 13:12:07 +01:00
mkdir -pv "${BUILDROOT}"
mkdir -pv "${DLDIR}"
mkdir -pv "${OUTDIR}"
2018-05-28 14:00:05 +01:00
mkdir -pv "$TRIDIR"/web-ext-artifacts/
## Download Python and Pip
2019-03-24 17:29:42 +01:00
colorEcho '[+] Downloading necessary files ...\n'
if [ ! -f "${WINPY_EXE}" ]; then
wget \
"https://github.com/winpython/winpython/releases/download/1.10.20180404/WinPython32-${PYVER}.2Zero.exe" \
-O "${WINPY_EXE}"
fi
if [ ! "$(sha256sum "${WINPY_EXE}" \
2018-05-28 13:12:07 +01:00
| cut -d" " -f1)" = ${WINPY_HASH} ]; then
2019-03-24 17:29:42 +01:00
colorEcho '[-] '"${WINPY_EXE}"' has incorrect hash, quitting ...\n'
2018-05-28 13:12:07 +01:00
exit 1
fi
2018-05-28 13:12:07 +01:00
## Extract Python-3.5.4 from WinPython if required
2018-05-28 13:12:07 +01:00
rm -rf "${WINEDIR}"
local winepython="wine $PYDIR/python.exe"
2018-05-28 13:12:07 +01:00
if [ ! -f "$PYDIR/python.exe" ]; then
2019-03-24 17:29:42 +01:00
colorEcho '[+] Extract Python-'${PYVER}'\n'
2018-05-28 14:54:34 +01:00
7z x "${DLDIR}/winpython-${PYVER}.exe" "python-$PYVER" -o"$BUILDROOT"
2018-05-28 14:54:34 +01:00
$winepython -m pip install --upgrade pip
2019-03-24 17:29:42 +01:00
colorEcho '[+] Installing PyInstaller ...\n'
2018-05-28 14:54:34 +01:00
$winepython -m pip install pyinstaller
2018-05-28 13:12:07 +01:00
fi
## Compile with PyInstaller
2019-03-24 17:29:42 +01:00
colorEcho '[+] Compiling with PyInstaller under Wine ...\n'
rm -rf "${OUTDIR}"
2018-05-28 14:54:34 +01:00
PYTHONHASHSEED=1 wine "$PYDIR"/Scripts/pyinstaller.exe \
--clean \
--console \
--onefile \
--noupx \
--noconfir \
--log-level=ERROR \
--workpath "${OUTDIR}" \
--distpath "${OUTDIR}" \
2018-05-28 13:12:07 +01:00
"$TRIDIR/native/native_main.py"
## Test the compiled EXE
2019-03-24 17:29:42 +01:00
colorEcho '[+] Checking compiled binary ...\n'
OUTFILE="${OUTDIR}/native_main.exe"
2018-05-28 14:00:05 +01:00
cp "$OUTFILE" "$TRIDIR"/web-ext-artifacts/
if [ -f "${OUTFILE}" ]; then
python3 \
2018-05-28 13:12:07 +01:00
"$TRIDIR/native/gen_native_message.py" cmd..version \
| wine "$TRIDIR"/web-ext-artifacts/native_main.exe
2019-03-24 17:29:42 +01:00
printf '\n'
colorEcho '[+] PyInstaller with Wine was successful!\n'
else
colorEcho \
2019-03-24 17:29:42 +01:00
'[-] PyInstaller compilation failed, quitting ...\n' \
"alert"
2019-05-29 21:09:58 +01:00
exit 1
fi
}
mainFunction "$@"