BreakingExpress

Package deal a brand new Python module in 4 steps

When you put in an utility, you are normally putting in a package deal that accommodates the executable code for an utility and essential recordsdata similar to documentation, icons, and so forth. On Linux, purposes are generally packaged as RPM or DEB recordsdata, and customers set up them with the dnf or apt instructions, relying on the Linux distribution. However, new Python modules are launched just about day by day, so you might simply encounter a module that hasn’t but been packaged. And that is precisely why the pyp2rpm command exists.

Recently, I attempted to put in a module referred to as python-concentration. It did not go nicely:

$ sudo dnf set up python-concentration
Updating Subscription Management repositories.
Last metadata expiration verify: 1:23:32 in the past on Sat 11 Jun 2022 06:37:25.
No match for argument: python-concentration
Error: Unable to discover a match: python-concentration

It’s a PyPi package deal, nevertheless it’s not but accessible as an RPM package deal. The excellent news is which you could construct an RPM your self with a comparatively easy course of utilizing pyp2rpm.

You’ll want two directories to get began:

$ mkdir rpmbuild
$ cd rpmbuild && mkdir SPECS

You’ll additionally want to put in pyp2rpm:

$ sudo dnf set up pyp2rpm

1. Generate the spec file

The basis of any RPM package deal is a file referred to as the spec file. This file accommodates all of the details about how one can construct the package deal, which dependencies it wants, the model of the appliance it gives, what recordsdata it installs, and extra. When pointed to a Python module, pyp2rpm generates a spec file for it, which you should use to construct an RPM.

Using python-concentration as an arbitrary instance, this is how one can generate a spec file:

$ pyp2rpm focus > ~/rpmbuild/SPECS/focus.spec

And this is the file it generates:

# Created by pyp2rpm-3.3.8
%international pypi_name focus
%international pypi_version 1.1.5

Name:           python-%{pypi_name}
Version:        %{pypi_version}
Release:        1%{?dist}
Summary:        Get work completed when you have to, goof off while you don't

License:        None
URL:            None
Source0:        %{pypi_source}
BuildArch:      noarch

BuildRequires:  python3-devel
BuildRequires:  python3dist(setuptools)

%description
Concentration [![PyPI version]( [![Test Status]( [![Lint Status]( [![codecov](

%package deal -n     python3-%{pypi_name}
Summary:        %{abstract}
%{?python_provide:%python_provide python3-%{pypi_name}}

Requires:       (python3dist(hug) >= 2.6.1 with python3dist(hug) < 3~~)
Requires:       python3dist(setuptools)
%description -n python3-%{pypi_name}
Concentration [![PyPI version]( [![Test Status]( [![Lint Status]( [![codecov](

%prep
%autosetup -n %{pypi_name}-%{pypi_version}

%construct
%py3_build

%set up
%py3_install

%recordsdata -n python3-%{pypi_name}
%license LICENSE
%doc README.md
%{_bindir}/focus
%{python3_sitelib}/%{pypi_name}
%{python3_sitelib}/%{pypi_name}-%{pypi_version}-py%{python3_version}.egg-info

%changelog
*  - 1.1.5-1
- Initial package deal.

2. Run rpmlint

To be sure that the spec file is as much as requirements, run the rpmlint command on the file:

$ rpmlint ~/rpmbuild/SPEC/focus.spec
error: unhealthy date in %changelog: - 1.1.5-1
0 packages and 1 specfiles checked; 0 errors, 0 warnings.

It appears the changelog entry requires a date.

%changelog
* Sat Jun 11 2022 Tux <tux@instance.com> - 1.1.5-1

Try rpmlint once more:

$ rpmlint ~/rpmbuild/SPEC/focus.spec
0 packages and 1 specfiles checked; 0 errors, 0 warnings.

Success!

3. Download the supply code

To construct an RPM package deal, you will need to obtain the code you are packaging up. The simple means to do that is to parse your spec file to seek out the supply code’s location on the Internet.

First, set up the spectool command with dnf:

$ sudo dnf set up spectool

Then use it to obtain the supply code:

$ cd ~/rpmbuild
$ spectool -g -R SPEC/focus.spec
Downloading: https://recordsdata.pythonhosted.org/...concentration-1.1.5.tar.gz
   6.0 KiB / 6.0 KiB    [=====================================]
Downloaded: concentration-1.1.5.tar.gz

This creates a SOURCES listing and locations the supply code archive into it.

4. Build the supply package deal

Now you could have a legitimate spec file, so it is time to construct the supply package deal with the rpmbuild command. If you do not have rpmbuild but, set up the rpm-build package deal with dnf (or settle for your terminal’s supply to put in that package deal while you try to make use of the rpmbuild command).

$ cd ~/rpmbuild
$ spectool -g -R SPEC/focus.spec
Downloading: https://recordsdata.pythonhosted.org/...concentration-1.1.5.tar.gz
   6.0 KiB / 6.0 KiB    [=====================================]
Downloaded: concentration-1.1.5.tar.gz

The -bs possibility stands for construct supply. This possibility offers you an src.rpm file, an all-purpose package deal that have to be rebuilt for a particular structure.

Build an installable RPM on your system:

$ rpmbuild –rebuild SRPMS/python-concentration-1.1.5-1.el9.src.rpm
error: Failed construct dependencies:
        python3-devel is required by python-concentration-1.1.5-1.el9.noarch

It appears like this package deal requires the event libraries of Python. Install them to proceed with the construct. This time the construct succeeds and renders much more output (which I abbreviate right here for readability):

$ sudo dnf set up python3-devel -y
$ rpmbuild –rebuild SRPMS/python-concentration-1.1.5-1.el9.src.rpm
[...]
Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.TYA7l2
+ umask 022
+ cd /dwelling/bogus/rpmbuild/BUILD
+ rm -rf concentration-1.1.5
+ RPM_EC=0
++ jobs -p
+ exit 0

Your RPM package deal has been constructed within the RPMS subdirectory. Install it as typical with dnf:

$ sudo dnf set up RPMS/noarch/python3-concentration*rpm

Why not simply use PyPi?

It’s not completely essential to make a Python module into an RPM. Installing a module with PyPi can also be acceptable, however PyPi provides one other package deal supervisor to your private record of issues to verify and replace. When you put in an RPM utilizing dnf, you could have an entire itemizing of what you have put in in your system. Thanks to pyp2rpm, the method is fast, simple, and automatable.

Exit mobile version