Blame scripts/brp-python-bytecompile

2ff057
#!/bin/bash
2ff057
errors_terminate=$2
2ff057
extra=$3
2ff057
2ff057
# If using normal root, avoid changing anything.
2ff057
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
2ff057
	exit 0
2ff057
fi
2ff057
2ff057
# Figure out how deep we need to descend.  We could pick an insanely high
2ff057
# number and hope it's enough, but somewhere, somebody's sure to run into it.
2ff057
depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
2ff057
       xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
2ff057
if [ -z "$depth" -o "$depth" -le "1" ]; then
2ff057
	exit 0
2ff057
fi
2ff057
2ff057
function python_bytecompile()
2ff057
{
2ff057
    local options=$1
2ff057
    local python_binary=$2
2ff057
    local exclude=$3
2ff057
    local python_libdir=$4
2ff057
    local depth=$5
2ff057
    local real_libdir=$6
2ff057
2ff057
cat << EOF | $python_binary $options
2ff057
import compileall, sys, os, re
2ff057
2ff057
python_libdir = "$python_libdir"
2ff057
depth = $depth
2ff057
real_libdir = "$real_libdir"
2ff057
build_root = "$RPM_BUILD_ROOT"
2ff057
exclude = r"$exclude"
2ff057
2ff057
class Filter:
2ff057
    def search(self, path):
2ff057
        ret = not os.path.realpath(path).startswith(build_root)
2ff057
        if exclude:
2ff057
            ret = ret or re.search(exclude, path)
2ff057
        return ret
2ff057
2ff057
sys.exit(not compileall.compile_dir(python_libdir, depth, real_libdir, force=1, rx=Filter(), quiet=1))
2ff057
EOF
2ff057
}
2ff057
2ff057
# .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
2ff057
# bytecode that they are for.
2ff057
#
2ff057
# The files below RPM_BUILD_ROOT could be targeting multiple versions of
2ff057
# python (e.g. a single build that emits several subpackages e.g. a
2ff057
# python26-foo subpackage, a python31-foo subpackage etc)
2ff057
#
2ff057
# Support this by assuming that below each /usr/lib/python$VERSION/, all
2ff057
# .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
2ff057
# 
2ff057
# For example, below /usr/lib/python2.6/, we're targeting /usr/bin/python2.6
2ff057
# and below /usr/lib/python3.1/, we're targeting /usr/bin/python3.1
2ff057
2ff057
shopt -s nullglob
2ff057
for python_libdir in `find "$RPM_BUILD_ROOT" -type d|grep -E "/usr/lib(64)?/python[0-9]\.[0-9]$"`;
2ff057
do
2ff057
	python_binary=/usr/bin/$(basename $python_libdir)
2ff057
	real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
2ff057
	echo "Bytecompiling .py files below $python_libdir using $python_binary"
2ff057
2ff057
	# Generate normal (.pyc) byte-compiled files.
2ff057
	python_bytecompile "" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
2ff057
	if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
2ff057
		# One or more of the files had a syntax error
2ff057
		exit 1
2ff057
	fi
2ff057
2ff057
	# Generate optimized (.pyo) byte-compiled files.
2ff057
	python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
2ff057
	if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
2ff057
		# One or more of the files had a syntax error
2ff057
		exit 1
2ff057
	fi
2ff057
done
2ff057
2ff057
2ff057
# Handle other locations in the filesystem using the default python implementation
2ff057
# if extra is set to 0, don't do this
2ff057
if [ 0$extra -eq 0 ]; then
2ff057
	exit 0
2ff057
fi
2ff057
2ff057
# If we don't have a default python interpreter, we cannot proceed
2ff057
default_python=${1:-/usr/bin/python}
2ff057
if [ ! -x "$default_python" ]; then
2ff057
	exit 0
2ff057
fi
2ff057
2ff057
# Figure out if there are files to be bytecompiled with the default_python at all
2ff057
# this prevents unnecessary default_python invocation
2ff057
find "$RPM_BUILD_ROOT" -type f -name "*.py" | grep -Ev "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" || exit 0
2ff057
2ff057
# Generate normal (.pyc) byte-compiled files.
2ff057
python_bytecompile "" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
2ff057
if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
2ff057
	# One or more of the files had a syntax error
2ff057
	exit 1
2ff057
fi
2ff057
2ff057
# Generate optimized (.pyo) byte-compiled files.
2ff057
python_bytecompile "-O" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
2ff057
if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
2ff057
	# One or more of the files had a syntax error
2ff057
	exit 1
2ff057
fi
2ff057
exit 0