Last active
December 13, 2020 23:40
-
-
Save iuridiniz/9539d8a3727635ee4750f25f4e36db10 to your computer and use it in GitHub Desktop.
bundle python files into a single zip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
PYTHON="/usr/bin/env python2" | |
BASEDIR=$(cd -P "`dirname "$0"`" && pwd) | |
SOURCE="${BASEDIR}/src" | |
OUTPUT="${BASEDIR}/app" | |
tmpfile=$(mktemp --suffix .zip) | |
# clear cache | |
find "$SOURCE" -name '*.pyc' -delete | |
find "$SOURCE" -name '__pycache__' -delete | |
# compile files | |
# check python version | |
if $PYTHON -c 'import sys; exit (not (sys.version_info[0] >= 3 and sys.version_info[1] >=2) )'; then | |
# python >= 3.2 uses __pycache__ | |
compile_opts="-b" | |
else | |
compile_opts="" | |
fi | |
$PYTHON -mcompileall $compile_opts "$SOURCE" | |
# content (zipped) | |
(cd "$SOURCE" && zip -r - . -x "*.py") > "$tmpfile" | |
# header (she bang) | |
echo "#!$PYTHON" > "$OUTPUT" | |
cat "$tmpfile" >> "$OUTPUT" | |
# make it executable | |
chmod +x "$OUTPUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment