Skip to content

Instantly share code, notes, and snippets.

@bsnux
Created July 18, 2019 17:25
Show Gist options
  • Save bsnux/cd6d35b392a2adcb0b730f666093207b to your computer and use it in GitHub Desktop.
Save bsnux/cd6d35b392a2adcb0b730f666093207b to your computer and use it in GitHub Desktop.
Generates a binary file for Linux from a Python source
#!/bin/zsh
#--------------------------------------------------------------
# py-compile.sh
#
# Generates a binary file for Linux from a Python source
#
# Usage: py-compile.sh myfile.py
#--------------------------------------------------------------
set -e
python_file=$1
output_dir="/tmp/"
output_file="${python_file//\.py/}"
cat > Dockerfile_py <<EOF
FROM python:3.7.4-buster
WORKDIR /app
RUN pip install PyInstaller==3.4
EOF
dir_to_delete=('__pycache__' 'build' 'dist' 't.spec')
for dir in $dir_to_delete; do
if [ -d "$dir" ] || [ -f "$dir" ]; then
rm -rf $dir
fi
done
if [[ ! $(docker images | grep python-compiler | head -1 | awk '{ print $1 }') ]]; then
echo "Creating python-compiler:latest image..."
docker build . -t python-compiler:latest -f Dockerfile_py
fi
echo "Running pyinstaller command in container..."
docker run --name pyc -ti -v $(pwd):/app python-compiler pyinstaller -F \
/app/$python_file -n $output_file && docker cp pyc:/app/dist/$output_file $output_dir \
&& docker stop pyc && docker rm pyc
echo "Deleting Dockerfile..."
rm Dockerfile_py
echo "Done! Your binary file is in $output_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment