From bbe15a98ff1a40caeecb48cfd50ca5eb8c1a396b Mon Sep 17 00:00:00 2001 From: Miro Hrončok Date: Nov 15 2018 11:26:53 +0000 Subject: [PATCH 1/2] Fix syntax in Python Appendix: Manual byte compilation --- diff --git a/guidelines/modules/ROOT/pages/Python_Appendix.adoc b/guidelines/modules/ROOT/pages/Python_Appendix.adoc index 5bd388e..cab68d5 100644 --- a/guidelines/modules/ROOT/pages/Python_Appendix.adoc +++ b/guidelines/modules/ROOT/pages/Python_Appendix.adoc @@ -257,21 +257,23 @@ If you have `+*.py+` files outside of the `+/usr/lib(64)?/pythonX.Y/+` directori Example for package that has both Python versions: -`+# Disable automatic compilation of Python files in extra directories+` + -`+%global _python_bytecompile_extra 0+` + - + -`+# Buildrequire both python2 and python3+` + -`+BuildRequires: python2-devel python3-devel+` + - + -`+%install+` + -`+# Installs a python2 private module into %{buildroot}%{_datadir}/mypackage/foo+` + -`+# and installs a python3 private module into %{buildroot}%{_datadir}/mypackage/bar+` + -`+make install DESTDIR=%{buildroot}+` + - + -`+# Manually invoke the python byte compile macro for each path that needs byte+` + -`+# compilation.+` + -`+%py_byte_compile %{__python2} %{buildroot}%{_datadir}/mypackage/foo+` + -`+%py_byte_compile %{__python3} %{buildroot}%{_datadir}/mypackage/bar+` +.... +# Disable automatic compilation of Python files in extra directories +%global _python_bytecompile_extra 0 + +# Buildrequire both python2 and python3 +BuildRequires: python2-devel python3-devel + +%install +# Installs a python2 private module into %{buildroot}%{_datadir}/mypackage/foo +# and installs a python3 private module into %{buildroot}%{_datadir}/mypackage/bar +make install DESTDIR=%{buildroot} + +# Manually invoke the python byte compile macro for each path that needs byte +# compilation. +%py_byte_compile %{__python2} %{buildroot}%{_datadir}/mypackage/foo +%py_byte_compile %{__python3} %{buildroot}%{_datadir}/mypackage/bar +.... Note that this *does not* disable the compilation of files in `+/usr/lib(64)?/pythonX.Y/+`. From 647454105e873c15ce0c5e10cfad37aae8a50225 Mon Sep 17 00:00:00 2001 From: Miro Hrončok Date: Nov 15 2018 23:15:04 +0000 Subject: [PATCH 2/2] Update Python manual byte compilation rules See https://fedoraproject.org/wiki/Changes/No_more_automagic_Python_bytecompilation_phase_2 Issue: https://pagure.io/packaging-committee/issue/830 --- diff --git a/guidelines/modules/ROOT/pages/Python_Appendix.adoc b/guidelines/modules/ROOT/pages/Python_Appendix.adoc index cab68d5..4b496b0 100644 --- a/guidelines/modules/ROOT/pages/Python_Appendix.adoc +++ b/guidelines/modules/ROOT/pages/Python_Appendix.adoc @@ -248,19 +248,25 @@ You should usually follow upstream's directions on how to run `+2to3+` and build When byte compiling a .py file, python embeds a magic number in the byte compiled files that correspond to the runtime. Files in `+%{python?_sitelib}+` and `+%{python?_sitearch}+` MUST correspond to the runtime for which they were built. For instance, a pure python module compiled for the 3.4 runtime MUST be below `+%{_usr}/lib/python3.4/site-packages+` -The `+brp-python-bytecompile+` script tries to figure this out for you. The script determines which interpreter to use when byte compiling the module by following these steps: - -1. What directory is the module installed in? If it's `+/usr/lib{,64}/pythonX.Y+`, then `+pythonX.Y+` is used to byte compile the module. If `+pythonX.Y+` is not installed, then an error is returned and the rpm build process will exit on an error so remember to `+BuildRequire+` the proper python package. -2. By default the script interpreter defined in `+%{__python}+` is used to compile the modules outside of those directories. This defaults to `+/usr/bin/python+` (that's Python 2.7 on Fedora). This only happens for backwards compatibility reasons. - -If you have `+*.py+` files outside of the `+/usr/lib(64)?/pythonX.Y/+` directories, you *MUST* disable their automatic compilation. If you require those files to be byte compiled (e.g. it's an importable Python module) you can then compile them explicitly using the `+%py_byte_compile+` macro. +The `+brp-python-bytecompile+` script tries to figure this out for you. +The script determines which interpreter to use when byte compiling the module +by checking what directory the file is installed in. +If it's `+/usr/lib{,64}/pythonX.Y+`, +then `+pythonX.Y+` is used to byte compile the module. +If `+pythonX.Y+` is not installed, +then an error is returned and the rpm build process will exit on an error +so remember to `+BuildRequire+` the proper python package. + +If you have `+*.py+` files outside of the `+/usr/lib(64)?/pythonX.Y/+` directories +and you require those files to be byte compiled +(e.g. it's an importable Python module) +you MUST compile them explicitly using the `+%py_byte_compile+` macro. +Note that not all Python files are importable Python modules; +when in doubt, grep the sources for the appropriate import statement. Example for package that has both Python versions: .... -# Disable automatic compilation of Python files in extra directories -%global _python_bytecompile_extra 0 - # Buildrequire both python2 and python3 BuildRequires: python2-devel python3-devel @@ -275,11 +281,15 @@ make install DESTDIR=%{buildroot} %py_byte_compile %{__python3} %{buildroot}%{_datadir}/mypackage/bar .... -Note that this *does not* disable the compilation of files in `+/usr/lib(64)?/pythonX.Y/+`. +The `+%py_byte_compile+` macro takes two arguments. +The first is the python interpreter to use for byte compiling. +The second is a file or directory to byte compile. +If the second argument is a directory, +the macro will recursively byte compile any *.py file in the directory. -The `+%py_byte_compile+` macro takes two arguments. The first is the python interpreter to use for byte compiling. The second is a file or directory to byte compile. If the second argument is a directory, the macro will recursively byte compile any *.py file in the directory. +NOTE: You can opt-in for this behavior on Fedora 29 by setting `+%global _python_bytecompile_extra 0+` in the spec. -== Manual byte compilation for Fedora 28 or earlier (including EPEL) +== Manual byte compilation for Fedora 29 or earlier (including EPEL) The script interpreter defined in `+%{__python}+` is used to compile the modules outside of `+/usr/lib(64)?/pythonX.Y/+` directories. This defaults to `+/usr/bin/python+` (that's Python 2.7 on Fedora). If you need to compile the modules for python3, set it to `+/usr/bin/python3+` instead: