Use a new module compileall2 for Python byte-compilation

This commit is contained in:
Lumir Balhar 2019-07-11 14:24:39 +02:00
commit 76681ad58e
3 changed files with 500 additions and 18 deletions

View file

@ -1,7 +1,7 @@
# Note that the path could itself be a python file, or a directory
# Python's compile_all module only works on directories, and requires a max
# recursion depth
# Note that the py_byte_compile macro should work for all Python versions
# Which unfortunately makes the definition more complicated than it should be
# Usage:
# %py_byte_compile <interpereter> <path>
@ -13,12 +13,22 @@
# (%{py_byte_compile <interpereter> <path>}) || :
%py_byte_compile()\
python_binary="%1"\
buildroot_path="%2"\
bytecode_compilation_path=".${buildroot_path/#$RPM_BUILD_ROOT}"\
failure=0\
pushd $RPM_BUILD_ROOT\
find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -O -m py_compile || failure=1\
find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -m py_compile || failure=1\
popd\
test $failure -eq 0
py2_byte_compile () {\
python_binary="%1"\
bytecode_compilation_path="%2"\
failure=0\
find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("'"$RPM_BUILD_ROOT"'")[2], doraise=True) for f in sys.argv[1:]]' || failure=1\
find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -O -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("'"$RPM_BUILD_ROOT"'")[2], doraise=True) for f in sys.argv[1:]]' || failure=1\
test $failure -eq 0\
}\
\
py3_byte_compile () {\
python_binary="%1"\
bytecode_compilation_path="%2"\
PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -B -m compileall2 -o 0 -o 1 -s $RPM_BUILD_ROOT -p / $bytecode_compilation_path \
}\
\
# Get version without a dot (36 instead of 3.6), bash doesn't compare floats well \
python_version=$(%1 -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))") \
# compileall2 Python module is not compatible with Python < 3.4 \
if [ "$python_version" -ge 34 ]; then py3_byte_compile "%1" "%2"; else py2_byte_compile "%1" "%2"; fi