~bzr/bzr-email/trunk

16.1.3 by John Arbash Meinel
Move guts into another file to improve startup time, fix bug when old revid is None.
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1 by Robert Collins
Start working toward publish - get a publishing_root option
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
39 by Robert Collins
Draft support for mailing on push/pull.
17
"""Sending emails for commits and branch changes.
27 by Robert Collins
Documentation overhaul.
18
19
To have bzr send an email you need to configure an address to send mail
20
to for that branch. To do this set the configuration option ``post_commit_to``
21
and the address to send the mail from is read from the configuration option
22
``post_commit_sender`` (if not supplied defaults to the email address reported
23
by ``bzr whoami``).
24
40.1.1 by James Teh
Clarify documentation about setting difflimit to 0 to disable sending of diffs.
25
By default, the diff for the commit will be included in the email if the
26
length is less than 1000 lines. This limit can be changed by setting the
27
configuration option 'post_commit_difflimit' to the number of lines you wish
28
it to be limited to. Set it to 0 to unconditionally disable sending of diffs.
27 by Robert Collins
Documentation overhaul.
29
39 by Robert Collins
Draft support for mailing on push/pull.
30
By default bzr-email only emails when a commit occurs, not when a push or
31
pull operation occurs. To email on push or pull set post_commit_push_pull=True
32
in the configuration.
33
27 by Robert Collins
Documentation overhaul.
34
If you are using a bzr release from before 0.15, you need to manually tell
35
bzr about the commit action, by setting
36
post_commit=bzrlib.plugins.email.post_commit in bazaar.conf or locations.conf.
37
38
The URL of the branch is determined from the following checks (in order):
39
 - If the configuration value 'post_commit_url' is set, it is used.
40
 - If the configuration value 'public_branch' is set, it is used.
41
 - The URL of the branch itself.
42
43
Setting public_branch is highly recommended if you commit via a protocol which
35 by Jelmer Vernooij
Fix typo: pricate -> private.
44
has a private address (e.g. bzr+ssh but anonymous access might be bzr:// or
27 by Robert Collins
Documentation overhaul.
45
http://).
46
47
How emails are sent is determined by the value of the configuration option
48
'post_commit_mailer':
49
 - Unset: use ``/usr/bin/mail``.
50
 - ``smtplib``: Use python's smtplib to send the mail. If you use 'smtplib' you
51
   can also configure the settings "smtp_server=host[:port]",
52
   "smtp_username=userid", "smtp_password". If "smtp_username" is set but
36 by Jelmer Vernooij
Fix another typo pointed out by Elmo.
53
   "smtp_password" is not, you will be prompted for a password.
27 by Robert Collins
Documentation overhaul.
54
55
   Also, if using 'smtplib', the messages will be sent as a UTF-8 text message,
56
   with a 8-bit text diff attached (rather than all-as-one). Work has also been
57
   done to make sure usernames do not have to be ascii.
58
 - Any other value: Run the value expecting it to behave like ``/usr/bin/mail``
59
   - in particular supporting the -s and -a options.
60
41.1.3 by Steve Langasek
change option name to 'revision_mail_headers' per Robert, and document the
61
When using smtplib, you can specify additional headers to be included in the
42 by Robert Collins
Merge patch from Steve Langasek adding support for arbitrary headers on revision notification emails.
62
mail by setting the 'revision_mail_headers' configuration option - something like::
41.1.3 by Steve Langasek
change option name to 'revision_mail_headers' per Robert, and document the
63
42 by Robert Collins
Merge patch from Steve Langasek adding support for arbitrary headers on revision notification emails.
64
  revision_mail_headers=X-Cheese: to the rescue!
16.1.3 by John Arbash Meinel
Move guts into another file to improve startup time, fix bug when old revid is None.
65
"""
4 by Robert Collins
basics are all there
66
57 by Jelmer Vernooij
Install hook lazily, remove unused import
67
from __future__ import absolute_import
68
55 by Jelmer Vernooij
Use config stacks.
69
from bzrlib.config import option_registry
22 by John Arbash Meinel
Cleanup import logic, and use lazy importing
70
from bzrlib.lazy_import import lazy_import
71
72
# lazy_import emailer so that it doesn't get loaded if it isn't used
73
lazy_import(globals(), """\
23 by John Arbash Meinel
Can't have a lazy imported object with the identical name as the actual module.
74
from bzrlib.plugins.email import emailer as _emailer
22 by John Arbash Meinel
Cleanup import logic, and use lazy importing
75
""")
4 by Robert Collins
basics are all there
76
3 by Robert Collins
convert the switch basics to an email plugin
77
78
def post_commit(branch, revision_id):
27 by Robert Collins
Documentation overhaul.
79
    """This is the post_commit hook that should get run after commit."""
