Blame macros.pybytecompile

7391367
# Note that the path could itself be a python file, or a directory
7391367
76681ad
# Note that the py_byte_compile macro should work for all Python versions
76681ad
# Which unfortunately makes the definition more complicated than it should be
7391367
8f067ff
# Usage:
03a1e3b
#    %%py_byte_compile <interpereter> <path>
8f067ff
# Example:
03a1e3b
#    %%py_byte_compile %%{__python3} %%{buildroot}%%{_datadir}/spam/plugins/
8f067ff
8f067ff
# This will terminate build on SyntaxErrors, if you want to avoid that,
8f067ff
# use it in a subshell like this:
03a1e3b
#    (%%{py_byte_compile <interpereter> <path>}) || :
438faf7
0eae1d9
# Setting PYTHONHASHSEED=0 disables Python hash seed randomization
0eae1d9
# This should help with byte-compilation reproducibility: https://bugzilla.redhat.com/show_bug.cgi?id=1686078
cfa45df
# Python 3.11+ no longer needs this: https://github.com/python/cpython/pull/27926 (but we support older Pythons as well)
0eae1d9
7391367
%py_byte_compile()\
77912c7
clamp_source_mtime () {\
77912c7
    python_binary="%{__env_unset_source_date_epoch_if_not_clamp_mtime} %1"\
77912c7
    bytecode_compilation_path="%2"\
77912c7
    PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m clamp_source_mtime $bytecode_compilation_path \
77912c7
}\
77912c7
\
76681ad
py2_byte_compile () {\
eb7a4fd
    python_binary="%{__env_unset_source_date_epoch_if_not_clamp_mtime} PYTHONHASHSEED=0 %1"\
76681ad
    bytecode_compilation_path="%2"\
76681ad
    failure=0\
4493789
    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\
4493789
    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\
76681ad
    test $failure -eq 0\
76681ad
}\
76681ad
\
e4baf5a
py34_byte_compile () {\
eb7a4fd
    python_binary="%{__env_unset_source_date_epoch_if_not_clamp_mtime} PYTHONHASHSEED=0 %1"\
76681ad
    bytecode_compilation_path="%2"\
10e0e53
    PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m compileall2 %{?_smp_build_ncpus:-j%{_smp_build_ncpus}} -o 0 -o 1 -s $RPM_BUILD_ROOT -p / --hardlink-dupes $bytecode_compilation_path \
76681ad
}\
e4baf5a
py37_byte_compile () {\
e4baf5a
    python_binary="%{__env_unset_source_date_epoch_if_not_clamp_mtime} PYTHONHASHSEED=0 %1"\
e4baf5a
    bytecode_compilation_path="%2"\
10e0e53
    PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m compileall2 %{?_smp_build_ncpus:-j%{_smp_build_ncpus}} -o 0 -o 1 -s $RPM_BUILD_ROOT -p / --hardlink-dupes --invalidation-mode=timestamp $bytecode_compilation_path \
e4baf5a
}\
76681ad
\
3a211cc
py39_byte_compile () {\
eb7a4fd
    python_binary="%{__env_unset_source_date_epoch_if_not_clamp_mtime} PYTHONHASHSEED=0 %1"\
3a211cc
    bytecode_compilation_path="%2"\
10e0e53
    $python_binary -s -B -m compileall %{?_smp_build_ncpus:-j%{_smp_build_ncpus}} -o 0 -o 1 -s $RPM_BUILD_ROOT -p / --hardlink-dupes --invalidation-mode=timestamp $bytecode_compilation_path \
3a211cc
}\
3a211cc
\
eb32743
# Path to intepreter should not contain any arguments \
eb32743
[[ "%1" =~ " -" ]] && echo "ERROR py_byte_compile: Path to interpreter should not contain any arguments" >&2 && exit 1 \
77912c7
# First, clamp source mtime https://fedoraproject.org/wiki/Changes/ReproducibleBuildsClampMtimes \
77912c7
clamp_source_mtime "%1" "%2"; \
76681ad
# Get version without a dot (36 instead of 3.6), bash doesn't compare floats well \
76681ad
python_version=$(%1 -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))") \
3a211cc
# compileall2 is an enhanced fork of stdlib compileall module for Python >= 3.4 \
3a211cc
# and it was merged back to stdlib in Python >= 3.9 \
e4baf5a
# Only Python 3.7+ supports and needs the --invalidation-mode option \
3a211cc
if [ "$python_version" -ge 39 ]; then \
3a211cc
py39_byte_compile "%1" "%2"; \
e4baf5a
elif [ "$python_version" -ge 37 ]; then \
e4baf5a
py37_byte_compile "%1" "%2"; \
3a211cc
elif [ "$python_version" -ge 34 ]; then \
e4baf5a
py34_byte_compile "%1" "%2"; \
3a211cc
else \
3a211cc
py2_byte_compile "%1" "%2"; \
3a211cc
fi