Braindead Python packaging

As you all know, distributing and building packages with the openSUSE Build Service is easy and fun. The only party pooper is that you have to write a spec file to get your RPMs out there. Thanks to darix, we have a decent solution at least for Ruby packages: gem2rpm, a script which auto-generates RPM spec files for Ruby gems. Ever wondered why we don’t have something similar for Python? Well, I did so too. Thus, after a half a week of hackery, I’d like to introduce py2pack, my take on braindead Python packaging. Here’s how it goes:

Lets suppose you want to package zope.interface and you don’t know how it is named exactly or where to download from. First of all, you can search for it and download the source tarball automatically if you found the correct module:


$ py2pack search zope.interface
searching for module zope.interface...
found zope.interface-3.6.1
$ py2pack fetch zope.interface
downloading package zope.interface-3.6.1...
from http://pypi.python.org/packages/source/z/zope.interface/zope.interface-3.6.1.tar.gz

As a next step you may want to generate a package recipe for your distribution. For RPM-based distributions (let’s use openSUSE as an example), you want to generate a spec file named python-zopeinterface.spec:


$ py2pack generate zope.interface -t opensuse.spec -f python-zopeinterface.spec

The above command has the parameter “-t opensuse.spec”, which is also the default (and thus optional). Well, this seems to imply that you can generate other files (and rumor has it that Debian support is on the way, too) :-)

Back to the topic, the source tarball and package recipe is all you need to generate the RPM file. This final step may depend on which distribution you use. Again, for openSUSE (and by using the openSUSE Build Service), the complete recipe becomes:


$ osc mkpac python-zopeinterface
$ cd python-zopeinterface
$ py2pack fetch zope.interface
$ py2pack generate zope.interface -f python-zopeinterface.spec
$ osc build
$ osc vc
$ osc commit

The first line uses osc, the Build Service command line tool to generate a new package (preferrably in your Build Service home project). The py2pack steps are known already. Finally, the package is tested (built locally), a changes (package changelog) file is generated (with ‘osc vc’) and the result is sent back to the Build Service for public consumption. However, depending on the Python module, you may have to adapt the generated spec file slightly. Py2pack is quite clever and tries to auto-generate as much as it can, but it depends on the metadata that the module provides. Thus, bad metadata implies mediocre results. To get further help about py2pack usage, issue the following command:


$ py2pack help

To demonstrate its capabilities, I re-packaged some Python packages that are in devel:languages:python. As you can see (when logged in to the Build Service), those package now build for all RPM-based distros we currently offer in the Build Service. Currently, you can download py2pack from the Python Package Index or install the package python-py2pack from the devel:languages:python repository.

Happy Python packaging!

About these ads

4 thoughts on “Braindead Python packaging

  1. Does this work with python 3 or only python 2? In my experience packaging python 3 is less then optimal, considering the lack of macros for python 3 and the very few python 3 versions of basic python packages available.

  2. To be honest, I haven’t tested it yet. But I’d say not really for the reasons you mentioned already. Nonetheless, feel free to test it, contributions are wamly welcome :-)

  3. Nice! Perl could use the same. Ever try to do the same for Perl modules? There’s CPAN to search/download modules, but no easy way make it an RPM. Unless there’s something I’m missing…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s