| 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 |
|
|---|
| 40 | import smtplib
|
|---|
| 41 | import time
|
|---|
| 42 | import os
|
|---|
| 43 |
|
|---|
| 44 | from StringIO import StringIO
|
|---|
| 45 |
|
|---|
| 46 | from email.utils import parseaddr
|
|---|
| 47 | from email.utils import formatdate
|
|---|
| 48 | from email.utils import make_msgid
|
|---|
| 49 | from email.Header import Header
|
|---|
| 50 | from email.Message import Message
|
|---|
| 51 | from email.mime.multipart import MIMEMultipart
|
|---|
| 52 | from email.mime.text import MIMEText
|
|---|
| 53 |
|
|---|
| 54 | from bzrlib import errors
|
|---|
| 55 | from bzrlib import revision as _mod_revision
|
|---|
| 56 | from bzrlib import __version__ as bzrlib_version
|
|---|
| 57 |
|
|---|
| 58 | from bzrlib.branch import Branch
|
|---|
| 59 | from bzrlib.diff import DiffTree
|
|---|
| 60 |
|
|---|
| 61 | def send_smtp(server, sender, to, subject, body):
|
|---|
| 62 | """Send SMTP message"""
|
|---|
| 63 |
|
|---|
| 64 | connection = smtplib.SMTP()
|
|---|
| 65 |
|
|---|
| 66 | try:
|
|---|
| 67 | connection.connect(server)
|
|---|
| 68 | except socket.error, err:
|
|---|
| 69 | raise errors.SocketConnectionError(host = server, msg = "Unable to connect to SMTP server", orig_error = err)
|
|---|
| 70 |
|
|---|
| 71 | sender_user, sender_email = parseaddr(sender)
|
|---|
| 72 | payload = MIMEText(body.decode("utf-8", 'ignore').encode("utf-8", 'ignore'), "plain", "utf-8")
|
|---|
| 73 |
|
|---|
| 74 | msg = MIMEMultipart()
|
|---|
| 75 | msg["From"] = "%s <%s>" % (Header(unicode(sender_user)), sender_email)
|
|---|
| 76 | msg["User-Agent"] = "bzr/%s" % bzrlib_version
|
|---|
| 77 | msg["Date"] = formatdate(None, True)
|
|---|
| 78 | msg["Message-Id"] = make_msgid("bzr")
|
|---|
| 79 | msg["To"] = to
|
|---|
| 80 | msg["Subject"] = Header(subject)
|
|---|
| 81 | msg.attach(payload)
|
|---|
| 82 |
|
|---|
| 83 | connection.sendmail(sender, [to], msg.as_string())
|
|---|
| 84 |
|
|---|
| 85 | def config_to(config):
|
|---|
| 86 | """Address the mail should go to"""
|
|---|
| 87 |
|
|---|
| 88 | return config.get_user_option("post_commit_to")
|
|---|
| 89 |
|
|---|
| 90 | def config_sender(config):
|
|---|
| 91 | """Address the email should be sent from"""
|
|---|
| 92 |
|
|---|
| 93 | result = config.get_user_option("post_commit_sender")
|
|---|
| 94 | if (result is None):
|
|---|
| 95 | result = config.username()
|
|---|
| 96 |
|
|---|
| 97 | return result
|
|---|
| 98 |
|
|---|
| 99 | def merge_marker(revision):
|
|---|
| 100 | if (len(revision.parent_ids) > 1):
|
|---|
| 101 | return " [merge]"
|
|---|
| 102 |
|
|---|
| 103 | return ""
|
|---|
| 104 |
|
|---|
| 105 | def iter_reverse_revision_history(repository, revision_id):
|
|---|
| 106 | """Iterate backwards through revision ids in the lefthand history"""
|
|---|
| 107 |
|
|---|
| 108 | graph = repository.get_graph()
|
|---|
| 109 | stop_revisions = (None, _mod_revision.NULL_REVISION)
|
|---|
| 110 | return graph.iter_lefthand_ancestry(revision_id, stop_revisions)
|
|---|
| 111 |
|
|---|
| 112 | def revision_sequence(branch, revision_old_id, revision_new_id):
|
|---|
| 113 | """Calculate a sequence of revisions"""
|
|---|
| 114 |
|
|---|
| 115 | for revision_ac_id in iter_reverse_revision_history(branch.repository, revision_new_id):
|
|---|
| 116 | if (revision_ac_id == revision_old_id):
|
|---|
| 117 | break
|
|---|
| 118 |
|
|---|
| 119 | yield revision_ac_id
|
|---|
| 120 |
|
|---|
| 121 | def send_email(branch, revision_old_id, revision_new_id, config):
|
|---|
| 122 | """Send the email"""
|
|---|
| 123 |
|
|---|
| 124 | if (config_to(config) is not None):
|
|---|
| 125 | branch.lock_read()
|
|---|
| 126 | branch.repository.lock_read()
|
|---|
| 127 | try:
|
|---|
| 128 | revision_prev_id = revision_old_id
|
|---|
| 129 |
|
|---|
| 130 | for revision_ac_id in reversed(list(revision_sequence(branch, revision_old_id, revision_new_id))):
|
|---|
| 131 | body = StringIO()
|
|---|
| 132 |
|
|---|
| 133 | revision_ac = branch.repository.get_revision(revision_ac_id)
|
|---|
| 134 | revision_ac_no = branch.revision_id_to_revno(revision_ac_id)
|
|---|
| 135 |
|
|---|
| 136 | repo_name = os.path.basename(branch.base)
|
|---|
| 137 | if (repo_name == ''):
|
|---|
| 138 | repo_name = os.path.basename(os.path.dirname(branch.base))
|
|---|
| 139 |
|
|---|
| 140 | committer = revision_ac.committer
|
|---|
| 141 | authors = revision_ac.get_apparent_authors()
|
|---|
| 142 | date = time.strftime("%Y-%m-%d %H:%M:%S %Z (%a, %d %b %Y)", time.localtime(revision_ac.timestamp))
|
|---|
| 143 |
|
|---|
| 144 | body.write("Repo: %s\n" % repo_name)
|
|---|
| 145 |
|
|---|
| 146 | if (authors != [committer]):
|
|---|
| 147 | body.write("Author: %s\n" % ", ".join(authors))
|
|---|
| 148 |
|
|---|
| 149 | body.write("Committer: %s\n" % committer)
|
|---|
| 150 | body.write("Date: %s\n" % date)
|
|---|
| 151 | body.write("New Revision: %s%s\n" % (revision_ac_no, merge_marker(revision_ac)))
|
|---|
| 152 | body.write("New Id: %s\n" % revision_ac_id)
|
|---|
| 153 | for parent_id in revision_ac.parent_ids:
|
|---|
| 154 | body.write("Parent: %s\n" % parent_id)
|
|---|
| 155 |
|
|---|
| 156 | body.write("\n")
|
|---|
| 157 |
|
|---|
| 158 | commit_message = None
|
|---|
| 159 | body.write("Log:\n")
|
|---|
| 160 | if (not revision_ac.message):
|
|---|
| 161 | body.write("(empty)\n")
|
|---|
| 162 | else:
|
|---|
| 163 | log = revision_ac.message.rstrip("\n\r")
|
|---|
| 164 | for line in log.split("\n"):
|
|---|
| 165 | body.write("%s\n" % line)
|
|---|
| 166 | if (commit_message == None):
|
|---|
| 167 | commit_message = line
|
|---|
| 168 |
|
|---|
| 169 | if (commit_message == None):
|
|---|
| 170 | commit_message = "(empty)"
|
|---|
| 171 |
|
|---|
| 172 | body.write("\n")
|
|---|
| 173 |
|
|---|
| 174 | tree_prev = branch.repository.revision_tree(revision_prev_id)
|
|---|
| 175 | tree_ac = branch.repository.revision_tree(revision_ac_id)
|
|---|
| 176 |
|
|---|
| 177 | delta = tree_ac.changes_from(tree_prev)
|
|---|
| 178 |
|
|---|
| 179 | if (len(delta.added) > 0):
|
|---|
| 180 | body.write("Added:\n")
|
|---|
| 181 | for item in delta.added:
|
|---|
| 182 | body.write(" %s\n" % item[0])
|
|---|
| 183 |
|
|---|
| 184 | if (len(delta.removed) > 0):
|
|---|
| 185 | body.write("Removed:\n")
|
|---|
| 186 | for item in delta.removed:
|
|---|
| 187 | body.write(" %s\n" % item[0])
|
|---|
| 188 |
|
|---|
| 189 | if (len(delta.renamed) > 0):
|
|---|
| 190 | body.write("Renamed:\n")
|
|---|
| 191 | for item in delta.renamed:
|
|---|
| 192 | body.write(" %s -> %s\n" % (item[0], item[1]))
|
|---|
| 193 |
|
|---|
| 194 | if (len(delta.kind_changed) > 0):
|
|---|
| 195 | body.write("Changed:\n")
|
|---|
| 196 | for item in delta.kind_changed:
|
|---|
| 197 | body.write(" %s\n" % item[0])
|
|---|
| 198 |
|
|---|
| 199 | if (len(delta.modified) > 0):
|
|---|
| 200 | body.write("Modified:\n")
|
|---|
| 201 | for item in delta.modified:
|
|---|
| 202 | body.write(" %s\n" % item[0])
|
|---|
| 203 |
|
|---|
| 204 | body.write("\n")
|
|---|
| 205 |
|
|---|
| 206 | tree_prev.lock_read()
|
|---|
| 207 | try:
|
|---|
| 208 | tree_ac.lock_read()
|
|---|
| 209 | try:
|
|---|
| 210 | diff = DiffTree.from_trees_options(tree_prev, tree_ac, body, "utf8", None, "", "", None, 3)
|
|---|
| 211 | diff.show_diff(None, None)
|
|---|
| 212 | finally:
|
|---|
| 213 | tree_ac.unlock()
|
|---|
| 214 | finally:
|
|---|
| 215 | tree_prev.unlock()
|
|---|
| 216 |
|
|---|
| 217 | subject = "[%s] r%d - %s" % (repo_name, revision_ac_no, commit_message)
|
|---|
| 218 | send_smtp("localhost", config_sender(config), config_to(config), subject, body.getvalue())
|
|---|
| 219 |
|
|---|
| 220 | revision_prev_id = revision_ac_id
|
|---|
| 221 |
|
|---|
| 222 | finally:
|
|---|
| 223 | branch.repository.unlock()
|
|---|
| 224 | branch.unlock()
|
|---|
| 225 |
|
|---|
| 226 | def branch_post_change_hook(params):
|
|---|
| 227 | """post_change_branch_tip hook"""
|
|---|
| 228 |
|
|---|
| 229 | send_email(params.branch, params.old_revid, params.new_revid, params.branch.get_config())
|
|---|
| 230 |
|
|---|
| 231 | install_named_hook = getattr(Branch.hooks, "install_named_hook", None)
|
|---|
| 232 | install_named_hook("post_change_branch_tip", branch_post_change_hook, "bzreml")
|
|---|