Blame brp-python-bytecompile

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
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
dd8caa5
# In Python >= 3.9, compileall2 was merged back to standard library (compileall) so we can use it directly again.
e64ffd7
# Python < 3.4 (inc. Python 2) uses compileall module from stdlib with some hacks
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"
e1bd214
dd8caa5
    python_version=$($python_binary -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
dd8caa5
dd8caa5
    #
dd8caa5
    # Python 3.4 and higher
dd8caa5
    #
dd8caa5
    if [ "$python_version" -ge 34 ]; then
dd8caa5
dd8caa5
        # We compile all opt levels in one go: only when $options is empty.
dd8caa5
        if [ -n "$options" ]; then
dd8caa5
            return
dd8caa5
        fi
dd8caa5
dd8caa5
        if [ "$python_version" -ge 39 ]; then
dd8caa5
            # For Pyhon 3.9+, use the standard library
dd8caa5
            compileall_module=compileall
dd8caa5
        else
dd8caa5
            # For older Pythons, use compileall2
dd8caa5
            compileall_module=compileall2
dd8caa5
        fi
dd8caa5
dd8caa5
        [ ! -z $exclude ] && exclude="-x '$exclude'"
dd8caa5
dd8caa5
        # PYTHONPATH is needed for compileall2, but doesn't hurt for the stdlib
dd8caa5
        # -o 0 -o 1 are the optimization levels
dd8caa5
        # -q disables verbose output
dd8caa5
        # -f forces the process to overwrite existing compiled files
dd8caa5
        # -x excludes paths defined by regex
dd8caa5
        # -e excludes symbolic links pointing outside the build root
dd8caa5
        # -x and -e together implements the same functionality as the Filter class below
dd8caa5
        # -s strips $RPM_BUILD_ROOT from the path
dd8caa5
        # -p prepends the leading slash to the path to make it absolute
dd8caa5
        PYTHONPATH=/usr/lib/rpm/redhat/ $python_binary -B -m $compileall_module -o 0 -o 1 -q -f $exclude -s "$RPM_BUILD_ROOT" -p / --hardlink-dupes -e "$RPM_BUILD_ROOT" "$python_libdir"
dd8caa5
dd8caa5
    else
e64ffd7
#
e64ffd7
# Python 3.3 and lower (incl. Python 2)
e64ffd7
#
e64ffd7
0044db1
local real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
0044db1
e1bd214
cat << EOF | $python_binary $options
e1bd214
import compileall, sys, os, re
e1bd214
e1bd214
python_libdir = "$python_libdir"
0044db1
depth = sys.getrecursionlimit()
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
	echo "Bytecompiling .py files below $python_libdir using $python_binary"
e1bd214
e1bd214
	# Generate normal (.pyc) byte-compiled files.
0044db1
	python_bytecompile "" "$python_binary" "" "$python_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.
dd8caa5
	# N.B. For Python 3.4+, this call does nothing
0044db1
	python_bytecompile "-O" "$python_binary" "" "$python_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