|
1 | 1 | .. TODO integrate this in commandref and configfile |
2 | 2 |
|
| 3 | +.. _packaging-command-hooks: |
| 4 | + |
3 | 5 | ============= |
4 | 6 | Command hooks |
5 | 7 | ============= |
6 | 8 |
|
7 | 9 | 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. |
13 | 16 |
|
14 | 17 | See also global setup hooks in :ref:`setupcfg-spec`. |
15 | 18 |
|
16 | 19 |
|
17 | | -Sample usage of hooks |
18 | | -===================== |
| 20 | +.. _packaging-finding-hooks: |
| 21 | + |
| 22 | +Finding hooks |
| 23 | +============= |
19 | 24 |
|
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:: |
22 | 28 |
|
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 |
26 | 30 |
|
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`:: |
29 | 37 |
|
30 | 38 | [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. |
32 | 43 |
|
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``. |
0 commit comments