summaryrefslogtreecommitdiff
path: root/src/mailman/docs/hooks.txt
blob: 14dc76667e18bc8c54a4f5165ca581a978a4efa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
=====
Hooks
=====

Mailman defines two initialization hooks, one which is run early in the
initialization process and the other run late in the initialization process.
Hooks name an importable callable so it must be accessible on sys.path.

    >>> import os, sys
    >>> from mailman.config import config
    >>> config_directory = os.path.dirname(config.filename)
    >>> sys.path.insert(0, config_directory)

    >>> hook_path = os.path.join(config_directory, 'hooks.py')
    >>> with open(hook_path, 'w') as fp:
    ...     print >> fp, """\
    ... counter = 1
    ... def pre_hook():
    ...     global counter
    ...     print 'pre-hook:', counter
    ...     counter += 1
    ...
    ... def post_hook():
    ...     global counter
    ...     print 'post-hook:', counter
    ...     counter += 1
    ... """
    >>> fp.close()


Pre-hook
========

We can set the pre-hook in the configuration file.

    >>> config_path = os.path.join(config_directory, 'hooks.cfg')
    >>> with open(config_path, 'w') as fp:
    ...     print >> fp, """\
    ... [meta]
    ... extends: test.cfg
    ...
    ... [mailman]
    ... pre_hook: hooks.pre_hook
    ... """

The hooks are run in the second and third steps of initialization.  However,
we can't run those initialization steps in process, so call a command line
script that will produce no output to force the hooks to run.

    >>> import subprocess
    >>> def call():
    ...     proc = subprocess.Popen(
    ...         'bin/mailman lists --domain ignore -q'.split(),
    ...         cwd='../..',  # testrunner runs from ./parts/test
    ...         env=dict(MAILMAN_CONFIG_FILE=config_path,
    ...                  PYTHONPATH=config_directory),
    ...         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    ...     stdout, stderr = proc.communicate()
    ...     assert proc.returncode == 0, stderr
    ...     print stdout

    >>> call()
    pre-hook: 1
    <BLANKLINE>

    >>> os.remove(config_path)


Post-hook
=========

We can set the post-hook in the configuration file.

    >>> with open(config_path, 'w') as fp:
    ...     print >> fp, """\
    ... [meta]
    ... extends: test.cfg
    ...
    ... [mailman]
    ... post_hook: hooks.post_hook
    ... """

    >>> call()
    post-hook: 1
    <BLANKLINE>

    >>> os.remove(config_path)


Running both hooks
==================

We can set the pre- and post-hooks in the configuration file.

    >>> with open(config_path, 'w') as fp:
    ...     print >> fp, """\
    ... [meta]
    ... extends: test.cfg
    ...
    ... [mailman]
    ... pre_hook: hooks.pre_hook
    ... post_hook: hooks.post_hook
    ... """

    >>> call()
    pre-hook: 1
    post-hook: 2
    <BLANKLINE>