source: mainline/contrib/bazaar/bzreml/__init__.py@ 798105ca

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

Repository.iter_reverse_revision_history() method is deprecated and will be removed eventually
reimplement the wrapper around Graph.iter_lefthand_ancestry by our means

  • Property mode set to 100644
File size: 7.1 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 iter_reverse_revision_history(repository, revision_id):
105 """Iterate backwards through revision ids in the lefthand history"""
106
107 graph = repository.get_graph()
108 stop_revisions = (None, _mod_revision.NULL_REVISION)
109 return graph.iter_lefthand_ancestry(revision_id, stop_revisions)
110
111def revision_sequence(branch, revision_old_id, revision_new_id):
112 """Calculate a sequence of revisions"""
113
114 for revision_ac_id in iter_reverse_revision_history(branch.repository, revision_new_id):
115 if (revision_ac_id == revision_old_id):
116 break
117
118 yield revision_ac_id
119
120def send_email(branch, revision_old_id, revision_new_id, config):
121 """Send the email"""
122
123 if (config_to(config) is not None):
124 branch.lock_read()
125 branch.repository.lock_read()
126 try:
127 revision_prev_id = revision_old_id
128
129 for revision_ac_id in reversed(list(revision_sequence(branch, revision_old_id, revision_new_id))):
130 body = StringIO()
131
132 revision_ac = branch.repository.get_revision(revision_ac_id)
133 revision_ac_no = branch.revision_id_to_revno(revision_ac_id)
134
135 committer = revision_ac.committer
136 authors = revision_ac.get_apparent_authors()
137 date = time.strftime("%Y-%m-%d %H:%M:%S %Z (%a, %d %b %Y)", time.localtime(revision_ac.timestamp))
138
139 if (authors != [committer]):
140 body.write("Author: %s\n" % ", ".join(authors))
141
142 body.write("Committer: %s\n" % committer)
143 body.write("Date: %s\n" % date)
144 body.write("New Revision: %s%s\n" % (revision_ac_no, merge_marker(revision_ac)))
145 body.write("New Id: %s\n" % revision_ac_id)
146 for parent_id in revision_ac.parent_ids:
147 body.write("Parent: %s\n" % parent_id)
148
149 body.write("\n")
150
151 commit_message = None
152 body.write("Log:\n")
153 if (not revision_ac.message):
154 body.write("(empty)\n")
155 else:
156 log = revision_ac.message.rstrip("\n\r")
157 for line in log.split("\n"):
158 body.write("%s\n" % line)
159 if (commit_message == None):
160 commit_message = line
161
162 if (commit_message == None):
163 commit_message = "(empty)"
164
165 body.write("\n")
166
167 tree_prev = branch.repository.revision_tree(revision_prev_id)
168 tree_ac = branch.repository.revision_tree(revision_ac_id)
169
170 delta = tree_ac.changes_from(tree_prev)
171
172 if (len(delta.added) > 0):
173 body.write("Added:\n")
174 for item in delta.added:
175 body.write(" %s\n" % item[0])
176
177 if (len(delta.removed) > 0):
178 body.write("Removed:\n")
179 for item in delta.removed:
180 body.write(" %s\n" % item[0])
181
182 if (len(delta.renamed) > 0):
183 body.write("Renamed:\n")
184 for item in delta.renamed:
185 body.write(" %s -> %s\n" % (item[0], item[1]))
186
187 if (len(delta.kind_changed) > 0):
188 body.write("Changed:\n")
189 for item in delta.kind_changed:
190 body.write(" %s\n" % item[0])
191
192 if (len(delta.modified) > 0):
193 body.write("Modified:\n")
194 for item in delta.modified:
195 body.write(" %s\n" % item[0])
196
197 body.write("\n")
198
199 tree_prev.lock_read()
200 try:
201 tree_ac.lock_read()
202 try:
203 diff = DiffTree.from_trees_options(tree_prev, tree_ac, body, "utf8", None, "", "", None)
204 diff.show_diff(None, None)
205 finally:
206 tree_ac.unlock()
207 finally:
208 tree_prev.unlock()
209
210 subject = "r%d - %s" % (revision_ac_no, commit_message)
211 send_smtp("localhost", config_sender(config), config_to(config), subject, body.getvalue())
212
213 revision_prev_id = revision_ac_id
214
215 finally:
216 branch.repository.unlock()
217 branch.unlock()
218
219def branch_post_change_hook(params):
220 """post_change_branch_tip hook"""
221
222 send_email(params.branch, params.old_revid, params.new_revid, params.branch.get_config())
223
224install_named_hook = getattr(Branch.hooks, "install_named_hook", None)
225install_named_hook("post_change_branch_tip", branch_post_change_hook, "bzreml")
Note: See TracBrowser for help on using the repository browser.