From Fedora Project Wiki
No edit summary
No edit summary
Line 35: Line 35:
BuildArch:      noarch
BuildArch:      noarch


# This will be python3-devel or python2-devel:
# python3-devel or python2-devel
BuildRequires:  %{python_default_devel}
BuildRequires:  %{python_default_devel}


# This will be python-setuptools or python3-setuptools
# python-setuptools or python3-setuptools
BuildRequires:  %{python_default_prefix}setuptools
BuildRequires:  %{python_default_prefix}setuptools


# This will be python3-pytest or pytest
# python3-pytest or pytest
BuildRequires:  %{python_default_prefix_only3}pytest
BuildRequires:  %{python_default_prefix_only3}pytest


# Unfortunately with weird package names, we have to do if/else
# Unfortunately with weird package names, we have to do if/else
%if %{python_default_version} > 2
%if %{python_default_version} > 2
BuildRequires:  python3-cairo
BuildRequires:  python3-cairo
Requires:      python3-cairo
Requires:      python3-cairo
%else
%else
BuildRequires:  pycairo
BuildRequires:  pycairo
Requires:      pycairo
Requires:      pycairo
%endif
%endif


Line 58: Line 63:
%prep
%prep
%setup -q
%setup -q
# change the shebangs if necessary
 
# you might also use a different regex depending on the project
# Change the shebangs if necessary
# You might need to use a different regex depending on the project
find -name '*.py' | xargs sed -i '1s|^#!python|#!%{__python_default}|'
find -name '*.py' | xargs sed -i '1s|^#!python|#!%{__python_default}|'


%build
%build
# this will use the default Python interpreter
 
# Use the default Python interpreter
%{__python_default} setup.py build
%{__python_default} setup.py build


%install
%install
# this will use the default Python interpreter
 
# Use the default Python interpreter
%{__python_default} setup.py install --skip-build --root %{buildroot}
%{__python_default} setup.py install --skip-build --root %{buildroot}


Line 73: Line 81:
%doc README.rst LICENSE
%doc README.rst LICENSE
%{_bindir}/%{name}
%{_bindir}/%{name}
# Files will end up in proper locations
# Files will end up in proper locations
%{python_default_sitelib}/%{name}
%{python_default_sitelib}/%{name}
Line 81: Line 90:
</pre>
</pre>


This package will use Python 3 (or 2) on Fedora releases where Python 3 (or 2) is the default.
This package will use Python 3 (or 2) on Fedora releases where Python 3 (or 2) is the default without unnecessary if/else bloat. Useful when you build RPMs for multiple targets from one spec - for example on Copr.

Revision as of 19:56, 17 October 2014

Default Python version macros

For situations when an application (i.e. not a library) is compatible with both Python 3 and Python 2, the default Python version should be used. Depending on Fedora release number the default Python version might differ. For packagers' convenience and better spec readability, the following set of macros is provided:

Macro Fedora >= 22 Fedora <= 21
python_default_version 3 2
__python_default %{__python3} %{__python2}
python_default_sitelib %{python3_sitelib} %{python2_sitelib}
python_default_sitearch %{python3_sitearch} %{python2_sitearch}
python_default_devel python3-devel python2-devel
python_default_prefix python3- python-
python_default_prefix_only3 python3- (empty string)

Note: The Fedora release since when Python 3 is the default might change.

Example spec file using this approach:

Name:           fooapp
Version:        1.0.0
Release:        1%{?dist}
Summary:        This is just an example
License:        MIT
URL:            http://example.com/
Source0:        http://pypi.python.org/packages/source/f/%{name}/%{name}-%{version}.tar.gz
BuildArch:      noarch

# python3-devel or python2-devel
BuildRequires:  %{python_default_devel}

# python-setuptools or python3-setuptools
BuildRequires:  %{python_default_prefix}setuptools

# python3-pytest or pytest
BuildRequires:  %{python_default_prefix_only3}pytest

# Unfortunately with weird package names, we have to do if/else

%if %{python_default_version} > 2

BuildRequires:  python3-cairo
Requires:       python3-cairo

%else

BuildRequires:  pycairo
Requires:       pycairo

%endif

%description
This is just an example

%prep
%setup -q

# Change the shebangs if necessary
# You might need to use a different regex depending on the project
find -name '*.py' | xargs sed -i '1s|^#!python|#!%{__python_default}|'

%build

# Use the default Python interpreter
%{__python_default} setup.py build

%install

# Use the default Python interpreter
%{__python_default} setup.py install --skip-build --root %{buildroot}

%files
%doc README.rst LICENSE
%{_bindir}/%{name}

# Files will end up in proper locations
%{python_default_sitelib}/%{name}
%{python_default_sitelib}/%{name}-%{version}-py?.?.egg-info

%changelog
...

This package will use Python 3 (or 2) on Fedora releases where Python 3 (or 2) is the default without unnecessary if/else bloat. Useful when you build RPMs for multiple targets from one spec - for example on Copr.