git-bin/post-receive-notify-updates

118 lines
3.3 KiB
Python
Executable File

#!/usr/bin/python2
#
# post-receive-notify-updates
#
# Copyright (C) 2008 Owen Taylor
# Copyright (C) 2009 Red Hat, Inc
# Copyright (C) 2009 Frederic Peters
#
# 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, If not, see
# http://www.gnu.org/licenses/.
#
# About
# =====
# This script is used to send out notification mails on branch updates; these
# notification mails are typically used to trigger automated rebuilds
#
import re
import os
import pwd
import sys
script_path = os.path.realpath(os.path.abspath(sys.argv[0]))
script_dir = os.path.dirname(script_path)
sys.path.insert(0, script_dir)
from git import *
from util import die, strip_string as s, start_email, end_email
# These addresses always get notified
ALL_MODULE_RECIPIENTS = []
BRANCH_RE = re.compile(r'^refs/heads/(.*)$')
def get_branch_name(refname):
m = BRANCH_RE.match(refname)
if m:
return m.group(1)
SPLIT_RE = re.compile("\s*,\s*")
def main():
module_name = get_module_name()
try:
recipients_s = git.config("hooks.update-recipients", _quiet=True)
except CalledProcessError:
recipients_s = ""
recipients_s = recipients_s.strip()
if recipients_s == "":
recipients = []
else:
recipients = SPLIT_RE.split(recipients_s)
for recipient in ALL_MODULE_RECIPIENTS:
if not recipient in recipients:
recipients.append(recipient)
changes = []
if len(sys.argv) > 1:
# For testing purposes, allow passing in a ref update on the command line
if len(sys.argv) != 4:
die("Usage: post-receive-notify-updates OLDREV NEWREV REFNAME")
branch_name = get_branch_name(sys.argv[3])
if branch_name is not None:
changes.append((branch_name, sys.argv[1], sys.argv[2], sys.argv[3]))
else:
for line in sys.stdin:
items = line.strip().split()
if len(items) != 3:
die("Input line has unexpected number of items")
branch_name = get_branch_name(items[2])
if branch_name is not None:
changes.append((branch_name, items[0], items[1], items[2]))
if len(changes) == 0:
# Nothing to mail about
return
branches = [branch_name for branch_name, _, _, _ in changes]
subject = module_name + " " + " ".join(branches)
if recipients:
out = start_email()
print >>out, s("""
To: %(recipients)s
From: noreply@entrouvert.org
Subject: %(subject)s
""") % {
'recipients': ", ".join(recipients),
'subject': subject
}
# Trailing newline to signal the end of the header
print >>out
for _, oldrev, newrev, refname in changes:
print >>out, oldrev, newrev, refname
end_email()
if __name__ == '__main__':
main()