| 1 |
==================== |
| 2 |
Packaging Cookbook |
| 3 |
==================== |
| 4 |
|
| 5 |
This chapter has recipes and tips for specific dh-virtualenv use-cases, |
| 6 |
like proper handling of binary ``manylinux1`` wheels. |
| 7 |
It also demonstrates some Debian packaging and debhelper features |
| 8 |
that are useful in the context of Python software packaging. |
| 9 |
|
| 10 |
.. contents:: List of Recipes |
| 11 |
:local: |
| 12 |
|
| 13 |
|
| 14 |
.. _py3-package: |
| 15 |
|
| 16 |
Building Packages for Python3 |
| 17 |
============================= |
| 18 |
|
| 19 |
The Python2 EOL in 2020 is not so far away, so you better start to use |
| 20 |
Python3 for new projects, and port old ones that you expect to survive until then. |
| 21 |
The following is for *Ubuntu Xenial* or *Debian Stretch* with Python 3.5, |
| 22 |
and on *Ubuntu Bionic* you get Python 3.6. |
| 23 |
|
| 24 |
In ``debian/control``, the ``Build-Depends`` and ``Pre-Depends`` lists |
| 25 |
have to refer to Python3 packages. |
| 26 |
|
| 27 |
.. code-block:: ini |
| 28 |
|
| 29 |
Source: py3software |
| 30 |
Section: contrib/python |
| 31 |
… |
| 32 |
Build-Depends: debhelper (>= 9), python3, dh-virtualenv (>= 1.0), |
| 33 |
python3-setuptools, python3-pip, python3-dev, libffi-dev |
| 34 |
… |
| 35 |
|
| 36 |
Package: py3software |
| 37 |
… |
| 38 |
Pre-Depends: dpkg (>= 1.16.1), python3 (>= 3.5), ${misc:Pre-Depends} |
| 39 |
|
| 40 |
And the Python update triggers in ``debian/«pkgname».triggers`` need to be adapted, too. |
| 41 |
|
| 42 |
.. code-block:: ini |
| 43 |
|
| 44 |
… |
| 45 |
interest-noawait /usr/bin/python3.5 |
| 46 |
… |
| 47 |
|
| 48 |
You may also need to add the :option:`--python` option in ``debian/rules``. |
| 49 |
|
| 50 |
.. code-block:: make |
| 51 |
|
| 52 |
%: |
| 53 |
dh $@ --with python-virtualenv |
| 54 |
|
| 55 |
override_dh_virtualenv: |
| 56 |
dh_virtualenv --python python3 |
| 57 |
|
| 58 |
If you're using the buildsystem alternative, it is instead specified through |
| 59 |
the :envvar:`DH_VIRTUALENV_ARGUMENTS` variable. |
| 60 |
|
| 61 |
.. code-block:: make |
| 62 |
|
| 63 |
export DH_VIRTUALENV_ARGUMENTS := --no-site-packages --python python3 |
| 64 |
|
| 65 |
%: |
| 66 |
dh $@ --buildsystem dh_virtualenv |
| 67 |
|
| 68 |
|
| 69 |
.. _fhs-links: |
| 70 |
|
| 71 |
Making executables available |
| 72 |
============================ |
| 73 |
|
| 74 |
To make executables in your virtualenv's ``bin`` directory callable from any shell prompt, |
| 75 |
do **not** add that directory to the global ``PATH`` by a ``profile.d`` hook or similar. |
| 76 |
This would add all the other stuff in there too, and you simply do not want that. |
| 77 |
|
| 78 |
So use the ``debian/«pkgname».links`` file to add a symbolic link to *those* exectuables |
| 79 |
you want to be visible, typically the one created by your main application package. |
| 80 |
|
| 81 |
.. code-block:: ini |
| 82 |
|
| 83 |
opt/venvs/«venvname»/bin/«cmdname» usr/bin/«cmdname» |
| 84 |
|
| 85 |
Replace the contained ``«placeholders»`` with the correct names. |
| 86 |
Add more links if there are additional tools, one line per extra executable. |
| 87 |
For ``root``-only commands, use ``usr/sbin/…``. |
| 88 |
|
| 89 |
|
| 90 |
.. _manylinux1: |
| 91 |
|
| 92 |
Handling binary wheels |
| 93 |
====================== |
| 94 |
|
| 95 |
The introduction of `manylinux`_ wheels via `PEP 513`_ is a gift, |
| 96 |
sent by the PyPA community to us lowly developers wanting to use |
| 97 |
packages like Numpy while *not* installing a Fortran compiler just for that. |
| 98 |
|
| 99 |
However, two steps during package building often clash with the contained shared libraries, |
| 100 |
namely *stripping* (reducing the size of symbol tables) |
| 101 |
and scraping package dependencies out of shared libraries (*shlibdeps*). |
| 102 |
|
| 103 |
So if you get errors thrown at you by either ``dh_strip`` or ``dh_shlibdeps``, |
| 104 |
extend your ``debian/rules`` file as outlined below. |
| 105 |
|
| 106 |
.. code-block:: makefile |
| 107 |
|
| 108 |
.PHONY: override_dh_strip override_dh_shlibdeps |
| 109 |
|
| 110 |
override_dh_strip: |
| 111 |
dh_strip --exclude=cffi |
| 112 |
|
| 113 |
override_dh_shlibdeps: |
| 114 |
dh_shlibdeps -X/x86/ -X/numpy/.libs -X/scipy/.libs -X/matplotlib/.libs |
| 115 |
|
| 116 |
This example works for the Python data science stack |
| 117 |
– you have to list the packages that cause *you* trouble. |
| 118 |
|
| 119 |
.. _manylinux: https://github.com/pypa/manylinux |
| 120 |
.. _`PEP 513`: https://www.python.org/dev/peps/pep-0513/ |
| 121 |
|
| 122 |
|
| 123 |
.. _node-env: |
| 124 |
|
| 125 |
Adding Node.js to your virtualenv |
| 126 |
================================= |
| 127 |
|
| 128 |
There are polyglot projects with a mix of Python and Javascript code, |
| 129 |
and some of the JS code might be executed server-side in a Node.js runtime. |
| 130 |
A typical example is server-side rendering for Angular apps with `Angular Universal`_. |
| 131 |
|
| 132 |
If you have this requirement, there is a useful helper named ``nodeenv``, |
| 133 |
which extends a Python virtualenv to also support installation of NPM packages. |
| 134 |
|
| 135 |
The following changes in ``debian/control`` require *Node.js* to be available on both |
| 136 |
the build and the target hosts. |
| 137 |
As written, the current LTS version is selected (i.e. `8.x` in mid 2018). |
| 138 |
The `NodeSource packages`_ are recommended to provide that dependency. |
| 139 |
|
| 140 |
.. code-block:: ini |
| 141 |
|
| 142 |
… |
| 143 |
Build-Depends: debhelper (>= 9), python3, dh-virtualenv (>= 1.0), |
| 144 |
python3-setuptools, python3-pip, python3-dev, libffi-dev, |
| 145 |
nodejs (>= 8), nodejs (<< 9) |
| 146 |
… |
| 147 |
Depends: ${shlibs:Depends}, ${misc:Depends}, nodejs (>= 8), nodejs (<< 9) |
| 148 |
… |
| 149 |
|
| 150 |
|
| 151 |
You also need to extend ``debian/rules`` as follows, |
| 152 |
change the variables in the first section to define different versions and filesystem locations. |
| 153 |
|
| 154 |
.. code-block:: make |
| 155 |
|
| 156 |
export DH_VIRTUALENV_INSTALL_ROOT=/opt/venvs |
| 157 |
SNAKE=/usr/bin/python3 |
| 158 |
EXTRA_REQUIREMENTS=--upgrade-pip --preinstall "setuptools>=17.1" --preinstall "wheel" |
| 159 |
NODEENV_VERSION=1.3.1 |
| 160 |
|
| 161 |
PACKAGE=$(shell dh_listpackages) |
| 162 |
DH_VENV_ARGS=--setuptools --python $(SNAKE) $(EXTRA_REQUIREMENTS) |
| 163 |
DH_VENV_DIR=debian/$(PACKAGE)$(DH_VIRTUALENV_INSTALL_ROOT)/$(PACKAGE) |
| 164 |
|
| 165 |
ifeq (,$(wildcard $(CURDIR)/.npmrc)) |
| 166 |
NPM_CONFIG=~/.npmrc |
| 167 |
else |
| 168 |
NPM_CONFIG=$(CURDIR)/.npmrc |
| 169 |
endif |
| 170 |
|
| 171 |
|
| 172 |
%: |
| 173 |
dh $@ --with python-virtualenv $(DH_VENV_ARGS) |
| 174 |
|
| 175 |
.PHONY: override_dh_virtualenv |
| 176 |
|
| 177 |
override_dh_virtualenv: |
| 178 |
dh_virtualenv $(DH_VENV_ARGS) |
| 179 |
$(DH_VENV_DIR)/bin/python $(DH_VENV_DIR)/bin/pip install nodeenv==$(NODEENV_VERSION) |
| 180 |
$(DH_VENV_DIR)/bin/nodeenv -C '' -p -n system |
| 181 |
. $(DH_VENV_DIR)/bin/activate \ |
| 182 |
&& node /usr/bin/npm install --userconfig=$(NPM_CONFIG) \ |
| 183 |
-g configurable-http-proxy |
| 184 |
|
| 185 |
You want to always copy all but the last line literally. |
| 186 |
The lines above it install and embed ``nodeenv`` into the virtualenv |
| 187 |
freshly created by the ``dh_virtualenv`` call. |
| 188 |
Also remember to use TABs in makefiles (``debian/rules`` is one). |
| 189 |
|
| 190 |
The last (logical) line globally installs the ``configurable-http-proxy`` NPM package |
| 191 |
– one important result of using ``-g`` is that Javascript commands appear |
| 192 |
in the ``bin`` directory just like Python ones. |
| 193 |
That in turn means that in the activated virtualenv Python can easily call those JS commands, |
| 194 |
because they're on the ``PATH``. |
| 195 |
|
| 196 |
Change the NPM package name to what you want to install. |
| 197 |
``npm`` uses either a local ``.npmrc`` file in the project root, |
| 198 |
or else the ``~/.npmrc`` one. |
| 199 |
Add local repository URLs and credentials to one of these files. |
| 200 |
|
| 201 |
.. _`NodeSource packages`: https://github.com/nodesource/distributions |
| 202 |
.. _`Angular Universal`: https://universal.angular.io/ |
| 203 |
|
| 204 |
|
| 205 |
.. _docker-builds: |
| 206 |
|
| 207 |
Multi-platform builds in Docker |
| 208 |
=============================== |
| 209 |
|
| 210 |
The code shown here is taken from the :ref:`example-debianized-jupyterhub` project, |
| 211 |
and explains how to build a package in a Docker container. |
| 212 |
|
| 213 |
Why build a package in a container? This is why: |
| 214 |
|
| 215 |
* *repeatable* builds in a *clean* environment |
| 216 |
* explicitly *documented installation* of build requirements *(as code)* |
| 217 |
* easy *multi-distro multi-release builds* |
| 218 |
|
| 219 |
The build is driven by a small shell script named ``build.sh``, |
| 220 |
which we use to get the target platform and some project metadata we already have, |
| 221 |
and feed that into the Dockerfile via simple ``sed`` templating. |
| 222 |
|
| 223 |
So we work on a copy of the Dockerfile, and that is one reason for |
| 224 |
anything in the project workdir that is controlled by git being copied to a staging area (a separate build directory). |
| 225 |
The other reason is performance – we present Docker with a pristine copy of our workdir, |
| 226 |
and so there are no accidents like ``COPY``\ ing a full development virtualenv |
| 227 |
or all of ``.git`` into the container build. |
| 228 |
|
| 229 |
.. rubric:: The build script |
| 230 |
|
| 231 |
Let's get to the code – since we apply the :ref:`node-env` recipe, |
| 232 |
we first set the repository where to get Node.js from. |
| 233 |
|
| 234 |
.. literalinclude:: examples/build.sh |
| 235 |
:language: shell |
| 236 |
:end-at: NODEREPO |
| 237 |
|
| 238 |
Next, the given platform and existing project metadata is stored into a bunch of variables. |
| 239 |
|
| 240 |
.. literalinclude:: examples/build.sh |
| 241 |
:language: shell |
| 242 |
:start-after: NODEREPO |
| 243 |
:end-before: Prepare |
| 244 |
|
| 245 |
Based on the collected input parameters, the staging area is set up in the ``build/staging`` directory. |
| 246 |
``tar`` does the selective copy work, and ``sed`` is used to inject dynamic values into the copied files. |
| 247 |
|
| 248 |
.. literalinclude:: examples/build.sh |
| 249 |
:language: shell |
| 250 |
:start-at: Prepare |
| 251 |
:end-before: Build in Docker |
| 252 |
|
| 253 |
After all that prep work, we finally get to build our package. |
| 254 |
The results are copied from ``/dpkg`` where the Dockerfile put them (see below), |
| 255 |
and then the package metadata is shown for a quick visual check if everything looks OK. |
| 256 |
|
| 257 |
.. literalinclude:: examples/build.sh |
| 258 |
:language: shell |
| 259 |
:start-at: Build in Docker |
| 260 |
|
| 261 |
|
| 262 |
.. rubric:: The Dockerfile |
| 263 |
|
| 264 |
This is the complete Dockerfile, the important things are the two ``RUN`` directives. |
| 265 |
|
| 266 |
.. literalinclude:: examples/Dockerfile.build |
| 267 |
:language: docker |
| 268 |
|
| 269 |
The first ``RUN`` installs all the build dependencies on top of the base image. |
| 270 |
The second one then builds the package and makes a copy of the resulting files, |
| 271 |
for the build script to pick them up. |
| 272 |
|
| 273 |
|
| 274 |
.. rubric:: Putting it all together |
| 275 |
|
| 276 |
Here's a sample run of building for *Ubuntu Bionic*. |
| 277 |
|
| 278 |
.. code-block:: console |
| 279 |
|
| 280 |
$ ./build.sh ubuntu:bionic |
| 281 |
Sending build context to Docker daemon 106kB |
| 282 |
Step 1/6 : FROM ubuntu:bionic AS dpkg-build |
| 283 |
… |
| 284 |
Successfully tagged debianized-jupyterhub-ubuntu-bionic:latest |
| 285 |
./ |
| 286 |
./jupyterhub_0.9.1-1~bionic_amd64.deb |
| 287 |
./jupyterhub_0.9.1-1~bionic_amd64.buildinfo |
| 288 |
./jupyterhub-dbgsym_0.9.1-1~bionic_amd64.ddeb |
| 289 |
./jupyterhub_0.9.1-1~bionic_amd64.changes |
| 290 |
new debian package, version 2.0. |
| 291 |
size 265372284 bytes: control archive=390780 bytes. |
| 292 |
84 bytes, 3 lines conffiles |
| 293 |
1214 bytes, 25 lines control |
| 294 |
2350661 bytes, 17055 lines md5sums |
| 295 |
4369 bytes, 141 lines * postinst #!/bin/sh |
| 296 |
1412 bytes, 47 lines * postrm #!/bin/sh |
| 297 |
696 bytes, 35 lines * preinst #!/bin/sh |
| 298 |
1047 bytes, 41 lines * prerm #!/bin/sh |
| 299 |
217 bytes, 6 lines shlibs |
| 300 |
419 bytes, 10 lines triggers |
| 301 |
Package: jupyterhub |
| 302 |
Version: 0.9.1-1~bionic |
| 303 |
Architecture: amd64 |
| 304 |
Maintainer: 1&1 Group <[email protected]> |
| 305 |
Installed-Size: 563574 |
| 306 |
Pre-Depends: dpkg (>= 1.16.1), python3 (>= 3.5) |
| 307 |
Depends: perl:any, libc6 (>= 2.25), libexpat1 (>= 2.1~beta3), libgcc1 (>= 1:4.0), … |
| 308 |
Suggests: oracle-java8-jre | openjdk-8-jre | zulu-8 |
| 309 |
Section: contrib/python |
| 310 |
Priority: extra |
| 311 |
Homepage: https://github.com/1and1/debianized-jupyterhub |
| 312 |
Description: Debian packaging of JupyterHub, a multi-user server for Jupyter notebooks. |
| 313 |
… |
| 314 |
|
| 315 |
The package files are now in ``build/``, and you can ``dput`` them into your local repository. |
| 316 |
|
| 317 |
|
| 318 |
.. _cross-package: |
| 319 |
|
| 320 |
Cross-packaging for ARM targets |
| 321 |
=============================== |
| 322 |
|
| 323 |
If you need to create packages that can be installed on ARM architectures, |
| 324 |
but want to use any build host (e.g. a CI worker), |
| 325 |
first install the ``qemu-user-static`` and ``binfmt-support`` packages. |
| 326 |
|
| 327 |
Then build the package by starting a container in QEMU using this ``Dockerfile``. |
| 328 |
|
| 329 |
.. code-block:: docker |
| 330 |
|
| 331 |
FROM arm32v7/debian:latest |
| 332 |
|
| 333 |
RUN apt-get update && apt-get -y upgrade && apt-get update \ |
| 334 |
&& apt-get -y install sudo dpkg-dev debhelper dh-virtualenv python3 python3-venv |
| 335 |
… |
| 336 |
|
| 337 |
The build might fail from time to time, due to unknown causes (maybe instabilities in QEMU). |
| 338 |
If you get a package out of it, that works 100% fine, however. |
| 339 |
|
| 340 |
See :ref:`example-configsite` for the full project that uses this. |
| 341 |
|
| 342 |
.. epigraph:: |
| 343 |
|
| 344 |
— with input from `@Nadav-Ruskin`_ |
| 345 |
|
| 346 |
.. _`@Nadav-Ruskin`: https://github.com/Nadav-Ruskin |