Blame SOURCES/macros.pybytecompile

51cf4a
# Note that the path could itself be a python file, or a directory
51cf4a
51cf4a
# Note that the py_byte_compile macro should work for all Python versions
51cf4a
# Which unfortunately makes the definition more complicated than it should be
51cf4a
51cf4a
# Usage:
51cf4a
#    %%py_byte_compile <interpereter> <path>
51cf4a
# Example:
51cf4a
#    %%py_byte_compile %%{__python3} %%{buildroot}%%{_datadir}/spam/plugins/
51cf4a
51cf4a
# This will terminate build on SyntaxErrors, if you want to avoid that,
51cf4a
# use it in a subshell like this:
51cf4a
#    (%%{py_byte_compile <interpereter> <path>}) || :
51cf4a
51cf4a
# Setting PYTHONHASHSEED=0 disables Python hash seed randomization
51cf4a
# This should help with byte-compilation reproducibility: https://bugzilla.redhat.com/show_bug.cgi?id=1686078
51cf4a
51cf4a
%py_byte_compile()\
51cf4a
py2_byte_compile () {\
51cf4a
    python_binary="env PYTHONHASHSEED=0 %1"\
51cf4a
    bytecode_compilation_path="%2"\
51cf4a
    failure=0\
51cf4a
    find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -s -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\
51cf4a
    find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -s -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\
51cf4a
    test $failure -eq 0\
51cf4a
}\
51cf4a
\
51cf4a
py3_byte_compile () {\
51cf4a
    python_binary="env PYTHONHASHSEED=0 %1"\
51cf4a
    bytecode_compilation_path="%2"\
51cf4a
    PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m compileall2 -o 0 -o 1 -s $RPM_BUILD_ROOT -p / $bytecode_compilation_path \
51cf4a
}\
51cf4a
\
51cf4a
py39_byte_compile () {\
51cf4a
    python_binary="env PYTHONHASHSEED=0 %1"\
51cf4a
    bytecode_compilation_path="%2"\
51cf4a
    $python_binary -s -B -m compileall -o 0 -o 1 -s $RPM_BUILD_ROOT -p / $bytecode_compilation_path \
51cf4a
}\
51cf4a
\
51cf4a
# Path to intepreter should not contain any arguments \
51cf4a
[[ "%1" =~ " -" ]] && echo "ERROR py_byte_compile: Path to interpreter should not contain any arguments" >&2 && exit 1 \
51cf4a
# Get version without a dot (36 instead of 3.6), bash doesn't compare floats well \
51cf4a
python_version=$(%1 -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))") \
51cf4a
# compileall2 is an enhanced fork of stdlib compileall module for Python >= 3.4 \
51cf4a
# and it was merged back to stdlib in Python >= 3.9 \
51cf4a
if [ "$python_version" -ge 39 ]; then \
51cf4a
py39_byte_compile "%1" "%2"; \
51cf4a
elif [ "$python_version" -ge 34 ]; then \
51cf4a
py3_byte_compile "%1" "%2"; \
51cf4a
else \
51cf4a
py2_byte_compile "%1" "%2"; \
51cf4a
fi