diff --git a/brp-python-bytecompile b/brp-python-bytecompile index cbc5830..7e20608 100644 --- a/brp-python-bytecompile +++ b/brp-python-bytecompile @@ -26,12 +26,13 @@ fi # 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 +# 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 + # 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 # - removed useless $depth - both compileall and compileall2 are limited by sys.getrecursionlimit() # 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() { local options=$1 @@ -41,45 +42,40 @@ function python_bytecompile() local depth=$5 # 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 - # - if [ "$python_version" -ge 39 ]; then + # + # Python 3.4 and higher + # + if [ "$python_version" -ge 34 ]; then - # For Python 3.9+, we compile all opt levels in one go: only - # when $options is empty. - if [ -n "$options" ]; then - return - fi + # We compile all opt levels in one go: only when $options is empty. + if [ -n "$options" ]; then + return + fi - [ ! -z $exclude ] && exclude="-x '$exclude'" - # -q disables verbose output - # -f forces the process to overwrite existing compiled files - # -x excludes paths defined by regex - # -e excludes symbolic links pointing outside the build root - # -x and -e together implements the same functionality as the Filter class below - # -s strips $RPM_BUILD_ROOT from the path - # -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" + if [ "$python_version" -ge 39 ]; then + # For Pyhon 3.9+, use the standard library + compileall_module=compileall + else + # For older Pythons, use compileall2 + compileall_module=compileall2 + fi - # - # Python 3.4 and higher - # - elif [ "$python_version" -ge 34 ]; then + [ ! -z $exclude ] && exclude="-x '$exclude'" - [ ! -z $exclude ] && exclude="-x '$exclude'" - # /usr/lib/rpm/redhat/ contains compileall2 Python module - # -q disables verbose output - # -f forces the process to overwrite existing compiled files - # -x excludes paths defined by regex - # -e excludes symbolic links pointing outside the build root - # -x and -e together implements the same functionality as the Filter class below - # -s strips $RPM_BUILD_ROOT from the path - # -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" - else + # PYTHONPATH is needed for compileall2, but doesn't hurt for the stdlib + # -o 0 -o 1 are the optimization levels + # -q disables verbose output + # -f forces the process to overwrite existing compiled files + # -x excludes paths defined by regex + # -e excludes symbolic links pointing outside the build root + # -x and -e together implements the same functionality as the Filter class below + # -s strips $RPM_BUILD_ROOT from the path + # -p prepends the leading slash to the path to make it absolute + 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 # # Python 3.3 and lower (incl. Python 2) # @@ -138,7 +134,7 @@ do fi # 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" if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then # One or more of the files had a syntax error diff --git a/macros.pybytecompile b/macros.pybytecompile index 6c89c8b..9dabee0 100644 --- a/macros.pybytecompile +++ b/macros.pybytecompile @@ -28,7 +28,7 @@ py2_byte_compile () {\ py3_byte_compile () {\ python_binary="env PYTHONHASHSEED=0 %1"\ 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 () {\ diff --git a/python-rpm-macros.spec b/python-rpm-macros.spec index e39b07f..624a66e 100644 --- a/python-rpm-macros.spec +++ b/python-rpm-macros.spec @@ -143,7 +143,7 @@ install -m 755 brp-* %{buildroot}%{_rpmconfigdir}/redhat/ %changelog * Thu Sep 09 2021 Petr Viktorin - 3.10-8 - Use --hardlink-dupes in %%py_byte_compile and brp-python-bytecompile - (for Python 3.9+) + (for Python 3) - Resolves: rhbz#1977895 * Fri Jul 23 2021 Fedora Release Engineering - 3.9-7