Debian packaging for python2 and python3 at the same time

The problem

The scenario was like this:

  • Python code that provides a library and a binary
  • The code is compatible with both Python v2 and v3

The requirements were:

  • Generate a package with the library part for python v2
  • Generate a package with the library part for python v3
  • Generate a binary package with the executable for python v3

I.e, from one source package (vdns) I wanted to create python-vdns (python2 lib), python3-vdns (python3 lib) and vdns (executable).

The approach

After trying other methods, I ended up using debhelper 9 and pybuild. Before that I tried using CDBS but had no luck there.

With DH9 it’s easy to package a python library for multiple python versions as it handles everything itself. The only catch was the package that contained the binaries and how to make it use the v3 version instead of the v2

The solution

The solution was this rules file:

#!/usr/bin/make -f

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# Don't set this or else the .install files will fail
#export PYBUILD_NAME = vdns
#export PYBUILD_SYSTEM = custom

# Otherwise the usr/bin/vdns.py file is from python2
export PYBUILD_INSTALL_ARGS_python2 = --install-scripts=/dev/null

# main packaging script based on dh7 syntax
%:
        dh $@ --with python3,python2 --buildsystem=pybuild

The control file is like this:

Source: vdns
Section: unknown
Priority: optional
Maintainer: Stefanos Harhalakis <v13@v13.gr>;
Build-Depends: debhelper (>= 9), dh-python,
 python-all (>=2.6.6-3~), python-setuptools,
 python3-all (>=3.2), python3-setuptools
Standards-Version: 3.9.5
X-Python-Version: >= 2.7
X-Python3-Version: >= 3.2
XS-Python-Version: >= 2.7
XS-Python3-Version: >= 3.2

Package: python-vdns
Architecture: all
Depends: ${python:Depends}, ${misc:Depends}, python-psycopg2
Description: vdns python2 libraries
 These libraries allow the reading and the creation of bind zone files

Package: python3-vdns
Architecture: all
Depends: ${python3:Depends}, ${misc:Depends}, python3-psycopg2
Description: vdns python3 libraries
 These libraries allow the reading and the creation of bind zone files

Package: vdns
Architecture: all
Depends: ${python:Depends}, ${misc:Depends}, python3-vdns, python(>=3.2)
Description: Database-based DNS management
 vdns is a database-based DNS management tool. It gets data from its database
 and generates bind zone files. It supports A, AAAA, MX, NS, DS, PTR, CNAME,
 TXT, DNSSEC, SSHFP, DKIM and SRV records
 .
 vdns uses a PostgreSQL database to store the data
 .
 vdns is not a 1-1 mapping of DB<->zone files. Instead the databsae
 is meant to describe the data that are later generated. E.g:
 * DKIM data are used to generate TXT records
 * A, AAAA and PTR entries are generated from the same data
 * NS glue records for sub-zones are auto-created

And the .install files are like this:

python-vdns.install:
usr/lib/python2*/dist-packages/vdns/*.py*
usr/lib/python2*/dist-packages/vdns/src

python3-vdns.install:
usr/lib/python3*/dist-packages/vdns/*.py*
usr/lib/python3*/dist-packages/vdns/src

Explanation

What the above does is to use DH9 with pybuild. Pybuild takes care of multiple versions by “compiling” the binariesĀ  twice under build/scripts-2.x and build/scripts/3.x directories. After that it copies themĀ  to debian/tmp and finally splits the contents of debian/tmp based on the .install files to debian/python-vdns, debian/python3-vdns and debian/vdns.

The biggest problem were the files that were meant for the vdns package as those were present in both build/scripts-2.x and build/scripts/3.x, each one prepared for the appropriate debian version:

-rwxr-xr-x 1 v13 v13 1197 Jul 19 21:12 build/scripts-2.7/vdns.py
-rwxr-xr-x 1 v13 v13 1198 Jul 19 21:12 build/scripts-3.4/vdns.py

The following line in rules takes care of the conflict by skipping a version:

export PYBUILD_INSTALL_ARGS_python2 = --install-scripts=/dev/null

This way, the python2 version never gets installed and thus only the python3 version is available to be copied to debian/tmp. Otherwise the behavior was random (it was picking the first one it was finding).

Other attempts

I also tried using autoconf with CDBS but that proved to be even more difficult.

Acknowledgements

The above was accomplished only because of the help of folks in #debian-python @ OFTC; namely: p1otr, mapreri and jcristau.

2 Comments

  1. You have this stanza repeated twice, I think it should be python2-vdns.install
    python-vdns.install:
    usr/lib/python2*/dist-packages/vdns/*.py*
    usr/lib/python2*/dist-packages/vdns/src

    and python-vdns.install should contain (?)
    usr/bin/*

Leave a Reply

Your email address will not be published. Required fields are marked *