#829 Update Python manual byte compilation rules
Merged 8 months ago by tibbs. Opened 8 months ago by churchyard.
churchyard/packaging-committee python_bytecompilation_magic  into  master

@@ -248,36 +248,48 @@ 

  

  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:

+ 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.

  

- 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.

+ Example for package that has both Python versions:

  

- 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.

+ ....

+ # Buildrequire both python2 and python3

+ BuildRequires: python2-devel python3-devel

  

- Example for package that has both Python versions:

+ %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

+ ....

+ 

+ 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.

  

- `+# 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/+`.

- 

- 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.

- 

- == 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:

  

Opened an issue as this needs announcement once merged: https://pagure.io/packaging-committee/issue/830

"what directory is the file installed in" should be "what directory the file is installed in".

2 new commits added

  • Update Python manual byte compilation rules
  • Fix syntax in Python Appendix: Manual byte compilation
8 months ago

Commit 7497e52 fixes this pull-request

Pull-Request has been merged by tibbs

8 months ago

Pull-Request has been merged by tibbs

8 months ago
Metadata