4 # If using normal root, avoid changing anything.
5 if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
9 # Figure out how deep we need to descend. We could pick an insanely high
10 # number and hope it's enough, but somewhere, somebody's sure to run into it.
11 depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
12 xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
13 if [ -z "$depth" -o "$depth" -le "1" ]; then
17 function python_bytecompile()
20 local python_binary=$2
22 local python_libdir=$4
26 cat << EOF | $python_binary $options
27 import compileall, sys, os, re
29 python_libdir = "$python_libdir"
31 real_libdir = "$real_libdir"
32 build_root = "$RPM_BUILD_ROOT"
36 def search(self, path):
37 ret = not os.path.realpath(path).startswith(build_root)
39 ret = ret or re.search(exclude, path)
42 sys.exit(not compileall.compile_dir(python_libdir, depth, real_libdir, force=1, rx=Filter(), quiet=1))
46 # .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
47 # bytecode that they are for.
49 # The files below RPM_BUILD_ROOT could be targeting multiple versions of
50 # python (e.g. a single build that emits several subpackages e.g. a
51 # python26-foo subpackage, a python31-foo subpackage etc)
53 # Support this by assuming that below each /usr/lib/python$VERSION/, all
54 # .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
56 # For example, below /usr/lib/python2.6/, we're targeting /usr/bin/python2.6
57 # and below /usr/lib/python3.1/, we're targeting /usr/bin/python3.1
60 for python_libdir in `find "$RPM_BUILD_ROOT" -type d|grep -E "/usr/lib(64)?/python[0-9]\.[0-9]$"`;
62 python_binary=/usr/bin/$(basename $python_libdir)
63 real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
64 echo "Bytecompiling .py files below $python_libdir using $python_binary"
66 # Generate normal (.pyc) byte-compiled files.
67 python_bytecompile "" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
68 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
69 # One or more of the files had a syntax error
73 # Generate optimized (.pyo) byte-compiled files.
74 python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
75 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
76 # One or more of the files had a syntax error
82 # Handle other locations in the filesystem using the default python
83 # implementation - if we have a default python interpreter
85 default_python=${1:-/usr/bin/python}
86 if [ ! -x "$default_python" ]; then
90 # Generate normal (.pyc) byte-compiled files.
91 python_bytecompile "" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
92 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
93 # One or more of the files had a syntax error
97 # Generate optimized (.pyo) byte-compiled files.
98 python_bytecompile "-O" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
99 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
100 # One or more of the files had a syntax error