Debian Codegraph @

doc/usage.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 =================
2 Packaging Guide
3 =================
4
5 Building packages with *dh-virtualenv* is relatively easy to start with,
6 but it also supports lot of customization to match your specific needs.
7
8 By default, *dh-virtualenv* installs your packages under
9 ``/opt/venvs/«packagename»``. The package name is provided by
10 the ``debian/control`` file.
11
12 To use an alternative install prefix, add a line like the following
13 to the top of your ``debian/rules`` file.
14
15 .. code-block:: make
16
17 export DH_VIRTUALENV_INSTALL_ROOT=«/your/custom/install/dir»
18
19 dh_virtualenv will now use the value of
20 :envvar:`DH_VIRTUALENV_INSTALL_ROOT` instead of ``/opt/venvs``
21 when it constructs the install path.
22
23 To use an install suffix other than the package name, call
24 ``dh_virtualenv`` using the :option:`--install-suffix` command
25 line option. See :ref:`advanced-usage` for further information on passing
26 options.
27
28
29 Simple usecase
30 ==============
31
32 To signal debhelper to use *dh-virtualenv* for building your
33 package, you need to pass ``--with python-virtualenv`` to the debhelper
34 sequencer.
35
36 In a nutshell, the simplest ``debian/rules`` file to build using
37 *dh-virtualenv* looks like this:
38
39 .. code-block:: make
40
41 #!/usr/bin/make -f
42
43 %:
44 dh $@ --with python-virtualenv
45
46 However, the tool makes a few assumptions of your project's structure:
47
48 * For installing requirements, you need to have a file called
49 ``requirements.txt`` in the root directory of your project. The
50 requirements file is not mandatory.
51 * The project must have a ``setup.py`` file in the root of the
52 project. ``dh_virtualenv`` will run ``setup.py install`` to add
53 your project to the virtualenv.
54
55 After these preparations, you can just build the package with your favorite tool!
56
57
58 Environment variables
59 =====================
60
61 Certain environment variables can be used to customise the behaviour
62 of the debhelper sequencer in addition to the standard debhelper
63 variables.
64
65 .. envvar:: DH_VIRTUALENV_INSTALL_ROOT
66
67 Define a custom root location to install your package(s). The
68 resulting location for a specific package will be
69 ``$DH_VIRTUALENV_INSTALL_ROOT/«<packagename»``,
70 unless :option:`--install-suffix` is also used to change ``«<packagename»``.
71
72
73 Command line options
74 ====================
75
76 To change its default behavior, the ``dh_virtualenv`` command accepts a
77 few command line options:
78
79 .. option:: -p <package>, --package <package>
80
81 Act on the package named *<package>*.
82
83 .. option:: -N <package>, --no-package <package>
84
85 Do not act on the specified package.
86
87 .. option:: -v, --verbose
88
89 Turn on verbose mode. This has a few effects: it sets the root logger
90 level to ``DEBUG``, and passes the verbose flag to ``pip`` when
91 installing packages. This can also be provided using the standard
92 ``DH_VERBOSE`` environment variable.
93
94 .. option:: --install-suffix <suffix>
95
96 Override virtualenv installation suffix. The suffix is appended to
97 ``/opt/venvs``, or the :envvar:`DH_VIRTUALENV_INSTALL_ROOT`
98 environment variable if specified, to construct the installation
99 path.
100
101 .. option:: --extra-index-url <url>
102
103 Use extra index url *<url>* when running ``pip`` to install
104 packages. This can be provided multiple times to pass multiple URLs
105 to ``pip``. A common use-case is enabling a private Python package repository.
106
107 .. option:: --preinstall <package>
108
109 Package to install before processing the requirements. This flag
110 can be used to provide a package that is installed by ``pip``
111 before processing the requirements file. It is handy if you need to
112 install a custom setup script or other packages needed
113 to parse ``setup.py``, and can be provided multiple times to
114 pass multiple packages for pre-install.
115
116 .. option:: --extras <name>
117
118 .. versionadded:: 1.1
119
120 Name of extras defined in the main package (specifically its ``setup.py``, in ``extras_require``).
121 You can pass this multiple times to add different extra requirements.
122
123 .. option:: --pip-tool <exename>
124
125 Executable that will be used to install requirements after the
126 preinstall stage. Usually you'll install this program by using the
127 ``--preinstall`` argument. The replacement is expected to be found
128 in the virtualenv's ``bin/`` directory.
129
130 .. option:: --upgrade-pip
131
132 .. versionadded:: 1.0
133
134 Force upgrading to the latest available release of ``pip``.
135 This is the first thing done in the pre-install stage,
136 and uses a separate ``pip`` call.
137
138 Options provided via :option:`--extra-pip-arg` are ignored here,
139 because the default ``pip`` of your system might not support them
140 (since version 1.1).
141
142 *Note:* This can produce non-repeatable builds. See also :option:`--upgrade-pip-to`.
143
144 .. option:: --upgrade-pip-to <VERSION>
145
146 .. versionadded:: 1.2
147
148 Same as :option:`--upgrade-pip`, but install an explicitly provided version.
149 You can specify ``latest`` to get the exact same behaviour as with the simple option.
150
151 *Note:* This can be used for more repeatable builds that do not have the risk of
152 breaking on a new ``pip`` release.
153
154 .. option:: --index-url <URL>
155
156 Base URL of the PyPI server. This flag can be used to pass in a
157 custom URL to a PyPI mirror. It's useful if you have an
158 internal PyPI mirror, or you run a special instance that only
159 exposes selected packages of PyPI. If this is not provided, the
160 default will be whatever ``pip`` uses as default (usually the API of
161 ``https://pypi.org/``).
162
163 .. option:: --extra-pip-arg <PIP ARG>
164
165 Extra arguments to pass to the pip executable. This is useful if
166 you need to change the behaviour of pip during the packaging process.
167 You can use this flag multiple times to pass in different pip flags.
168
169 As an example, adding ``--extra-pip-arg --no-compile`` in the call of a
170 ``override_dh_virtualenv`` rule in the ``debian/rules`` file will
171 disable the generation of ``*.pyc`` files.
172
173 .. option:: --extra-virtualenv-arg <VIRTUALENV ARG>
174
175 Extra parameters to pass to the virtualenv executable. This is useful if
176 you need to change the behaviour of virtualenv during the packaging process.
177 You can use this flag multiple times to pass in different virtualenv flags.
178
179 .. option:: --requirements <REQUIREMENTS FILE>
180
181 Use a different requirements file when installing. Some packages
182 such as `pbr <http://docs.openstack.org/developer/pbr/>`_ expect
183 the ``requirements.txt`` file to be a simple list of requirements
184 that can be copied verbatim into the ``install_requires``
185 list. This command option allows specifying a different
186 ``requirements.txt`` file that may include pip specific flags such
187 as ``-i``, ``-r-`` and ``-e``.
188
189 .. option:: --setuptools
190
191 Use setuptools instead of distribute in the virtualenv.
192
193 .. option:: --setuptools-test
194
195 .. versionadded:: 1.0
196
197 Run ``python setup.py test`` when building the package. This was
198 the old default behaviour before version 1.0. This option is
199 incompatible with the deprecated :option:`--no-test`.
200
201 .. option:: --python <path>
202
203 Use a specific Python interpreter found in ``path`` as the
204 interpreter for the virtualenv. Default is to use the system
205 default, usually ``/usr/bin/python``.
206
207 .. option:: --builtin-venv
208
209 Enable the use of the build-in ``venv`` module, i.e. use ``python -m venv``
210 to create the virtualenv. It will only work with Python 3.4 or later,
211 e.g. by using the option
212 :option:`--python` ``/usr/bin/python3.4``.
213
214 .. option:: -S, --use-system-packages
215
216 Enable the use of system site-packages in the created virtualenv
217 by passing the ``--system-site-packages`` flag to ``virtualenv``.
218
219 .. option:: --skip-install
220
221 Skip running ``pip install .`` after dependencies have been
222 installed. This will result in anything specified in ``setup.py`` being
223 ignored. If this package is intended to install a virtualenv
224 and a program that uses the supplied virtualenv, it is up to
225 the user to ensure that if ``setup.py`` exists, any installation logic
226 or dependencies contained therein are handled.
227
228 This option is useful for web application deployments, where the
229 package's virtual environment merely supports
230 an application installed via other means.
231 Typically, the ``debian/«packagename».install`` file is used
232 to place the application at a location outside of the virtual environment.
233
234 .. option:: --pypi-url <URL>
235
236 .. deprecated:: 1.0
237 Use :option:`--index-url` instead.
238
239 .. option:: --no-test
240
241 .. deprecated:: 1.0
242 This option has no effect. See :option:`--setuptools-test`.
243
244
245 .. _advanced-usage:
246
247 Advanced usage
248 ==============
249
250 To provide command line options to the ``dh_virtualenv`` step,
251 use debhelper's override mechanism.
252
253 The following ``debian/rules`` will provide *http://example.com* as
254 an additional source of Python packages:
255
256 .. code-block:: make
257
258 #!/usr/bin/make -f
259
260 %:
261 dh $@ --with python-virtualenv
262
263 override_dh_virtualenv:
264 dh_virtualenv --extra-index-url http://example.com
265
266
267 pbuilder and dh-virtualenv
268 ==========================
269
270 Building your Debian package in a pbuilder_ environment can help to ensure
271 proper dependencies and repeatable builds. However, precisely because pbuilder
272 creates its own build environment, build failues can be much more difficult to
273 understand and troubleshoot. This is especially true when there is a pip error
274 inside the pbuilder environment. For that reason, make sure that you can build
275 your Debian package successfully outside of a pbuilder environment before
276 trying to build it inside.
277
278 With those caveats, here are some tips for making pip and dh_virtual work
279 inside pbuilder.
280
281 If you want pip to retrieve packages from the network, you need to
282 add ``USENETWORK=yes`` to your /etc/pbuilderrc or ~/.pbuilderrc file.
283
284 pip has several options that can be used to make it more compatible
285 with pbuilder.
286
287 Use ``--no-cache-dir`` to stop creating wheels in your home directory,
288 which will fail when running in a pbuilder environment, because
289 pbuilder sets the HOME environment variable to "/nonexistent".
290
291 Use ``--no-deps`` to make pip builds more repeatable_.
292
293 Use ``--ignore-installed`` to ensure that pip installs every package in
294 ``requirements.txt`` in the virtualenv. This option is especially important if
295 you are using the --system-site-packages option in your virtualenv.
296
297 Here's an example of how to use these arguments in your ``rules`` file.
298
299 .. code-block:: make
300
301 override_dh_virtualenv:
302 dh_virtualenv \
303 --extra-pip-arg "--ignore-installed" \
304 --extra-pip-arg "--no-deps" \
305 --extra-pip-arg "--no-cache-dir"
306
307 .. _pbuilder: https://wiki.ubuntu.com/PbuilderHowto
308
309 .. _repeatable: https://pip.readthedocs.org/en/stable/user_guide.html#ensuring-repeatability
310
311
312 Experimental buildsystem support
313 ================================
314
315 .. important::
316
317 This section describes a completely experimental
318 functionality of dh-virtualenv.
319
320 Starting with version 0.9 of dh-virtualenv, there is a buildsystem alternative.
321 The main difference in use is that instead of the ``--with python-virtualenv``
322 option, ``--buildsystem=dh_virtualenv`` is passed to debhelper. The ``debian rules``
323 file should look like this:
324
325 .. code-block:: make
326
327 #!/usr/bin/make -f
328
329 %:
330 dh $@ --buildsystem=dh_virtualenv
331
332 Using the buildsystem instead of the part of the sequence (in other
333 words, instead of the ``--with python-virtualenv``) one can get more
334 flexibility into the build process.
335
336 Flexibility comes from the fact that buildsystem will have individual
337 steps for configure, build, test and install and those can be
338 overridden by adding ``override_dh_auto_<STEP>`` target into the
339 ``debian/rules`` file. For example:
340
341 .. code-block:: make
342
343 #!/usr/bin/make -f
344
345 %:
346 dh $@ --buildsystem=dh_virtualenv
347
348 override_dh_auto_test:
349 py.test test/
350
351 In addition the separation of build and install steps makes it
352 possible to use ``debian/install`` files to include built files into
353 the Debian package. This is not possible with the sequencer addition.
354
355 The build system honors the :envvar:`DH_VIRTUALENV_INSTALL_ROOT`
356 environment variable. Following other environment variables can be
357 used to customise the functionality:
358
359 .. envvar:: DH_VIRTUALENV_ARGUMENTS
360
361 Pass given extra arguments to the ``virtualenv`` command
362
363 For example:
364
365 .. code-block:: make
366
367 export DH_VIRTUALENV_ARGUMENTS="--no-site-packages --always-copy"
368
369 The default is to create the virtual environment with
370 ``--no-site-packages``.
371
372 .. envvar:: DH_VIRTUALENV_INSTALL_SUFFIX
373
374 Override the default virtualenv name, instead of source package name.
375
376 For example:
377
378 .. code-block::make
379
380 export DH_VIRTUALENV_INSTALL_SUFFIX=venv
381
382 .. envvar:: DH_REQUIREMENTS_FILE
383
384 .. versionadded:: 1.0
385
386 Override the location of requirements file. See :option:`--requirements`.
387
388 .. envvar:: DH_UPGRADE_PIP
389
390 .. versionadded:: 1.0
391
392 Force upgrade of the ``pip`` tool by setting
393 :envvar:`DH_UPGRADE_PIP` to empty (latest version) or specific
394 version. For example:
395
396 .. code-block::make
397
398 export DH_UPGRADE_PIP=8.1.2
399
400 .. envvar:: DH_UPGRADE_SETUPTOOLS
401
402 .. versionadded:: 1.0
403
404 Force upgrade of setuptools by setting
405 :envvar:`DH_UPGRADE_SETUPTOOLS` to empty (latest version) or
406 specific version.
407
408 .. envvar:: DH_UPGRADE_WHEEL
409
410 .. versionadded:: 1.0
411
412 Force upgrade of wheel by setting ``DH_UPGRADE_WHEEL`` to empty
413 (latest version) or specific version.
414
415 .. envvar:: DH_PIP_EXTRA_ARGS
416
417 .. versionadded:: 1.0
418
419 Pass additional parameters to the ``pip`` command. For example:
420
421 .. code-block:: make
422
423 export DH_PIP_EXTRA_ARGS="--no-index --find-links=./requirements/wheels"