4 # If using normal root, avoid changing anything.
5 if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
9 # If we don't have a python interpreter, avoid changing anything.
10 default_python=${1:-/usr/bin/python}
11 if [ ! -x "$default_python" ]; then
15 # Figure out how deep we need to descend. We could pick an insanely high
16 # number and hope it's enough, but somewhere, somebody's sure to run into it.
17 depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
18 xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
19 if [ -z "$depth" -o "$depth" -le "1" ]; then
23 # .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
24 # bytecode that they are for.
26 # The files below RPM_BUILD_ROOT could be targetting multiple versions of
27 # python (e.g. a single build that emits several subpackages e.g. a
28 # python26-foo subpackage, a python31-foo subpackage etc)
30 # Support this by assuming that below each /usr/lib/python$VERSION/, all
31 # .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
33 # For example, below /usr/lib/python2.6/, we're targetting /usr/bin/python2.6
34 # and below /usr/lib/python3.1/, we're targetting /usr/bin/python3.1
37 for python_libdir in "$RPM_BUILD_ROOT"/usr/lib{,64}/python[0-9].[0-9]/ ;
39 python_binary=/usr/bin/$(basename $python_libdir)
40 real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
41 echo "Bytecompiling .py files below $python_libdir using $python_binary"
43 # Generate normal (.pyc) byte-compiled files.
44 $python_binary -c 'import compileall, sys; sys.exit(not compileall.compile_dir("'"$python_libdir"'", '"$depth"', "'"$real_libdir"'", force=1, quiet=1))'
45 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
46 # One or more of the files had a syntax error
50 # Generate optimized (.pyo) byte-compiled files.
51 $python_binary -O -c 'import compileall, sys; sys.exit(not compileall.compile_dir("'"$python_libdir"'", '"$depth"', "'"$real_libdir"'", force=1, quiet=1))'
52 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
53 # One or more of the files had a syntax error
59 # Handle other locations in the filesystem using the default python
62 # Generate normal (.pyc) byte-compiled files.
63 $default_python -c 'import compileall, re, sys; sys.exit (not compileall.compile_dir("'"$RPM_BUILD_ROOT"'", '"$depth"', "/", 1, re.compile(r"'"/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]"'"), quiet=1))'
64 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
65 # One or more of the files had a syntax error
69 # Generate optimized (.pyo) byte-compiled files.
70 $default_python -O -c 'import compileall, re, sys; sys.exit(not compileall.compile_dir("'"$RPM_BUILD_ROOT"'", '"$depth"', "/", 1, re.compile(r"'"/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]"'"), quiet=1))' > /dev/null
71 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
72 # One or more of the files had a syntax error