| 1 |
================= |
| 2 |
Getting Started |
| 3 |
================= |
| 4 |
|
| 5 |
This tutorial will guide you through setting up your first project |
| 6 |
using *dh-virtualenv*. Having some knowledge on how Debian packages |
| 7 |
work might help, but it is not a mandatory requirement when working |
| 8 |
on simple projects. |
| 9 |
|
| 10 |
You also need some basic build tools, so you should install these packages: |
| 11 |
|
| 12 |
.. code-block:: shell |
| 13 |
|
| 14 |
sudo apt-get install build-essential debhelper devscripts equivs |
| 15 |
|
| 16 |
These are only required on the *build host*, |
| 17 |
not the *target hosts* you later install the built packages on. |
| 18 |
|
| 19 |
If you perform your :ref:`package builds in a Docker container <docker-builds>`, |
| 20 |
you can also skip installing these tools, because then only ``docker-ce`` is needed. |
| 21 |
|
| 22 |
|
| 23 |
Step 1: Install dh-virtualenv |
| 24 |
============================= |
| 25 |
|
| 26 |
.. rubric:: Overview |
| 27 |
|
| 28 |
In order to use it, you need to install *dh-virtualenv* as a debhelper add-on |
| 29 |
on the build host. For Debian and Ubuntu, there are pre-built packages for |
| 30 |
the 1.0 version available – note that some of this info might get outdated over time, |
| 31 |
so take extra care to check the version numbers you're actually getting vs. what features you need. |
| 32 |
|
| 33 |
The following paragraphs describe the various installation options, |
| 34 |
including building from source when your specific environment provides |
| 35 |
no packages or only older versions. |
| 36 |
|
| 37 |
Using pre-1.1 versions is possible, but you don't get all features described in this document, |
| 38 |
and not all projects using dh-virtualenv might work with older versions |
| 39 |
(check their documentation). |
| 40 |
|
| 41 |
.. rubric:: Package installation from OS repositories |
| 42 |
|
| 43 |
On Debian *Stretch* (stable) it is a simple ``apt install dh-virtualenv`` to get v1.0 installed. |
| 44 |
To install on *Jessie* (oldstable) from `their package repositories`_, use these commands: |
| 45 |
|
| 46 |
.. code-block:: bash |
| 47 |
|
| 48 |
echo "deb http://ftp.debian.org/debian jessie-backports main" \ |
| 49 |
| sudo tee /etc/apt/sources.list.d/jessie-backports.list >/dev/null |
| 50 |
sudo apt-get update -qq |
| 51 |
sudo apt-get install -t jessie-backports dh-virtualenv |
| 52 |
|
| 53 |
Note that ``jessie-backports`` also only offers the 1.0 version. |
| 54 |
|
| 55 |
Another option to check out for *Ubuntu* is `this PPA`_, maintained by the author. |
| 56 |
|
| 57 |
It is also possible to get newer versions from Debian testing (sid) |
| 58 |
or recent releases in the `official Ubuntu repositories`_. |
| 59 |
Since dh-virtualenv has the ``all`` architecture (contains no native code), |
| 60 |
that is generally possible, but you might need to take extra care of dependencies. |
| 61 |
The recommendation is to only follow that route in :ref:`Docker container builds <docker-builds>`, |
| 62 |
where manipulating dependencies has no lasting effect |
| 63 |
– don't do that on your workstation. |
| 64 |
|
| 65 |
.. rubric:: Build your own package |
| 66 |
|
| 67 |
For all other platforms you have to build and install the tool yourself. |
| 68 |
The easiest way (since v1.1) is to build the package using Docker |
| 69 |
with the ``invoke bdist_deb`` command in a boot-strapped working directory, |
| 70 |
see the README for details on that. |
| 71 |
Using Docker also allows cross-distribution builds. |
| 72 |
|
| 73 |
Otherwise, after you have cloned the repository, |
| 74 |
you must install build tools and dependencies on your worksatation, |
| 75 |
and then start the build: |
| 76 |
|
| 77 |
.. code-block:: bash |
| 78 |
|
| 79 |
# Install needed packages |
| 80 |
sudo apt-get install devscripts python-virtualenv python-sphinx \ |
| 81 |
python-sphinx-rtd-theme git equivs |
| 82 |
# Clone git repository |
| 83 |
git clone https://github.com/spotify/dh-virtualenv.git |
| 84 |
# Change into working directory |
| 85 |
cd dh-virtualenv |
| 86 |
# This will install build dependencies |
| 87 |
sudo mk-build-deps -ri |
| 88 |
# Build the *dh-virtualenv* package |
| 89 |
dpkg-buildpackage -us -uc -b |
| 90 |
|
| 91 |
# And finally, install it (you might have to solve some |
| 92 |
# dependencies when doing this) |
| 93 |
sudo dpkg -i ../dh-virtualenv_<version>.deb |
| 94 |
|
| 95 |
|
| 96 |
.. _`their package repositories`: https://packages.debian.org/source/sid/dh-virtualenv |
| 97 |
.. _`official Ubuntu repositories`: http://packages.ubuntu.com/search?keywords=dh-virtualenv |
| 98 |
.. _`this PPA`: https://launchpad.net/~spotify-jyrki/+archive/ubuntu/dh-virtualenv |
| 99 |
|
| 100 |
|
| 101 |
Step 2: Set up packaging for your project |
| 102 |
========================================= |
| 103 |
|
| 104 |
Grab your favourite Python project you want to use *dh-virtualenv* |
| 105 |
with and set it up. Only requirement is that your project has a |
| 106 |
somewhat sane ``setup.py`` and requirements listed in a |
| 107 |
``requirements.txt`` file. Note however that defining any requirements |
| 108 |
is not mandatory, if you have none. |
| 109 |
|
| 110 |
Instead of following all the steps outlined below, |
| 111 |
you can use cookiecutters (project templates) to quickly create the needed information |
| 112 |
in the ``debian/`` directory for any existing project. |
| 113 |
|
| 114 |
* `dh-virtualenv-mold <https://github.com/Springerle/dh-virtualenv-mold>`_ is |
| 115 |
a cookiecutter template to add easy Debianization to any existing Python project. |
| 116 |
* `debianized-pypi-mold <https://github.com/Springerle/debianized-pypi-mold>`_ |
| 117 |
does the same for 3rd party software released to PyPI which you want to package |
| 118 |
for deployment. |
| 119 |
|
| 120 |
See the related READMEs for details. |
| 121 |
|
| 122 |
For the manual way, |
| 123 |
start with defining the Debian packaging metadata for your software. |
| 124 |
To do this, create a directory called ``debian`` in the project root. |
| 125 |
|
| 126 |
To be able to build a debian package, a few files are needed. First, we |
| 127 |
need to define the compatibility level of the project. For this, do: |
| 128 |
|
| 129 |
.. code-block:: bash |
| 130 |
|
| 131 |
echo "9" > debian/compat |
| 132 |
|
| 133 |
The 9 is a magic number for latest compatibility level, but we don't |
| 134 |
need to worry about that. Next we need a file that tells what our |
| 135 |
project is about, a file called ``control``. |
| 136 |
Create a ``debian/control`` file similar to the following: |
| 137 |
|
| 138 |
.. code-block:: control |
| 139 |
|
| 140 |
Source: my-awesome-python-software |
| 141 |
Section: python |
| 142 |
Priority: extra |
| 143 |
Maintainer: Matt Maintainer <[email protected]> |
| 144 |
Build-Depends: debhelper (>= 9), python, dh-virtualenv (>= 0.8) |
| 145 |
Standards-Version: 3.9.5 |
| 146 |
|
| 147 |
Package: my-awesome-python-software |
| 148 |
Architecture: any |
| 149 |
Pre-Depends: dpkg (>= 1.16.1), python2.7 | python2.6, ${misc:Pre-Depends} |
| 150 |
Depends: ${misc:Depends} |
| 151 |
Description: A short summary of what this is. |
| 152 |
Further indented lines can contain extra information. |
| 153 |
. |
| 154 |
A single dot separates paragraphs. |
| 155 |
|
| 156 |
The ``control`` file is used to define the build dependencies, so if you |
| 157 |
are building a package that requires for example ``lxml``, make sure |
| 158 |
you define ``libxml2-dev`` in *Build-Depends*. |
| 159 |
|
| 160 |
*Depends* in the 2nd section is used to define run-time dependencies. |
| 161 |
The *debhelper* magic will usually take care of that via the ``${misc:Depends}`` you see above. |
| 162 |
|
| 163 |
To help keeping your installed virtualenv in sync with the host's Python |
| 164 |
interpreter in case of updates, create a file named |
| 165 |
``debian/«pkgname».triggers``, where ``«pkgname»`` is what you |
| 166 |
named your package in the ``control`` file. It triggers a special script |
| 167 |
whenever the Python binary changes; don't worry, that script is provided |
| 168 |
by ``dh-virtualenv`` automatically. |
| 169 |
|
| 170 |
.. rubric:: «pkgname».triggers |
| 171 |
|
| 172 |
.. code-block:: ini |
| 173 |
|
| 174 |
# Register interest in Python interpreter changes (Python 2 for now); and |
| 175 |
# don't make the Python package dependent on the virtualenv package |
| 176 |
# processing (noawait) |
| 177 |
interest-noawait /usr/bin/python2.6 |
| 178 |
interest-noawait /usr/bin/python2.7 |
| 179 |
|
| 180 |
# Also provide a symbolic trigger for all dh-virtualenv packages |
| 181 |
interest dh-virtualenv-interpreter-update |
| 182 |
|
| 183 |
That file *must* end with a new-line – |
| 184 |
if your editor is misconfigured to eat the end of the last line in a file, |
| 185 |
you better fix that. |
| 186 |
|
| 187 |
Note that if you provide a custom ``postinst`` script with your package, |
| 188 |
then don't forget to put the ``#DEBHELPER#`` marker into it, else the trigger |
| 189 |
script will be missing. |
| 190 |
The same applies to other maintainer scripts. |
| 191 |
|
| 192 |
Next, we need a changelog file. It is basically a documentation of |
| 193 |
changes in your package plus the source for version number for Debian |
| 194 |
package builder. Here's a short sample changelog to be entered in |
| 195 |
``debian/changelog``: |
| 196 |
|
| 197 |
:: |
| 198 |
|
| 199 |
my-awesome-python-software (0.1-1) unstable; urgency=low |
| 200 |
|
| 201 |
* Initial public release |
| 202 |
|
| 203 |
-- Matt Maintainer <[email protected]> Fri, 01 Nov 2013 17:00:00 +0200 |
| 204 |
|
| 205 |
You don't need to create this file by hand, a handy tool called |
| 206 |
``dch`` exists for entering new changelog entries. |
| 207 |
|
| 208 |
Now, the last bit left is adding the ``debian/rules`` file. This file |
| 209 |
is usually an executable *Makefile* that Debian uses to build the package. |
| 210 |
The content for that is fairly simple: |
| 211 |
|
| 212 |
.. code-block:: make |
| 213 |
|
| 214 |
#!/usr/bin/make -f |
| 215 |
|
| 216 |
%: |
| 217 |
dh $@ --with python-virtualenv |
| 218 |
|
| 219 |
And there we go, debianization of your new package is ready! |
| 220 |
|
| 221 |
.. tip:: |
| 222 |
|
| 223 |
Do not forget to ``git add`` the ``debian/`` directory *before* |
| 224 |
you build for the first time, because generated files will be added there |
| 225 |
that you don't want in your source code repository. |
| 226 |
|
| 227 |
|
| 228 |
Step 3: Build your project |
| 229 |
========================== |
| 230 |
|
| 231 |
Now you can just build your project by running |
| 232 |
``( deactivate ; dpkg-buildpackage -us -uc -b )``. |
| 233 |
Enjoy your newly baked *dh-virtualenv* backed project! ☺ |