Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54bb1e6

Browse files
committed
Improve documentation of packaging hooks
1 parent 3e425ac commit 54bb1e6

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

Doc/packaging/commandhooks.rst

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
.. TODO integrate this in commandref and configfile
22
3+
.. _packaging-command-hooks:
4+
35
=============
46
Command hooks
57
=============
68

79
Packaging provides a way of extending its commands by the use of pre- and
8-
post- command hooks. The hooks are simple Python functions (or any callable
9-
objects) and are specified in the config file using their full qualified names.
10-
The pre-hooks are run after the command is finalized (its options are
11-
processed), but before it is run. The post-hooks are run after the command
12-
itself. Both types of hooks receive an instance of the command object.
10+
post-command hooks. Hooks are Python functions (or any callable object) that
11+
take a command object as argument. They're specified in :ref:`config files
12+
<packaging-config-filenames>` using their fully qualified names. After a
13+
command is finalized (its options are processed), the pre-command hooks are
14+
executed, then the command itself is run, and finally the post-command hooks are
15+
executed.
1316

1417
See also global setup hooks in :ref:`setupcfg-spec`.
1518

1619

17-
Sample usage of hooks
18-
=====================
20+
.. _packaging-finding-hooks:
21+
22+
Finding hooks
23+
=============
1924

20-
Firstly, you need to make sure your hook is present in the path. This is usually
21-
done by dropping them to the same folder where `setup.py` file lives ::
25+
As a hook is configured with a Python dotted name, it must either be defined in
26+
a module installed on the system, or in a module present in the project
27+
directory, where the :file:`setup.cfg` file lives::
2228

23-
# file: myhooks.py
24-
def my_install_hook(install_cmd):
25-
print "Oh la la! Someone is installing my project!"
29+
# file: _setuphooks.py
2630

27-
Then, you need to point to it in your `setup.cfg` file, under the appropriate
28-
command section ::
31+
def hook(install_cmd):
32+
metadata = install_cmd.dist.metadata
33+
print('Hooked while installing %r %s!' % (metadata['Name'],
34+
metadata['Version']))
35+
36+
Then you need to configure it in :file:`setup.cfg`::
2937

3038
[install_dist]
31-
pre-hook.project = myhooks.my_install_hook
39+
pre-hook.a = _setuphooks.hook
40+
41+
Packaging will add the project directory to :data:`sys.path` and find the
42+
``_setuphooks`` module.
3243

33-
The hooks defined in different config files (system-wide, user-wide and
34-
package-wide) do not override each other as long as they are specified with
35-
different aliases (additional names after the dot). The alias in the example
36-
above is ``project``.
44+
Hooks defined in different config files (system-wide, user-wide and
45+
project-wide) do not override each other as long as they are specified with
46+
different aliases (additional names after the dot). The alias in the example
47+
above is ``a``.

Doc/packaging/setupcfg.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,20 @@ compilers
178178

179179
setup_hooks
180180
Defines a list of callables to be called right after the :file:`setup.cfg`
181-
file is read, before any other processing. The callables are executed in the
181+
file is read, before any other processing. Each value is a Python dotted
182+
name to an object, which has to be defined in a module present in the project
183+
directory alonside :file:`setup.cfg` or on Python's :data:`sys.path` (see
184+
:ref:`packaging-finding-hooks`). The callables are executed in the
182185
order they're found in the file; if one of them cannot be found, tools should
183186
not stop, but for example produce a warning and continue with the next line.
184187
Each callable receives the configuration as a dictionary (keys are
185188
:file:`setup.cfg` sections, values are dictionaries of fields) and can make
186-
any changes to it. *optional*, *multi*
189+
any change to it. *optional*, *multi*
187190

188191
Example::
189192

190193
[global]
191-
setup_hooks = package.setup.customize_dist
194+
setup_hooks = _setuphooks.customize_config
192195

193196

194197
Metadata

0 commit comments

Comments
 (0)