blob: b261d002703d1a3e6e75dedaa75aa1966aaa9ef4 [file] [log] [blame]
Éric Araujo823759e2011-06-04 18:46:25 +02001.. TODO integrate this in commandref and configfile
2
Éric Araujo54bb1e62011-06-19 21:34:16 +02003.. _packaging-command-hooks:
4
Éric Araujo3a9f58f2011-06-01 20:42:49 +02005=============
6Command hooks
7=============
8
9Packaging provides a way of extending its commands by the use of pre- and
Éric Araujo54bb1e62011-06-19 21:34:16 +020010post-command hooks. Hooks are Python functions (or any callable object) that
11take a command object as argument. They're specified in :ref:`config files
12<packaging-config-filenames>` using their fully qualified names. After a
13command is finalized (its options are processed), the pre-command hooks are
14executed, then the command itself is run, and finally the post-command hooks are
15executed.
Éric Araujo3a9f58f2011-06-01 20:42:49 +020016
Éric Araujo0300b5c2011-06-06 01:54:54 +020017See also global setup hooks in :ref:`setupcfg-spec`.
Éric Araujo823759e2011-06-04 18:46:25 +020018
19
Éric Araujo54bb1e62011-06-19 21:34:16 +020020.. _packaging-finding-hooks:
Éric Araujo3a9f58f2011-06-01 20:42:49 +020021
Éric Araujo54bb1e62011-06-19 21:34:16 +020022Finding hooks
23=============
Éric Araujo3a9f58f2011-06-01 20:42:49 +020024
Éric Araujo54bb1e62011-06-19 21:34:16 +020025As a hook is configured with a Python dotted name, it must either be defined in
26a module installed on the system, or in a module present in the project
27directory, where the :file:`setup.cfg` file lives::
Éric Araujo3a9f58f2011-06-01 20:42:49 +020028
Éric Araujo54bb1e62011-06-19 21:34:16 +020029 # file: _setuphooks.py
30
31 def hook(install_cmd):
32 metadata = install_cmd.dist.metadata
33 print('Hooked while installing %r %s!' % (metadata['Name'],
34 metadata['Version']))
35
36Then you need to configure it in :file:`setup.cfg`::
Éric Araujo3a9f58f2011-06-01 20:42:49 +020037
38 [install_dist]
Éric Araujo54bb1e62011-06-19 21:34:16 +020039 pre-hook.a = _setuphooks.hook
Éric Araujo3a9f58f2011-06-01 20:42:49 +020040
Éric Araujo54bb1e62011-06-19 21:34:16 +020041Packaging will add the project directory to :data:`sys.path` and find the
42``_setuphooks`` module.
43
44Hooks defined in different config files (system-wide, user-wide and
45project-wide) do not override each other as long as they are specified with
46different aliases (additional names after the dot). The alias in the example
47above is ``a``.