~jteh/bzr-email/pushPullEnhancements

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Copyright (C) 2005-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import errno
import subprocess
import tempfile

from bzrlib import (
    errors,
    revision as _mod_revision,
    )

from smtp_connection import SMTPConnection


class EmailSender(object):
    """An email message sender."""

    _smtplib_implementation = SMTPConnection

    def __init__(self, branch, revision_id, config, local_branch=None,
        op='commit'):
        self.config = config
        self.branch = branch
        self.repository = branch.repository
        if (local_branch is not None and
            local_branch.repository.has_revision(revision_id)):
            self.repository = local_branch.repository
        self._revision_id = revision_id
        self.revision = None
        self.revno = None
        self.op = op

    def _setup_revision_and_revno(self):
        if not self._revision_id:
            # We're sending an email for a new branch.
            return
        self.revision = self.repository.get_revision(self._revision_id)
        self.revno = self.branch.revision_id_to_revno(self._revision_id)

    def body(self):
        if not self.revision:
            return self._new_branch_body()
        else:
            return self._revision_body(self.revno, self.revno)

    def _revision_body(self, rev1, rev2):
        from bzrlib import log

        # use 'replace' so that we don't abort if trying to write out
        # in e.g. the default C locale.

        # We must use StringIO.StringIO because we want a Unicode string that
        # we can pass to send_email and have that do the proper encoding.
        from StringIO import StringIO
        outf = StringIO()

        outf.write('At %s\n\n' % self.url())

        log_format = self.config.get_user_option('post_commit_log_format')
        lf = log.log_formatter(log_format or 'long',
                               show_ids=True,
                               to_file=outf
                               )

        if len(self.revision.parent_ids) <= 1:
            # This is not a merge, so we can special case the display of one
            # revision, and not have to encur the show_log overhead.
            lr = log.LogRevision(self.revision, self.revno, 0, None)
            lf.log_revision(lr)
        else:
            # let the show_log code figure out what revisions need to be
            # displayed, as this is a merge
            log.show_log(self.branch,
                         lf,
                         start_revision=rev1,
                         end_revision=rev2,
                         verbose=True
                         )

        return outf.getvalue()

    def _new_branch_body(self):
        return 'New branch at %s' % self.url()

    def get_diff(self):
        """Add the diff from the commit to the output.

        If the diff has more than difflimit lines, it will be skipped.
        """
        difflimit = self.difflimit()
        if not self.revision:
            # This is a new branch, so no diff.
            return

        if not difflimit:
            # No need to compute a diff if we aren't going to display it
            return

        from bzrlib.diff import show_diff_trees
        # optionally show the diff if its smaller than the post_commit_difflimit option
        revid_new = self.revision.revision_id
        if self.revision.parent_ids:
            revid_old = self.revision.parent_ids[0]
            tree_new, tree_old = self.repository.revision_trees((revid_new, revid_old))
        else:
            # revision_trees() doesn't allow None or 'null:' to be passed as a
            # revision. So we need to call revision_tree() twice.
            revid_old = _mod_revision.NULL_REVISION
            tree_new = self.repository.revision_tree(revid_new)
            tree_old = self.repository.revision_tree(revid_old)

        # We can use a cStringIO because show_diff_trees should only write
        # 8-bit strings. It is an error to write a Unicode string here.
        from cStringIO import StringIO
        diff_content = StringIO()
        diff_options = self.config.get_user_option('post_commit_diffoptions')
        show_diff_trees(tree_old, tree_new, diff_content, None, diff_options)
        numlines = diff_content.getvalue().count('\n')+1
        if numlines <= difflimit:
            return diff_content.getvalue()
        else:
            return ("\nDiff too large for email"
                    " (%d lines, the limit is %d).\n"
                    % (numlines, difflimit))

    def difflimit(self):
        """Maximum number of lines of diff to show."""
        result = self.config.get_user_option('post_commit_difflimit')
        if result is None:
            result = 1000
        return int(result)

    def mailer(self):
        """What mail program to use."""
        result = self.config.get_user_option('post_commit_mailer')
        if result is None:
            result = "mail"
        return result

    def _command_line(self):
        cmd = [self.mailer(), '-s', self.subject(), '-a',
                "From: " + self.from_address()]
        to = self.to()
        if isinstance(to, basestring):
            cmd.append(to)
        else:
            cmd.extend(to)
        return cmd

    def to(self):
        """What is the address the mail should go to."""
        return self.config.get_user_option('post_commit_to')

    def url(self):
        """What URL to display in the subject of the mail"""
        url = self.config.get_user_option('post_commit_url')
        if url is None:
            url = self.config.get_user_option('public_branch')
        if url is None:
            url = self.branch.base
        return url

    def from_address(self):
        """What address should I send from."""
        result = self.config.get_user_option('post_commit_sender')
        if result is None:
            result = self.config.username()
        return result

    def extra_headers(self):
        """Additional headers to include when sending."""
        result = {}
        headers = self.config.get_user_option('revision_mail_headers')
        if not headers:
            return
        if type(headers) is not list:
            headers = [headers]
        for line in headers:
            key, value = line.split(": ", 1)
            result[key] = value
        return result

    def send(self):
        """Send the email.

        Depending on the configuration, this will either use smtplib, or it
        will call out to the 'mail' program.
        """
        self.branch.lock_read()
        self.repository.lock_read()
        try:
            # Do this after we have locked, to make things faster.
            self._setup_revision_and_revno()
            mailer = self.mailer()
            if mailer == 'smtplib':
                self._send_using_smtplib()
            else:
                self._send_using_process()
        finally:
            self.repository.unlock()
            self.branch.unlock()

    def _send_using_process(self):
        """Spawn a 'mail' subprocess to send the email."""
        # TODO think up a good test for this, but I think it needs
        # a custom binary shipped with. RBC 20051021
        msgfile = tempfile.NamedTemporaryFile()
        try:
            msgfile.write(self.body().encode('utf8'))
            diff = self.get_diff()
            if diff:
                msgfile.write(diff)
            msgfile.flush()
            msgfile.seek(0)

            process = subprocess.Popen(self._command_line(),
                stdin=msgfile.fileno())

            rc = process.wait()
            if rc != 0:
                raise errors.BzrError("Failed to send email: exit status %s" % (rc,))
        finally:
            msgfile.close()

    def _send_using_smtplib(self):
        """Use python's smtplib to send the email."""
        body = self.body()
        diff = self.get_diff()
        subject = self.subject()
        from_addr = self.from_address()
        to_addrs = self.to()
        if isinstance(to_addrs, basestring):
            to_addrs = [to_addrs]

        smtp = self._smtplib_implementation(self.config)
        if diff:
            smtp.send_text_and_attachment_email(from_addr, to_addrs,
                                                subject, body, diff,
                                                self.diff_filename(),
                                                self.extra_headers())
        else:
            smtp.send_text_email(from_addr, to_addrs, subject, body,
                                 self.extra_headers())

    def should_send(self):
        post_commit_push_pull = self.config.get_user_option(
            'post_commit_push_pull') == 'True'
        if post_commit_push_pull and self.op == 'commit':
            # We will be called again with a push op, send the mail then.
            return False
        if not post_commit_push_pull and self.op != 'commit':
            # Mailing on commit only, and this is a push/pull operation.
            return False
        return bool(self.to() and self.from_address())

    def send_maybe(self):
        if self.should_send():
            self.send()

    def subject(self):
        if self.revno:
            return ("Rev %d: %s in %s" %
                    (self.revno,
                     self.revision.get_summary(),
                     self.url()))
        else:
            return 'New branch: %s' % self.url()

    def diff_filename(self):
        return "patch-%s.diff" % (self.revno,)