Blame SOURCES/brp-python-bytecompile-with-scl-python-38

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