I wanted to learn how to build an RPM package out of a Python module so, now that I’m playing a bit with OpenStack, I decided to pick up a log merger for OpenStack files and build the corresponding package on my Fedora 24.

First thing is to setup the distribution with the right packages:

1[root@localhost ~]$ dnf install @development-tools fedora-packager
1[dani@localhost ~]$ rpmdev-setuptree
2[dani@localhost ~]$ ls rpmbuild/
3BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS

Now, under the SPECS directory, we need to create the spec file which will include the necessary info to build the RPM:

1%global srcname os-log-merger
2%global sum OpenStack Log Merger
3 
4Name:       python-%{srcname}
5Version:    1.0.6
6Release:    1%{?dist}
7Summary:    %{sum}
8 
9License:    Apache
12 
13BuildRoot:      %{_tmppath}/%{srcname}-%{version}-build
14BuildArch:  noarch
15BuildRequires:  python2
16 
17%description
18A tool designed to take a bunch of openstack logs across different projects, and merge them in a single file, ordered by time entries
19 
20%package -n %{srcname}
21Summary:    %{sum}
22%{?python_provide:%python_provide python2-%{srcname}}
23 
24%description -n %{srcname}
25A tool designed to take a bunch of openstack logs across different projects, and merge them in a single file, ordered by time entries
26 
27%prep
28%autosetup -n %{srcname}-%{version}
29 
30%install
31%py2_install
32 
33%check
34%{__python2} setup.py test
35 
36%files -n %{srcname}
37#%license LICENSE
38%doc README.rst
39%{python2_sitelib}/*
40%{_bindir}/os-log-merger
41%{_bindir}/oslogmerger
42%{_bindir}/netprobe
43 
44%changelog
45* Tue Jul 19 2016 dani - 1.0.6-1
46- First version of the os-log-merger-package

Once the file is created, it’s time to build the RPM package:

1[dani@localhost SPECS]$ rpmbuild -bb os-log-merger.spec
2....
3+ umask 022
4+ cd /home/dani/rpmbuild/BUILD
5+ cd os-log-merger-1.0.6
6+ /usr/bin/rm -rf /home/dani/rpmbuild/BUILDROOT/python-os-log-merger-1.0.6-1.fc24.x86_64
7+ exit 0
8[dani@localhost SPECS]$ ls -alh ../RPMS/noarch/
9total 44K
10drwxr-xr-x. 2 dani dani 4,0K jul 19 20:35 .
11drwxr-xr-x. 3 dani dani 4,0K jul 19 20:35 ..
12-rw-rw-r--. 1 dani dani  34K jul 19 20:47 os-log-merger-1.0.6-1.fc24.noarch.rpm

We can see that the rpmbuild command produced the RPM file inside ~/rpmbuild/RPMS/noarch. Let’s pull the info from it and check whether it’s correct:

1[dani@localhost SPECS]$ rpm -qip ../RPMS/noarch/os-log-merger-1.0.6-1.fc24.noarch.rpm
2Name        : os-log-merger
3Version     : 1.0.6
4Release     : 1.fc24
5Architecture: noarch
6Install Date: (not installed)
7Group       : Unspecified
8Size        : 85356
9License     : Apache
10Signature   : (none)
11Source RPM  : python-os-log-merger-1.0.6-1.fc24.src.rpm
12Build Date  : mar 19 jul 2016 20:47:42 CEST
13Build Host  : localhost
14Relocations : (not relocatable)
16Summary     : OpenStack Log Merger
17Description :
18A tool designed to take a bunch of openstack logs across different projects, and merge them in a single file, ordered by time entries

The last step is trying to install the actual file and execute the module to see if everything went fine:

1[root@localhost noarch]$ rpm -qa | grep os-log-merger
2[root@localhost noarch]$ rpm -i os-log-merger-1.0.6-1.fc24.noarch.rpm
3[root@localhost noarch]$ oslogmerger
4usage: oslogmerger [-h] [-v] [--log-base  LOG_BASE]
5                   [--log-postfix  LOG_POSTFIX] [--alias-level ALIAS_LEVEL]
6                   [--min-memory] [--msg-logs file[:ALIAS] [file[:ALIAS] ...]]
7                   [--timestamp-logs file[:ALIAS] [file[:ALIAS] ...]]
8                   log_file[:ALIAS] [log_file[:ALIAS] ...]

References:
https://fedoraproject.org/wiki/How_to_create_a_GNU_Hello_RPM_package
https://fedoraproject.org/wiki/Packaging:Python