2010-01-28 19:09:18 +00:00
|
|
|
# Note that the path could itself be a python file, or a directory
|
|
|
|
|
2019-07-11 12:24:39 +00:00
|
|
|
# Note that the py_byte_compile macro should work for all Python versions
|
|
|
|
# Which unfortunately makes the definition more complicated than it should be
|
2010-01-28 19:09:18 +00:00
|
|
|
|
2018-08-15 16:26:37 +00:00
|
|
|
# Usage:
|
|
|
|
# %py_byte_compile <interpereter> <path>
|
|
|
|
# Example:
|
|
|
|
# %py_byte_compile %{__python3} %{buildroot}%{_datadir}/spam/plugins/
|
|
|
|
|
|
|
|
# This will terminate build on SyntaxErrors, if you want to avoid that,
|
|
|
|
# use it in a subshell like this:
|
|
|
|
# (%{py_byte_compile <interpereter> <path>}) || :
|
2018-01-18 15:53:05 +00:00
|
|
|
|
2020-07-23 10:23:49 +00:00
|
|
|
# Setting PYTHONHASHSEED=0 disables Python hash seed randomization
|
|
|
|
# This should help with byte-compilation reproducibility: https://bugzilla.redhat.com/show_bug.cgi?id=1686078
|
|
|
|
|
2010-01-28 19:09:18 +00:00
|
|
|
%py_byte_compile()\
|
2019-07-11 12:24:39 +00:00
|
|
|
py2_byte_compile () {\
|
2020-07-23 10:23:49 +00:00
|
|
|
python_binary="env PYTHONHASHSEED=0 %1"\
|
2019-07-11 12:24:39 +00:00
|
|
|
bytecode_compilation_path="%2"\
|
|
|
|
failure=0\
|
2019-07-11 12:40:51 +00:00
|
|
|
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\
|
|
|
|
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\
|
2019-07-11 12:24:39 +00:00
|
|
|
test $failure -eq 0\
|
|
|
|
}\
|
|
|
|
\
|
|
|
|
py3_byte_compile () {\
|
2020-07-23 10:23:49 +00:00
|
|
|
python_binary="env PYTHONHASHSEED=0 %1"\
|
2019-07-11 12:24:39 +00:00
|
|
|
bytecode_compilation_path="%2"\
|
2019-07-11 12:40:51 +00:00
|
|
|
PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m compileall2 -o 0 -o 1 -s $RPM_BUILD_ROOT -p / $bytecode_compilation_path \
|
2019-07-11 12:24:39 +00:00
|
|
|
}\
|
|
|
|
\
|
2020-06-05 06:52:58 +00:00
|
|
|
py39_byte_compile () {\
|
2020-07-23 10:23:49 +00:00
|
|
|
python_binary="env PYTHONHASHSEED=0 %1"\
|
2020-06-05 06:52:58 +00:00
|
|
|
bytecode_compilation_path="%2"\
|
|
|
|
$python_binary -s -B -m compileall -o 0 -o 1 -s $RPM_BUILD_ROOT -p / $bytecode_compilation_path \
|
|
|
|
}\
|
|
|
|
\
|
2019-07-11 12:42:28 +00:00
|
|
|
# Path to intepreter should not contain any arguments \
|
|
|
|
[[ "%1" =~ " -" ]] && echo "ERROR py_byte_compile: Path to interpreter should not contain any arguments" >&2 && exit 1 \
|
2019-07-11 12:24:39 +00:00
|
|
|
# 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))") \
|
2020-06-05 06:52:58 +00:00
|
|
|
# compileall2 is an enhanced fork of stdlib compileall module for Python >= 3.4 \
|
|
|
|
# and it was merged back to stdlib in Python >= 3.9 \
|
|
|
|
if [ "$python_version" -ge 39 ]; then \
|
|
|
|
py39_byte_compile "%1" "%2"; \
|
|
|
|
elif [ "$python_version" -ge 34 ]; then \
|
|
|
|
py3_byte_compile "%1" "%2"; \
|
|
|
|
else \
|
|
|
|
py2_byte_compile "%1" "%2"; \
|
|
|
|
fi
|