5361740
#!/bin/bash
5361740
errors_terminate=$2
5361740
extra=$3
5361740
5361740
# If using normal root, avoid changing anything.
5361740
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
5361740
	exit 0
5361740
fi
5361740
5361740
# Figure out how deep we need to descend.  We could pick an insanely high
5361740
# number and hope it's enough, but somewhere, somebody's sure to run into it.
5361740
depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
5361740
       xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
5361740
if [ -z "$depth" -o "$depth" -le "1" ]; then
5361740
	exit 0
5361740
fi
5361740
5437dfc
# This function now implements Python byte-compilation in two different ways:
5437dfc
# Python >= 3.4 uses a new module compileall2 - https://github.com/fedora-python/compileall2
5437dfc
# Python < 3.4 (inc. Python 2) uses compileall module from stdlib with some hacks
5437dfc
# When we drop support for Python 2, we'd be able to use all compileall2 features like:
5437dfc
# - -s and -p options to manipulate with a path baked into pyc files instead of $real_libdir
5437dfc
# - -o 0 -o 1 to produce multiple files in one run - each with a different optimization level - instead of $options
5437dfc
# - removed useless $depth - both compileall and compileall2 are limited by sys.getrecursionlimit()
5437dfc
# These changes will make this script much simpler
5361740
function python_bytecompile()
5361740
{
5361740
    local options=$1
5361740
    local python_binary=$2
5361740
    local exclude=$3
5361740
    local python_libdir=$4
6335a7f
    local depth=$5 # Not used for Python >= 3.4
6335a7f
    local real_libdir=$6 # Not used for Python >= 3.4
5361740
5437dfc
	python_version=$($python_binary -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
5437dfc
5437dfc
	#
5437dfc
	# Python 3.4 and higher
5437dfc
	#
5437dfc
	if [ "$python_version" -ge 34 ]; then
5437dfc
5437dfc
		[ ! -z $exclude ] && exclude="-x '$exclude'"
5437dfc
		# /usr/lib/rpm/redhat/ contains compileall2 Python module
5437dfc
		# -q disables verbose output
5437dfc
		# -f forces the process to overwrite existing compiled files
5437dfc
		# -x excludes paths defined by regex
5437dfc
		# -e excludes symbolic links pointing outside the build root
5437dfc
		# -x and -e together implements the same functionality as the Filter class below
6335a7f
		# -s strips $RPM_BUILD_ROOT from the path
6335a7f
		# -p prepends the leading slash to the path to make it absolute
312bfba
		PYTHONPATH=/usr/lib/rpm/redhat/ $python_binary -B $options -m compileall2 -q -f $exclude -s $RPM_BUILD_ROOT -p / -e $RPM_BUILD_ROOT $python_libdir
5437dfc
	else
5437dfc
#
5437dfc
# Python 3.3 and lower (incl. Python 2)
5437dfc
#
5437dfc
5361740
cat << EOF | $python_binary $options
5361740
import compileall, sys, os, re
5361740
5361740
python_libdir = "$python_libdir"
5361740
depth = $depth
5361740
real_libdir = "$real_libdir"
5361740
build_root = "$RPM_BUILD_ROOT"
5361740
exclude = r"$exclude"
5361740
5361740
class Filter:
5361740
    def search(self, path):
5361740
        ret = not os.path.realpath(path).startswith(build_root)
5361740
        if exclude:
5361740
            ret = ret or re.search(exclude, path)
5361740
        return ret
5361740
5361740
sys.exit(not compileall.compile_dir(python_libdir, depth, real_libdir, force=1, rx=Filter(), quiet=1))
5361740
EOF
5437dfc
5437dfc
fi
5361740
}
5361740
5361740
# .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
5361740
# bytecode that they are for.
5361740
#
5361740
# The files below RPM_BUILD_ROOT could be targeting multiple versions of
5361740
# python (e.g. a single build that emits several subpackages e.g. a
5361740
# python26-foo subpackage, a python31-foo subpackage etc)
5361740
#
5361740
# Support this by assuming that below each /usr/lib/python$VERSION/, all
5361740
# .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
5361740
# 
5361740
# For example, below /usr/lib/python2.6/, we're targeting /usr/bin/python2.6
5361740
# and below /usr/lib/python3.1/, we're targeting /usr/bin/python3.1
5361740
5361740
shopt -s nullglob
9ce9926
for python_libdir in `find "$RPM_BUILD_ROOT" -type d|grep -E "/usr/lib(64)?/python[0-9]\.[0-9]+$"`;
5361740
do
5361740
	python_binary=/usr/bin/$(basename $python_libdir)
5361740
	real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
5361740
	echo "Bytecompiling .py files below $python_libdir using $python_binary"
5361740
5361740
	# Generate normal (.pyc) byte-compiled files.
5361740
	python_bytecompile "" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
5361740
	if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
5361740
		# One or more of the files had a syntax error
5361740
		exit 1
5361740
	fi
5361740
5361740
	# Generate optimized (.pyo) byte-compiled files.
5361740
	python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
5361740
	if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
5361740
		# One or more of the files had a syntax error
5361740
		exit 1
5361740
	fi
5361740
done
5361740
5361740
5361740
# Handle other locations in the filesystem using the default python implementation
5361740
# if extra is set to 0, don't do this
5361740
if [ 0$extra -eq 0 ]; then
5361740
	exit 0
5361740
fi
5361740
5361740
# If we don't have a default python interpreter, we cannot proceed
5361740
default_python=${1:-/usr/bin/python}
5361740
if [ ! -x "$default_python" ]; then
5361740
	exit 0
5361740
fi
5361740
5361740
# Figure out if there are files to be bytecompiled with the default_python at all
5361740
# this prevents unnecessary default_python invocation
5361740
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
5361740
5361740
# Generate normal (.pyc) byte-compiled files.
5361740
python_bytecompile "" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
5361740
if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
5361740
	# One or more of the files had a syntax error
5361740
	exit 1
5361740
fi
5361740
5361740
# Generate optimized (.pyo) byte-compiled files.
5361740
python_bytecompile "-O" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
5361740
if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
5361740
	# One or more of the files had a syntax error
5361740
	exit 1
5361740
fi
5361740
exit 0