Use --hardlink-dupes for Python 3.4+ as well

This commit is contained in:
Petr Viktorin 2021-09-10 15:34:10 +02:00
parent 37bf640f37
commit dd8caa5aa3
3 changed files with 34 additions and 38 deletions

View File

@ -26,12 +26,13 @@ fi
# This function now implements Python byte-compilation in three different ways: # This function now implements Python byte-compilation in three different ways:
# Python >= 3.4 and < 3.9 uses a new module compileall2 - https://github.com/fedora-python/compileall2 # Python >= 3.4 and < 3.9 uses a new module compileall2 - https://github.com/fedora-python/compileall2
# In Python >= 3.9, compileall2 was merged back to standard library (compileall) so we can use it directly again.
# Python < 3.4 (inc. Python 2) uses compileall module from stdlib with some hacks # Python < 3.4 (inc. Python 2) uses compileall module from stdlib with some hacks
# When we drop support for Python 2, we'd be able to use all compileall2 features like: # When we drop support for Python 2, we'd be able to use all compileall2 features like:
# - -s and -p options to manipulate with a path baked into pyc files instead of $real_libdir # - -s and -p options to manipulate with a path baked into pyc files instead of $real_libdir
# - removed useless $depth - both compileall and compileall2 are limited by sys.getrecursionlimit() # - removed useless $depth - both compileall and compileall2 are limited by sys.getrecursionlimit()
# These changes will make this script much simpler # These changes will make this script much simpler
# In Python >= 3.9, compileall2 was merged back to standard library (compileall) so we can use it directly again.
function python_bytecompile() function python_bytecompile()
{ {
local options=$1 local options=$1
@ -41,45 +42,40 @@ function python_bytecompile()
local depth=$5 # Not used for Python >= 3.4 local depth=$5 # Not used for Python >= 3.4
local real_libdir=$6 # Not used for Python >= 3.4 local real_libdir=$6 # Not used for Python >= 3.4
python_version=$($python_binary -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))") python_version=$($python_binary -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
# #
# Python 3.9 and higher # Python 3.4 and higher
# #
if [ "$python_version" -ge 39 ]; then if [ "$python_version" -ge 34 ]; then
# For Python 3.9+, we compile all opt levels in one go: only # We compile all opt levels in one go: only when $options is empty.
# when $options is empty. if [ -n "$options" ]; then
if [ -n "$options" ]; then return
return fi
fi
[ ! -z $exclude ] && exclude="-x '$exclude'" if [ "$python_version" -ge 39 ]; then
# -q disables verbose output # For Pyhon 3.9+, use the standard library
# -f forces the process to overwrite existing compiled files compileall_module=compileall
# -x excludes paths defined by regex else
# -e excludes symbolic links pointing outside the build root # For older Pythons, use compileall2
# -x and -e together implements the same functionality as the Filter class below compileall_module=compileall2
# -s strips $RPM_BUILD_ROOT from the path fi
# -p prepends the leading slash to the path to make it absolute
$python_binary -B -m compileall -o 0 -o 1 -q -f $exclude -s "$RPM_BUILD_ROOT" -p / --hardlink-dupes -e "$RPM_BUILD_ROOT" "$python_libdir"
# [ ! -z $exclude ] && exclude="-x '$exclude'"
# Python 3.4 and higher
#
elif [ "$python_version" -ge 34 ]; then
[ ! -z $exclude ] && exclude="-x '$exclude'" # PYTHONPATH is needed for compileall2, but doesn't hurt for the stdlib
# /usr/lib/rpm/redhat/ contains compileall2 Python module # -o 0 -o 1 are the optimization levels
# -q disables verbose output # -q disables verbose output
# -f forces the process to overwrite existing compiled files # -f forces the process to overwrite existing compiled files
# -x excludes paths defined by regex # -x excludes paths defined by regex
# -e excludes symbolic links pointing outside the build root # -e excludes symbolic links pointing outside the build root
# -x and -e together implements the same functionality as the Filter class below # -x and -e together implements the same functionality as the Filter class below
# -s strips $RPM_BUILD_ROOT from the path # -s strips $RPM_BUILD_ROOT from the path
# -p prepends the leading slash to the path to make it absolute # -p prepends the leading slash to the path to make it absolute
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" PYTHONPATH=/usr/lib/rpm/redhat/ $python_binary -B -m $compileall_module -o 0 -o 1 -q -f $exclude -s "$RPM_BUILD_ROOT" -p / --hardlink-dupes -e "$RPM_BUILD_ROOT" "$python_libdir"
else
else
# #
# Python 3.3 and lower (incl. Python 2) # Python 3.3 and lower (incl. Python 2)
# #
@ -138,7 +134,7 @@ do
fi fi
# Generate optimized (.pyo) byte-compiled files. # Generate optimized (.pyo) byte-compiled files.
# N.B. For Python 3.9+, this call does nothing # N.B. For Python 3.4+, this call does nothing
python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir" python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
# One or more of the files had a syntax error # One or more of the files had a syntax error

View File

@ -28,7 +28,7 @@ py2_byte_compile () {\
py3_byte_compile () {\ py3_byte_compile () {\
python_binary="env PYTHONHASHSEED=0 %1"\ python_binary="env PYTHONHASHSEED=0 %1"\
bytecode_compilation_path="%2"\ bytecode_compilation_path="%2"\
PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m compileall2 -o 0 -o 1 -s $RPM_BUILD_ROOT -p / $bytecode_compilation_path \ PYTHONPATH="%{_rpmconfigdir}/redhat" $python_binary -s -B -m compileall2 -o 0 -o 1 -s $RPM_BUILD_ROOT -p / --hardlink-dupes $bytecode_compilation_path \
}\ }\
\ \
py39_byte_compile () {\ py39_byte_compile () {\

View File

@ -143,7 +143,7 @@ install -m 755 brp-* %{buildroot}%{_rpmconfigdir}/redhat/
%changelog %changelog
* Thu Sep 09 2021 Petr Viktorin <pviktori@redhat.com> - 3.10-8 * Thu Sep 09 2021 Petr Viktorin <pviktori@redhat.com> - 3.10-8
- Use --hardlink-dupes in %%py_byte_compile and brp-python-bytecompile - Use --hardlink-dupes in %%py_byte_compile and brp-python-bytecompile
(for Python 3.9+) (for Python 3)
- Resolves: rhbz#1977895 - Resolves: rhbz#1977895
* Fri Jul 23 2021 Fedora Release Engineering <releng@fedoraproject.org> - 3.9-7 * Fri Jul 23 2021 Fedora Release Engineering <releng@fedoraproject.org> - 3.9-7