55 by Jelmer Vernooij
Use config stacks.
80
    _emailer.EmailSender(branch, revision_id, branch.get_config_stack()).send_maybe()
3 by Robert Collins
convert the switch basics to an email plugin
81
82
19 by Robert Collins
Update email plugin to support bzr 0.15 branch hooks.
83
def branch_commit_hook(local, master, old_revno, old_revid, new_revno, new_revid):
21 by Robert Collins
Merge up with HEAD.
84
    """This is the post_commit hook that runs after commit."""
55 by Jelmer Vernooij
Use config stacks.
85
    _emailer.EmailSender(master, new_revid, master.get_config_stack(),
24 by John Arbash Meinel
Switch to using a local repository if available,
86
                         local_branch=local).send_maybe()
19 by Robert Collins
Update email plugin to support bzr 0.15 branch hooks.
87
88
39 by Robert Collins
Draft support for mailing on push/pull.
89
def branch_post_change_hook(params):
90
    """This is the post_change_branch_tip hook."""
91
    # (branch, old_revno, new_revno, old_revid, new_revid)
92
    _emailer.EmailSender(params.branch, params.new_revid,
55 by Jelmer Vernooij
Use config stacks.
93
        params.branch.get_config_stack(), local_branch=None, op='change').send_maybe()
39 by Robert Collins
Draft support for mailing on push/pull.
94
95
1 by Robert Collins
Start working toward publish - get a publishing_root option
96
def test_suite():
97
    from unittest import TestSuite
22 by John Arbash Meinel
Cleanup import logic, and use lazy importing
98
    import bzrlib.plugins.email.tests
1 by Robert Collins
Start working toward publish - get a publishing_root option
99
    result = TestSuite()
3 by Robert Collins
convert the switch basics to an email plugin
100
    result.addTest(bzrlib.plugins.email.tests.test_suite())
1 by Robert Collins
Start working toward publish - get a publishing_root option
101
    return result
102
19 by Robert Collins
Update email plugin to support bzr 0.15 branch hooks.
103
55 by Jelmer Vernooij
Use config stacks.
104
option_registry.register_lazy("post_commit_body",
105
    "bzrlib.plugins.email.emailer", "opt_post_commit_body")
106
option_registry.register_lazy("post_commit_subject",
107
    "bzrlib.plugins.email.emailer", "opt_post_commit_subject")
108
option_registry.register_lazy("post_commit_log_format",
109
    "bzrlib.plugins.email.emailer", "opt_post_commit_log_format")
110
option_registry.register_lazy("post_commit_difflimit",
111
    "bzrlib.plugins.email.emailer", "opt_post_commit_difflimit")
112
option_registry.register_lazy("post_commit_push_pull",
113
    "bzrlib.plugins.email.emailer", "opt_post_commit_push_pull")
114
option_registry.register_lazy("post_commit_diffoptions",
115
    "bzrlib.plugins.email.emailer", "opt_post_commit_diffoptions")
116
option_registry.register_lazy("post_commit_sender",
117
    "bzrlib.plugins.email.emailer", "opt_post_commit_sender")
118
option_registry.register_lazy("post_commit_to",
119
    "bzrlib.plugins.email.emailer", "opt_post_commit_to")
120
option_registry.register_lazy("post_commit_mailer",
121
    "bzrlib.plugins.email.emailer", "opt_post_commit_mailer")
122
option_registry.register_lazy("revision_mail_headers",
123
    "bzrlib.plugins.email.emailer", "opt_revision_mail_headers")
57 by Jelmer Vernooij
Install hook lazily, remove unused import
124
125
try:
126
    from bzrlib.hooks import install_lazy_named_hook
127
except ImportError:
128
    from bzrlib.branch import Branch
129
    Branch.hooks.install_named_hook('post_commit', branch_commit_hook, 'bzr-email')
130
    Branch.hooks.install_named_hook('post_change_branch_tip', branch_post_change_hook, 'bzr-email')
131
else:
132
    install_lazy_named_hook("bzrlib.branch", "Branch.hooks", 'post_commit',
133
        branch_commit_hook, 'bzr-email')
134
    install_lazy_named_hook("bzrlib.branch", "Branch.hooks",
135
        'post_change_branch_tip', branch_post_change_hook, 'bzr-email')