Building CentOS RPM packages without Python Setuptools

There are some compelling reasons to use setuptools with python: it is used by pypi and it manages building C extensions (or cython and the ilk). However, if you are in a position of integrating python scripts in polyglot environment setuptools has some compelling disadvantages: its dependency resolution is terrible and it can't express dependencies on non-python packages.

As described in Part 1 you can have setuptools build RPM packages for you. In this, Part 2, I will describe using rpmbuild to drive the packaging of setuptools packaged python tools. Then in Part 3 I will describe a de novo packaging of python code via RPM without using setuptools.

rpmbuild and setuptools

As an example, I will show a packaging of BioPython.

Install rpmbuild

sudo yum install rpmbuild -y

Create a working directory for use by rpmbuild

mkdir -p ~/rpmbuild/{SPECS,SOURCES}

Manually copy the source file for biopython to the correct location

cd ~/rpmbuild/SOURCES
wget http://biopython.org/DIST/biopython-1.68.tar.gz

Build a spec file

Name:           python-BioPython
Version:        1.68
Release:        1%{?dist}
Summary:        A Python module for accessing cpio archives

Group:          Development/Languages
License:        LGPLv2+
URL:            http://biopython.org/
Source0:        http://biopython.org/DIST/biopython-1.68.tar.gz
# Note, even though a URL is specified here rpmbuild _does_ not
# fetch anythin for you
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

BuildRequires:  python2-devel, gcc

%description
Biopython is a set of freely available tools for biological computation written in Python by an international team of developers.

%prep
%setup -q -n biopython-%{version}
# %setup is a maacro used by RPM to handle unpacking the source
# archive. -n is used to describe what directory the tarball
# will expand into.

%build
CFLAGS="$RPM_OPT_FLAGS" %{__python} setup.py build
# Use setuptools to build the package.

%install
rm -rf $RPM_BUILD_ROOT
%{__python} setup.py install --skip-build --root $RPM_BUILD_ROOT
# Note the use of --root to have python install into a new directory
# tree.

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
%doc README LICENSE NEWS Doc/*
# %doc are specified relative to the directory where the software is built

%{python_sitearch}/Bio/*
%{python_sitearch}/BioSQL*
%{python_sitearch}/*.egg-info
# %{python_sitearch} expands to the site-packages directory where
# things were installed to.

Build the rpm

cd ~/rpmbuild/SPECS
rpmbuild -ba 

Install the rpm (the exact path may differ slightly on your system)

yum install ~/rpmbuild/RPMS/x86-64/python-BioPython-1.68-1.el7.centos.x86_64.rpm

social