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
|
#
# Copyright (c) 2009 Martin Decky
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
"""Send emails for commits and repository changes."""
#
# Inspired by bzr-email plugin (copyright (c) 2005 - 2007 Canonical Ltd.,
# distributed under GPL), but no code is shared with the original plugin.
#
# Configuration options:
# - post_commit_to (destination email address for the commit emails)
# - post_commit_sender (source email address for the commit emails)
#
import smtplib
import time
from StringIO import StringIO
from email.utils import parseaddr
from email.utils import formatdate
from email.utils import make_msgid
from email.Header import Header
from email.Message import Message
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from bzrlib import errors
from bzrlib import revision
from bzrlib import __version__ as bzrlib_version
from bzrlib.branch import Branch
from bzrlib.diff import DiffTree
def send_smtp(server, sender, to, subject, body):
"""Send SMTP message"""
connection = smtplib.SMTP()
try:
connection.connect(server)
except socket.error, err:
raise errors.SocketConnectionError(host = server, msg = "Unable to connect to SMTP server", orig_error = err)
sender_user, sender_email = parseaddr(sender)
payload = MIMEText(body.encode("utf-8"), "plain", "utf-8")
msg = MIMEMultipart()
msg["From"] = "%s <%s>" % (Header(unicode(sender_user)), sender_email)
msg["User-Agent"] = "bzr/%s" % bzrlib_version
msg["Date"] = formatdate(None, True)
msg["Message-Id"] = make_msgid("bzr")
msg["To"] = to
msg["Subject"] = Header(subject)
msg.attach(payload)
connection.sendmail(sender, [to], msg.as_string())
def config_to(config):
"""Address the mail should go to"""
return config.get_user_option("post_commit_to")
def config_sender(config):
"""Address the email should be sent from"""
result = config.get_user_option("post_commit_sender")
if (result is None):
result = config.username()
return result
def merge_marker(revision):
if (len(revision.parent_ids) > 1):
return " [merge]"
return ""
def revision_sequence(branch, revision_old_id, revision_new_id):
"""Calculate a sequence of revisions"""
for revision_ac_id in branch.repository.iter_reverse_revision_history(revision_new_id):
if (revision_ac_id == revision_old_id):
break
yield revision_ac_id
def send_email(branch, revision_old_id, revision_new_id, config):
"""Send the email"""
if (config_to(config) is not None):
branch.lock_read()
branch.repository.lock_read()
try:
body = StringIO()
for revision_ac_id in revision_sequence(branch, revision_old_id, revision_new_id):
revision_ac = branch.repository.get_revision(revision_ac_id)
revision_ac_no = branch.revision_id_to_revno(revision_ac_id)
committer = revision_ac.committer
authors = revision_ac.get_apparent_authors()
date = time.strftime("%Y-%m-%d %H:%M:%S %Z (%a, %d %b %Y)", time.localtime(revision_ac.timestamp))
if (authors != [committer]):
body.write("Author: %s\n" % ", ".join(authors))
body.write("Committer: %s\n" % committer)
body.write("Date: %s\n" % date)
body.write("New Revision: %s%s\n" % (revision_ac_no, merge_marker(revision_ac)))
body.write("New Id: %s\n" % revision_ac_id)
for parent_id in revision_ac.parent_ids:
body.write("Parent: %s\n" % parent_id)
body.write("\n")
commit_message = ""
body.write("Log:\n")
if (not revision_ac.message):
body.write("(empty)\n")
else:
log = revision_ac.message.rstrip("\n\r")
for line in log.split("\n"):
body.write("%s\n" % line)
if (commit_message == ""):
commit_message = line
if (commit_message == ""):
commit_message = "(empty)"
body.write("\n")
tree_old = branch.repository.revision_tree(revision_old_id)
tree_new = branch.repository.revision_tree(revision_new_id)
revision_new_no = branch.revision_id_to_revno(revision_new_id)
delta = tree_new.changes_from(tree_old)
if (len(delta.added) > 0):
body.write("Added:\n")
for item in delta.added:
body.write(" %s\n" % item[0])
if (len(delta.removed) > 0):
body.write("Removed:\n")
for item in delta.removed:
body.write(" %s\n" % item[0])
if (len(delta.renamed) > 0):
body.write("Renamed:\n")
for item in delta.renamed:
body.write(" %s -> %s\n" % (item[0], item[1]))
if (len(delta.kind_changed) > 0):
body.write("Changed:\n")
for item in delta.kind_changed:
body.write(" %s\n" % item[0])
if (len(delta.modified) > 0):
body.write("Modified:\n")
for item in delta.modified:
body.write(" %s\n" % item[0])
body.write("\n")
tree_old.lock_read()
try:
tree_new.lock_read()
try:
diff = DiffTree.from_trees_options(tree_old, tree_new, body, "utf8", None, "", "", None)
diff.show_diff(None, None)
finally:
tree_new.unlock()
finally:
tree_old.unlock()
subject = "r%d - %s" % (revision_new_no, commit_message)
send_smtp("localhost", config_sender(config), config_to(config), subject, body.getvalue())
finally:
branch.repository.unlock()
branch.unlock()
def branch_post_change_hook(params):
"""post_change_branch_tip hook"""
send_email(params.branch, params.old_revid, params.new_revid, params.branch.get_config())
install_named_hook = getattr(Branch.hooks, "install_named_hook", None)
install_named_hook("post_change_branch_tip", branch_post_change_hook, "bzreml")
|