e1bd214
#!/bin/bash
e1bd214
errors_terminate=$2
f77cb3e
f77cb3e
# Usage of %_python_bytecompile_extra is not allowed anymore
f77cb3e
# See: https://fedoraproject.org/wiki/Changes/No_more_automagic_Python_bytecompilation_phase_3
f77cb3e
# Therefore $1 ($default_python) is not needed and is invoked with "" by default.
f77cb3e
# $default_python stays in the arguments for backward compatibility and $extra for the following check:
e1bd214
extra=$3
f77cb3e
if [ 0$extra -eq 1 ]; then
f77cb3e
    echo -e "%_python_bytecompile_extra is discontinued, use %py_byte_compile instead.\nSee: https://fedoraproject.org/wiki/Changes/No_more_automagic_Python_bytecompilation_phase_3" >/dev/stderr
f77cb3e
    exit 1
f77cb3e
fi
e1bd214
e1bd214
# If using normal root, avoid changing anything.
e1bd214
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
e1bd214
	exit 0
e1bd214
fi
e1bd214
e1bd214
# Figure out how deep we need to descend.  We could pick an insanely high
e1bd214
# number and hope it's enough, but somewhere, somebody's sure to run into it.
e1bd214
depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
e1bd214
       xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
e1bd214
if [ -z "$depth" -o "$depth" -le "1" ]; then
e1bd214
	exit 0
e1bd214
fi
e1bd214
8e9c3d8
# This function now implements Python byte-compilation in three different ways:
8e9c3d8
# Python >= 3.4 and < 3.9 uses a new module compileall2 - https://github.com/fedora-python/compileall2
e64ffd7
# Python < 3.4 (inc. Python 2) uses compileall module from stdlib with some hacks
e64ffd7
# When we drop support for Python 2, we'd be able to use all compileall2 features like:
e64ffd7
# - -s and -p options to manipulate with a path baked into pyc files instead of $real_libdir
e64ffd7
# - -o 0 -o 1 to produce multiple files in one run - each with a different optimization level - instead of $options
e64ffd7
# - removed useless $depth - both compileall and compileall2 are limited by sys.getrecursionlimit()
e64ffd7
# These changes will make this script much simpler
8e9c3d8
# In Python >= 3.9, compileall2 was merged back to standard library (compileall) so we can use it directly again.
e1bd214
function python_bytecompile()
e1bd214
{
e1bd214
    local options=$1
e1bd214
    local python_binary=$2
e1bd214
    local exclude=$3
Ben Burton c487f82
    local python_libdir="$4"
8f6bc2f
    local depth=$5 # Not used for Python >= 3.4
8f6bc2f
    local real_libdir=$6 # Not used for Python >= 3.4
e1bd214
e64ffd7
	python_version=$($python_binary -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
e64ffd7
e64ffd7
	#
8e9c3d8
	# Python 3.9 and higher
8e9c3d8
	#
8e9c3d8
	if [ "$python_version" -ge 39 ]; then
8e9c3d8
8e9c3d8
		[ ! -z $exclude ] && exclude="-x '$exclude'"
8e9c3d8
		# -q disables verbose output
8e9c3d8
		# -f forces the process to overwrite existing compiled files
8e9c3d8
		# -x excludes paths defined by regex
8e9c3d8
		# -e excludes symbolic links pointing outside the build root
8e9c3d8
		# -x and -e together implements the same functionality as the Filter class below
8e9c3d8
		# -s strips $RPM_BUILD_ROOT from the path
8e9c3d8
		# -p prepends the leading slash to the path to make it absolute
Ben Burton c487f82
		$python_binary -B $options -m compileall -q -f $exclude -s "$RPM_BUILD_ROOT" -p / -e "$RPM_BUILD_ROOT" "$python_libdir"
8e9c3d8
8e9c3d8
	#
e64ffd7
	# Python 3.4 and higher
e64ffd7
	#
8e9c3d8
	elif [ "$python_version" -ge 34 ]; then
e64ffd7
e64ffd7
		[ ! -z $exclude ] && exclude="-x '$exclude'"
e64ffd7
		# /usr/lib/rpm/redhat/ contains compileall2 Python module
e64ffd7
		# -q disables verbose output
e64ffd7
		# -f forces the process to overwrite existing compiled files
e64ffd7
		# -x excludes paths defined by regex
e64ffd7
		# -e excludes symbolic links pointing outside the build root
e64ffd7
		# -x and -e together implements the same functionality as the Filter class below
8f6bc2f
		# -s strips $RPM_BUILD_ROOT from the path
8f6bc2f
		# -p prepends the leading slash to the path to make it absolute
Ben Burton c487f82
		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"
e64ffd7
	else
e64ffd7
#
e64ffd7
# Python 3.3 and lower (incl. Python 2)
e64ffd7
#
e64ffd7
e1bd214
cat << EOF | $python_binary $options
e1bd214
import compileall, sys, os, re
e1bd214
e1bd214
python_libdir = "$python_libdir"
e1bd214
depth = $depth
e1bd214
real_libdir = "$real_libdir"
e1bd214
build_root = "$RPM_BUILD_ROOT"
e1bd214
exclude = r"$exclude"
e1bd214
e1bd214
class Filter:
e1bd214
    def search(self, path):
e1bd214
        ret = not os.path.realpath(path).startswith(build_root)
e1bd214
        if exclude:
e1bd214
            ret = ret or re.search(exclude, path)
e1bd214
        return ret
e1bd214
e1bd214
sys.exit(not compileall.compile_dir(python_libdir, depth, real_libdir, force=1, rx=Filter(), quiet=1))
e1bd214
EOF
e64ffd7
e64ffd7
fi
e1bd214
}
e1bd214
e1bd214
# .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
e1bd214
# bytecode that they are for.
e1bd214
#
e1bd214
# The files below RPM_BUILD_ROOT could be targeting multiple versions of
e1bd214
# python (e.g. a single build that emits several subpackages e.g. a
e1bd214
# python26-foo subpackage, a python31-foo subpackage etc)
e1bd214
#
e1bd214
# Support this by assuming that below each /usr/lib/python$VERSION/, all
e1bd214
# .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
437166c
#
e1bd214
# For example, below /usr/lib/python2.6/, we're targeting /usr/bin/python2.6
e1bd214
# and below /usr/lib/python3.1/, we're targeting /usr/bin/python3.1
e1bd214
71c410d
# Disable Python hash seed randomization
71c410d
# This should help with byte-compilation reproducibility: https://bugzilla.redhat.com/show_bug.cgi?id=1686078
71c410d
export PYTHONHASHSEED=0
71c410d
e1bd214
shopt -s nullglob
Ben Burton c487f82
find "$RPM_BUILD_ROOT" -type d -print0|grep -z -E "/(usr|app)/lib(64)?/python[0-9]\.[0-9]+$" | while read -d "" python_libdir;
e1bd214
do
Ben Burton c487f82
	python_binary=$(basename "$python_libdir")
e1bd214
	real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
e1bd214
	echo "Bytecompiling .py files below $python_libdir using $python_binary"
e1bd214
e1bd214
	# Generate normal (.pyc) byte-compiled files.
e1bd214
	python_bytecompile "" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
e1bd214
	if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
e1bd214
		# One or more of the files had a syntax error
e1bd214
		exit 1
e1bd214
	fi
e1bd214
e1bd214
	# Generate optimized (.pyo) byte-compiled files.
e1bd214
	python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
e1bd214
	if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
e1bd214
		# One or more of the files had a syntax error
e1bd214
		exit 1
e1bd214
	fi
e1bd214
done