Skip to content

Instantly share code, notes, and snippets.

@iuridiniz
Last active December 13, 2020 23:40
Show Gist options
  • Save iuridiniz/9539d8a3727635ee4750f25f4e36db10 to your computer and use it in GitHub Desktop.
Save iuridiniz/9539d8a3727635ee4750f25f4e36db10 to your computer and use it in GitHub Desktop.
bundle python files into a single zip
#!/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