| 1 |
# dh-virtualenv |
| 2 |
|
| 3 |
[](https://travis-ci.org/spotify/dh-virtualenv) |
| 4 |
[](http://dh-virtualenv.readthedocs.io/en/latest/) |
| 5 |
|
| 6 |
**Contents** |
| 7 |
|
| 8 |
* [Overview](#overview) |
| 9 |
* [Presentations, Blogs & Other Resources](#presentations-blogs--other-resources) |
| 10 |
* [Using dh-virtualenv](#using-dh-virtualenv) |
| 11 |
* [How does it work?](#how-does-it-work) |
| 12 |
* [Running tests](#running-tests) |
| 13 |
* [Building the package in a Docker container](#building-the-package-in-a-docker-container) |
| 14 |
* [Building the documentation locally](#building-the-documentation-locally) |
| 15 |
* [Releasing a new version](#releasing-a-new-version) |
| 16 |
* [Code of conduct](#code-of-conduct) |
| 17 |
* [License](#license) |
| 18 |
|
| 19 |
|
| 20 |
## Overview |
| 21 |
|
| 22 |
dh-virtualenv is a tool that aims to combine Debian packaging with |
| 23 |
self-contained virtualenv based Python deployments. |
| 24 |
|
| 25 |
The idea behind dh-virtualenv is to be able to combine the power of |
| 26 |
Debian packaging with the sandboxed nature of virtualenvs. In addition |
| 27 |
to this, using virtualenv enables installing requirements via |
| 28 |
[Python Package Index](https://pypi.org) instead of relying on |
| 29 |
the operating system provided Python packages. The only limiting |
| 30 |
factor is that you have to run the same Python interpreter as the |
| 31 |
operating system. |
| 32 |
|
| 33 |
For complete online documentation including installation instructions, see |
| 34 |
[the online documentation](https://dh-virtualenv.readthedocs.io/en/latest/). |
| 35 |
|
| 36 |
|
| 37 |
## Presentations, Blogs & Other Resources |
| 38 |
|
| 39 |
Here are a few external resources that can help you |
| 40 |
to get a more detailed first impression of dh-virtualenv, |
| 41 |
or advocate its use in your company or project team. |
| 42 |
|
| 43 |
* [How We Deploy Python Code](https://www.nylas.com/blog/packaging-deploying-python/) |
| 44 |
– Building, packaging & deploying Python using versioned artifacts in Debian packages at Nylas. |
| 45 |
* [DevOps Tool Bazaar](https://speakerdeck.com/jhermann/devops-karlsruhe-meetup-2018-02-20) |
| 46 |
– Slide deck presented at the DevOps Karlsruhe Meetup in February 2018, regarding Python software deployment for Debian with a practical example. |
| 47 |
* [The Architecture of Open Source Applications: Python Packaging](http://aosabook.org/en/packaging.html) |
| 48 |
– This provides a lot of background (and possibly things you didn't know) about the Python side of packaging. |
| 49 |
|
| 50 |
|
| 51 |
## Using dh-virtualenv |
| 52 |
|
| 53 |
Using dh-virtualenv is fairly straightforward. First, you need to |
| 54 |
define the requirements of your package in `requirements.txt` file, in |
| 55 |
[the format defined by pip](https://pip.pypa.io/en/latest/user_guide.html#requirements-files). |
| 56 |
|
| 57 |
To build a package using dh-virtualenv, you need to add dh-virtualenv |
| 58 |
in to your build dependencies and write following `debian/rules` file: |
| 59 |
|
| 60 |
%: |
| 61 |
dh $@ --with python-virtualenv |
| 62 |
|
| 63 |
Note that you might need to provide |
| 64 |
additional build dependencies too, if your requirements require them. |
| 65 |
|
| 66 |
Also, you are able to define the root path for your source directory using |
| 67 |
`--sourcedirectory` or `-D` argument: |
| 68 |
|
| 69 |
%: |
| 70 |
dh $@ --with python-virtualenv --sourcedirectory=root/srv/application |
| 71 |
|
| 72 |
NOTE: Be aware that the configuration in debian/rules expects tabs instead of spaces! |
| 73 |
|
| 74 |
Once the package is built, you have a virtualenv contained in a Debian |
| 75 |
package and upon installation it gets placed, by default, under |
| 76 |
`/opt/venvs/<packagename>`. |
| 77 |
|
| 78 |
For more information and usage documentation, check the accompanying |
| 79 |
documentation in the `doc` folder, also available at |
| 80 |
[Read the Docs](https://dh-virtualenv.readthedocs.io/en/latest/). |
| 81 |
|
| 82 |
|
| 83 |
## How does it work? |
| 84 |
|
| 85 |
To do the packaging, *dh-virtualenv* extends debhelper's sequence by |
| 86 |
inserting a new `dh_virtualenv` command, which effectively replaces |
| 87 |
the following commands in the original sequence: |
| 88 |
|
| 89 |
* `dh_auto_clean` |
| 90 |
* `dh_auto_build` |
| 91 |
* `dh_auto_test` |
| 92 |
* `dh_auto_install` |
| 93 |
* `dh_python2` |
| 94 |
* `dh_pycentral` |
| 95 |
* `dh_pysupport` |
| 96 |
|
| 97 |
In the new sequence, `dh_virtualenv` is inserted right before `dh_installinit`. |
| 98 |
|
| 99 |
|
| 100 |
## Running tests |
| 101 |
|
| 102 |
$ nosetests ./test/test_deployment.py |
| 103 |
|
| 104 |
|
| 105 |
## Building the package in a Docker container |
| 106 |
|
| 107 |
To build ``dh-virtualenv`` itself in a Docker container, call ``docker build --tag dh-venv-builder .``. |
| 108 |
This will build the DEB package for Debian stable by default. |
| 109 |
Add e.g. ``--build-arg distro=ubuntu:bionic`` to build for Ubuntu LTS instead. |
| 110 |
|
| 111 |
The resulting files must be copied out of the build container, using these commands: |
| 112 |
|
| 113 |
```sh |
| 114 |
mkdir -p dist && docker run --rm dh-venv-builder tar -C /dpkg -c . | tar -C dist -xv |
| 115 |
``` |
| 116 |
|
| 117 |
There is also a short-cut for all this, in the form of ``invoke bdist_deb [--distro=‹id›:‹codename›]``. |
| 118 |
|
| 119 |
|
| 120 |
## Building the documentation locally |
| 121 |
|
| 122 |
If you execute the following commands in your clone of the repository, |
| 123 |
a virtualenv with all necessary tools is created. |
| 124 |
``invoke docs`` then builds the documentation into ``doc/_build/``. |
| 125 |
|
| 126 |
```sh |
| 127 |
command . .env --yes --develop |
| 128 |
invoke docs |
| 129 |
``` |
| 130 |
|
| 131 |
To **start a watchdog that auto-rebuilds documentation** and reloads the opened browser tab on any change, |
| 132 |
call ``invoke docs -w -b`` (stop the watchdog using the ``-k`` option). |
| 133 |
|
| 134 |
|
| 135 |
## Releasing a new version |
| 136 |
|
| 137 |
Follow these steps when creating a new release: |
| 138 |
|
| 139 |
1. Check version in `dh_virtualenv/_version.py` and `debian/changelog`. |
| 140 |
1. Make sure `doc/changes.rst` is complete. |
| 141 |
1. Bump release date in `debian/changelog` (`dch -r`). |
| 142 |
1. Tag the release and `git push --tags`. |
| 143 |
1. Edit release entry on GitHub (add changes). |
| 144 |
1. Update the *Ubuntu PPA*. |
| 145 |
1. Bump to next release version in `dh_virtualenv/_version.py`. |
| 146 |
1. Open new section in `debian/changelog` (with `…-0.1+dev` added). |
| 147 |
1. Open a new section in `doc/changes.rst`, so it can be maintained as features are added! |
| 148 |
|
| 149 |
|
| 150 |
## Code of conduct |
| 151 |
|
| 152 |
This project adheres to the [Open Code of Conduct][code-of-conduct]. |
| 153 |
By participating, you are expected to honor this code. |
| 154 |
|
| 155 |
|
| 156 |
## License |
| 157 |
|
| 158 |
Copyright (c) 2013-2017 Spotify AB |
| 159 |
|
| 160 |
dh-virtualenv is licensed under GPL v2 or later. Full license is |
| 161 |
available in the `LICENSE` file. |
| 162 |
|
| 163 |
[code-of-conduct]: https://github.com/spotify/code-of-conduct/blob/master/code-of-conduct.md |