| 1 | #!/usr/bin/env bash |
|---|
| 2 | |
|---|
| 3 | # This tries to compile a Python script into a self-contained binary. |
|---|
| 4 | # It uses the freeze.py tool in the standard Python distribution. |
|---|
| 5 | # Dependencies are statically linked, with the exception of glibc. |
|---|
| 6 | # Compilation will exclude most of the "batteries included" in the |
|---|
| 7 | # Python distribution, as they are not appropriate for infoarena. |
|---|
| 8 | # |
|---|
| 9 | # Usage: |
|---|
| 10 | # pybin.sh <python-dist-dir> <temp-dir> <python-script> <output-binary> |
|---|
| 11 | # (Note: It will fill your temp dir with tons of files!) |
|---|
| 12 | # |
|---|
| 13 | # Example: |
|---|
| 14 | # ./pybin.sh ~/Desktop/Python-2.6.1 /tmp/pybin myscript.py mybinary |
|---|
| 15 | |
|---|
| 16 | if [ $# -ne 4 ] |
|---|
| 17 | then |
|---|
| 18 | echo -n "Usage: " |
|---|
| 19 | echo "pybin.sh <python-dist-dir> <temp-dir> <python-script> <output-binary>" |
|---|
| 20 | exit 1 |
|---|
| 21 | fi |
|---|
| 22 | |
|---|
| 23 | DIR_PY_DIST=$1 |
|---|
| 24 | DIR_TMP=$2 |
|---|
| 25 | PY_SRC=$3 |
|---|
| 26 | PY_BINARY=$4 |
|---|
| 27 | PY_TMP_BINARY=$DIR_TMP/`basename $PY_SRC | cut -f1 -d"."` |
|---|
| 28 | |
|---|
| 29 | echo Freezing python module... |
|---|
| 30 | FREEZE="$DIR_PY_DIST/python $DIR_PY_DIST/Tools/freeze/freeze.py" |
|---|
| 31 | $FREEZE -q -o $DIR_TMP \ |
|---|
| 32 | -X BaseHTTPServer -X distutils -X difflib -X EasyDialogs -X email \ |
|---|
| 33 | -X email.utils -X FixTk -X ftplib -X getopt -X gettext \ |
|---|
| 34 | -X httplib -X MacOS -X mimetools -X msvcrt -X ntpath \ |
|---|
| 35 | -X pdb -X pkgutil -X pydoc -X readline -X riscos -X shlex -X socket \ |
|---|
| 36 | -X SocketServer -X sre -X sre_compile -X sre_constants -X sre_parse \ |
|---|
| 37 | -X ssl -X subprocess -X textwrap -X threading -X Tkinter \ |
|---|
| 38 | -X unittest -X urllib -X uu -X _warnings -X webbrowser $PY_SRC |
|---|
| 39 | |
|---|
| 40 | echo Compiling python binary... |
|---|
| 41 | # Change Makefile, add static link flag. |
|---|
| 42 | mv $DIR_TMP/Makefile $DIR_TMP/Makefile.dynamic |
|---|
| 43 | sed $DIR_TMP/Makefile.dynamic -e 's/^LDFLAGS=\(.*\)/LDFLAGS=\1 --static/' > $DIR_TMP/Makefile |
|---|
| 44 | make -C $DIR_TMP 1>/dev/null |
|---|
| 45 | cp -p $PY_TMP_BINARY $PY_BINARY 2>/dev/null |
|---|
| 46 | echo Done |
|---|
| 47 | |
|---|