source: mainline/contrib/bazaar/bzreml/__init__.py@ 1f419ecf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1f419ecf was 1f419ecf, checked in by Martin Decky <martin@…>, 14 years ago

update Bazaar email plugin
(show the first line of the commit log in the subject instead of the list of touched files, provide a global overview of all touched files, not a separate overview for each parent tree)

  • Property mode set to 100644
File size: 6.8 KB
Line 
1#
2# Copyright (c) 2009 Martin Decky
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# - Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# - Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# - The name of the author may not be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28
29"""Send emails for commits and repository changes."""
30
31#
32# Inspired by bzr-email plugin (copyright (c) 2005 - 2007 Canonical Ltd.,
33# distributed under GPL), but no code is shared with the original plugin.
34#
35# Configuration options:
36# - post_commit_to (destination email address for the commit emails)
37# - post_commit_sender (source email address for the commit emails)
38#
39
40import smtplib
41import time
42
43from StringIO import StringIO
44
45from email.utils import parseaddr
46from email.utils import formatdate
47from email.utils import make_msgid
48from email.Header import Header
49from email.Message import Message
50from email.mime.multipart import MIMEMultipart
51from email.mime.text import MIMEText
52
53from bzrlib import errors
54from bzrlib import revision
55from bzrlib import __version__ as bzrlib_version
56
57from bzrlib.branch import Branch
58from bzrlib.diff import DiffTree
59
60def send_smtp(server, sender, to, subject, body):
61 """Send SMTP message"""
62
63 connection = smtplib.SMTP()
64
65 try:
66 connection.connect(server)
67 except socket.error, err:
68 raise errors.SocketConnectionError(host = server, msg = "Unable to connect to SMTP server", orig_error = err)
69
70 sender_user, sender_email = parseaddr(sender)
71 payload = MIMEText(body.encode("utf-8"), "plain", "utf-8")
72
73 msg = MIMEMultipart()
74 msg["From"] = "%s <%s>" % (Header(unicode(sender_user)), sender_email)
75 msg["User-Agent"] = "bzr/%s" % bzrlib_version
76 msg["Date"] = formatdate(None, True)
77 msg["Message-Id"] = make_msgid("bzr")
78 msg["To"] = to
79 msg["Subject"] = Header(subject)
80 msg.attach(payload)
81
82 connection.sendmail(sender, [to], msg.as_string())
83
84def config_to(config):
85 """Address the mail should go to"""
86
87 return config.get_user_option("post_commit_to")
88
89def config_sender(config):
90 """Address the email should be sent from"""
91
92 result = config.get_user_option("post_commit_sender")
93 if (result is None):
94 result = config.username()
95
96 return result
97
98def merge_marker(revision):
99 if (len(revision.parent_ids) > 1):
100 return " [merge]"
101
102 return ""
103
104def revision_sequence(branch, revision_old_id, revision_new_id):
105 """Calculate a sequence of revisions"""
106
107 for revision_ac_id in branch.repository.iter_reverse_revision_history(revision_new_id):
108 if (revision_ac_id == revision_old_id):
109 break
110 yield revision_ac_id
111
112def send_email(branch, revision_old_id, revision_new_id, config):
113 """Send the email"""
114
115 if (config_to(config) is not None):
116 branch.lock_read()
117 branch.repository.lock_read()
118 try:
119 body = StringIO()
120
121 for revision_ac_id in revision_sequence(branch, revision_old_id, revision_new_id):
122 revision_ac = branch.repository.get_revision(revision_ac_id)
123 revision_ac_no = branch.revision_id_to_revno(revision_ac_id)
124
125 committer = revision_ac.committer
126 authors = revision_ac.get_apparent_authors()
127 date = time.strftime("%Y-%m-%d %H:%M:%S %Z (%a, %d %b %Y)", time.localtime(revision_ac.timestamp))
128
129 if (authors != [committer]):
130 body.write("Author: %s\n" % ", ".join(authors))
131
132 body.write("Committer: %s\n" % committer)
133 body.write("Date: %s\n" % date)
134 body.write("New Revision: %s%s\n" % (revision_ac_no, merge_marker(revision_ac)))
135 body.write("New Id: %s\n" % revision_ac_id)
136 for parent_id in revision_ac.parent_ids:
137 body.write("Parent: %s\n" % parent_id)
138
139 body.write("\n")
140
141 commit_message = ""
142 body.write("Log:\n")
143 if (not revision_ac.message):
144 body.write("(empty)\n")
145 else:
146 log = revision_ac.message.rstrip("\n\r")
147 for line in log.split("\n"):
148 body.write("%s\n" % line)
149 if (commit_message == ""):
150 commit_message = line
151
152 if (commit_message == ""):
153 commit_message = "(empty)"
154
155 body.write("\n")
156
157 tree_old = branch.repository.revision_tree(revision_old_id)
158 tree_new = branch.repository.revision_tree(revision_new_id)
159
160 revision_new_no = branch.revision_id_to_revno(revision_new_id)
161 delta = tree_new.changes_from(tree_old)
162
163 if (len(delta.added) > 0):
164 body.write("Added:\n")
165 for item in delta.added:
166 body.write(" %s\n" % item[0])
167
168 if (len(delta.removed) > 0):
169 body.write("Removed:\n")
170 for item in delta.removed:
171 body.write(" %s\n" % item[0])
172
173 if (len(delta.renamed) > 0):
174 body.write("Renamed:\n")
175 for item in delta.renamed:
176 body.write(" %s -> %s\n" % (item[0], item[1]))
177
178 if (len(delta.kind_changed) > 0):
179 body.write("Changed:\n")
180 for item in delta.kind_changed:
181 body.write(" %s\n" % item[0])
182
183 if (len(delta.modified) > 0):
184 body.write("Modified:\n")
185 for item in delta.modified:
186 body.write(" %s\n" % item[0])
187
188 body.write("\n")
189
190 tree_old.lock_read()
191 try:
192 tree_new.lock_read()
193 try:
194 diff = DiffTree.from_trees_options(tree_old, tree_new, body, "utf8", None, "", "", None)
195 diff.show_diff(None, None)
196 finally:
197 tree_new.unlock()
198 finally:
199 tree_old.unlock()
200
201 subject = "r%d - %s" % (revision_new_no, commit_message)
202
203 send_smtp("localhost", config_sender(config), config_to(config), subject, body.getvalue())
204 finally:
205 branch.repository.unlock()
206 branch.unlock()
207
208def branch_post_change_hook(params):
209 """post_change_branch_tip hook"""
210
211 send_email(params.branch, params.old_revid, params.new_revid, params.branch.get_config())
212
213install_named_hook = getattr(Branch.hooks, "install_named_hook", None)
214install_named_hook("post_change_branch_tip", branch_post_change_hook, "bzreml")
Note: See TracBrowser for help on using the repository browser.