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