comparison bin/sendnotificationmails.py @ 99:7888fe374e11

Add support for notification mails in case of build errors This involves a new status field notification_mail to keep track of whether a notification has been sent for a particular build attempt and two programs to list the pending notifications and to send the pending notifications (similar to how the static web pages are published) as well as the corresponding configuration files.
author Bernhard Herzog <bh@intevation.de>
date Tue, 19 Feb 2008 19:19:23 +0000
parents
children aea6b97e7c68
comparison
equal deleted inserted replaced
98:f7b9c7113c46 99:7888fe374e11
1 #! /usr/bin/python2.4
2 # Copyright (C) 2008 by Intevation GmbH
3 # Authors:
4 # Bernhard Herzog <bh@intevation.de>
5 #
6 # This program is free software under the GPL (>=v2)
7 # Read the file COPYING coming with the software for details.
8
9 """Send pending notification mails"""
10
11 import os
12 import smtplib
13 import email
14 import email.Utils
15 from optparse import OptionParser
16 from ConfigParser import SafeConfigParser
17
18 import treepkgcmd
19 from treepkg.readconfig import read_config_section
20 from treepkg.run import capture_output
21 from treepkg.cmdexpand import cmdexpand
22
23 notification_desc = ["build_user", "build_host", "build_listpending",
24 "notification_template", "base_url",
25 "smtp_host", ("smtp_port", int),
26 ]
27
28 def read_config(filename):
29 parser = SafeConfigParser()
30 parser.read([filename])
31 return read_config_section(parser, "notification", notification_desc)
32
33 def parse_commandline():
34 parser = OptionParser()
35 parser.set_defaults(config_file=os.path.join(treepkgcmd.topdir,
36 "notification.cfg"))
37 parser.add_option("--config-file",
38 help=("The configuration file."
39 " Default notification.cfg"))
40 return parser.parse_args()
41
42
43 def send_mail(config, raw_message):
44 msg = email.message_from_string(raw_message)
45 sender = email.Utils.parseaddr(msg["From"])[1]
46 recipients = [addr[1] for addr
47 in email.Utils.getaddresses(msg.get_all("To", [])
48 + msg.get_all("Cc", []))]
49 server = smtplib.SMTP(config["smtp_host"], config["smtp_port"])
50 server.sendmail(sender, recipients, raw_message)
51 server.quit()
52
53
54 def send_notification_mails(config_filename):
55 config = read_config(config_filename)
56
57 template = open(config["notification_template"]).read()
58
59 lines = capture_output(cmdexpand("ssh $build_user$@$build_host"
60 " $build_listpending",
61 **config))
62 for line in lines.splitlines():
63 words = line.split()
64 if len(words) == 3:
65 status, track, revision = words
66 values = config.copy()
67 values.update(locals())
68 send_mail(config, template % values)
69
70
71 def main():
72 options, args = parse_commandline()
73 send_notification_mails(options.config_file)
74
75 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)