How can I Debian-package a python application with a systemd unit using stdeb3/py2dsc and dpkg-buildpackage?

2.4k views Asked by At

What I'd like to do is create a Debian package for a Python3 application and have the package install an associated systemd service, and remove/update it when the package is removed or updated.

I have a Python3 program that I'd like to bundle into a Debian package on Ubuntu 18.04. I've been able to do this successfully with a setup.py and the py2dsc program that ships with stdeb3.

$ python setup.py sdist
$ py2dsc dist/mypackage-0.0.1.tar.gz
$ (cd deb_dist/mypackage-0.0.1/ && dpkg-buildpackage -rfakeroot -uc -us)

This creates an installable .deb and apart from a weird py2dsc dependency on Python 3.6 even though I'm using 3.8, it works well.

py2dsc creates a debian staging tree, including debian/rules with:

#!/usr/bin/make -f

export PYBUILD_NAME=mypackage
%:
    dh $@ --with python3 --buildsystem=pybuild

Next I want to have this package install a systemd service Unit that will run the Python3 program. I think the service file needs to be called python3-mypackage.service to match <package-name>.service, because py2dsc prepends python3- to the package name.

I did some research and found that, rather than hacking the pre/postinst scripts, the dh command in debian/rules should have the --with systemd option added, and I need to have dh-systemd installed on my Ubuntu 18.04 system. To achieve this, I pause after running py2dsc, add the relevant entry to debian/rules, and then run dpkg-buildpackage.

For reasons I don't understand, there is no mention of dh-systemd in the output of dpkg-buildpackage:

...
   dh_installdocs -O--buildsystem=pybuild
   dh_installchangelogs -O--buildsystem=pybuild
   dh_python3 -O--buildsystem=pybuild
   dh_perl -O--buildsystem=pybuild
   dh_link -O--buildsystem=pybuild
   dh_strip_nondeterminism -O--buildsystem=pybuild
   dh_compress -O--buildsystem=pybuild
   dh_fixperms -O--buildsystem=pybuild
   dh_missing -O--buildsystem=pybuild
   dh_installdeb -O--buildsystem=pybuild
   dh_gencontrol -O--buildsystem=pybuild
   dh_md5sums -O--buildsystem=pybuild
   dh_builddeb -O--buildsystem=pybuild
...

I've searched around and although I can find examples of people building python packages, and people building packages with systemd Units, I haven't been able to find an example of someone packaging both a python project and an associated systemd Unit, so I'm not sure where to go from here. I'm wondering if the use of pybuild conflicts with the use of --with systemd?

1

There are 1 answers

0
Braiam On BEST ANSWER

If you are going to use dpkg-buildpackage, you should create the debian/ directory and all files there according to the python packaging documentation. This will create a fully compliant to policy Debian package. The stdeb package only offers a quick and dirty method to create a Debian package. If you are meaning to publish this package in a Debian repository, using stdeb is counterproductive.

Using this method, you can use the dh_systemd helper, and activate it automatically using debcompat >= 10. The service file must be packagename.service on the Debian directory.