From: Michael Tremer Date: Tue, 2 Oct 2012 22:45:11 +0000 (+0200) Subject: python: Byte-compile all files in sitelib. X-Git-Tag: 0.9.24~69 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e9e430d4fd5b09327f57651726ffac1b7874ae01;p=pakfire.git python: Byte-compile all files in sitelib. For files which are located elsewhere, it is required to call: python_bytecompile ... (Python 2) or python3_bytecompile ... (Python 3) --- diff --git a/macros/build.macro b/macros/build.macro index 698706806..4e4d830bf 100644 --- a/macros/build.macro +++ b/macros/build.macro @@ -46,7 +46,17 @@ def MACRO_INSTALL_LOGROTATE_FILES end def MACRO_PYTHON_COMPILE - # XXX TODO + if [ -x "%{python3}" ]; then + %{python3_bytecompile} \ + %{BUILDROOT}%{python3_sitearch} \ + %{BUILDROOT}%{python3_sitelib} + fi + + if [ -x "%{python}" ]; then + %{python_bytecompile} \ + %{BUILDROOT}%{python_sitearch} \ + %{BUILDROOT}%{python_sitelib} + fi end MACRO_PERL_CLEANUP diff --git a/macros/python.macro b/macros/python.macro index 1b417f269..15c1209ec 100644 --- a/macros/python.macro +++ b/macros/python.macro @@ -1,7 +1,9 @@ # A bunch of predefined things for Python. +python_bytecompile_script = /usr/lib/pakfire/py-compile # Python 3 constants. python3 = /usr/bin/python3 +python3_bytecompile = %{python_bytecompile_script} --python=%{python3} python3_sitearch = %(%{python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))") python3_sitelib = %(%{python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") @@ -9,6 +11,7 @@ python3_version = %(%{python3} -c "import sys; sys.stdout.write(sys.version[:3] # Python 2 constants. python = /usr/bin/python2 +python_bytecompile = %{python_bytecompile_script} --python=%{python} python_sitearch = %(%{python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)") python_sitelib = %(%{python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()") diff --git a/tools/py-compile b/tools/py-compile index 66c33edfe..5ccd8f9e6 100755 --- a/tools/py-compile +++ b/tools/py-compile @@ -1,48 +1,58 @@ #!/bin/sh -PYTHON=$(which python 2>/dev/null) +python_interpreter="python" +paths="" + +while [ $# -gt 0 ]; do + case "${1}" in + --python=*) + python_interpreter=${1#--python=} + ;; + *) + paths="${paths} ${1}" + ;; + esac + shift +done -if [ -z "${PYTHON}" ]; then - # Python is not present. Fail silently. - exit 0 +if [ -z "${paths}" ]; then + echo >&2 "No path specified!" + exit 1 fi -files="" -for i in $*; do - if [ -e ${i}c ] && [ -e ${i}o ]; then - continue # all files we want are already there - fi - files="$files $i" -done +if [ "${python_interpreter:0:1}" != "/" ]; then + python_interpreter=$(which ${python_interpreter} 2>/dev/null) +fi -if [ -z "${files}" ]; then - # No files need to be proceeded. - exit 0 +if [ ! -x "${python_interpreter}" ]; then + echo >&2 "Python interpreter is not executable: ${python_interpreter}" + exit 1 fi -$PYTHON -c " -import sys, os, string, py_compile - -files = '''$files''' -print 'Byte-compiling python modules...' -for file in string.split(files): - if not os.path.exists(file) or not (len(file) >= 3 and file[-3:] == '.py'): - continue - print file, - sys.stdout.flush() - py_compile.compile(file) -print" || exit $? - -# this will fail for python < 1.5, but that doesn't matter ... -$PYTHON -O -c " -import sys, os, string, py_compile - -files = '''$files''' -print 'Byte-compiling python modules (optimised versions) ...' -for file in string.split(files): - if not os.path.exists(file) or not (len(file) >= 3 and file[-3:] == '.py'): - continue - print file, - sys.stdout.flush() - py_compile.compile(file) -print" 2>/dev/null || : +tempfile=$(mktemp) +trap "rm -f ${tempfile}" EXIT + +cat > ${tempfile} <