annotate bin/sendnotificationmails.py @ 196:86ea689eda5f

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