Mercurial > treepkg
annotate bin/sendnotificationmails.py @ 576:8f62a825addb
Use shlex.split for build_listpanding command because the string may contain arguments like --config-file foo.cfg
When returning lists cmdexpand replacement variable must use @ instead of $
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Sat, 03 Sep 2011 11:35:36 +0000 |
parents | 7de7869962ef |
children | 7a9841e4958f |
rev | line source |
---|---|
287
1fcdffbeb9de
Make the #! line in the commands more portable. Use /usr/bin/python
Bernhard Herzog <bh@intevation.de>
parents:
241
diff
changeset
|
1 #! /usr/bin/python |
241
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
2 # Copyright (C) 2008, 2009 by Intevation GmbH |
99
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 |
564
a61046da30a1
Check if config file exists and set destination of --config option to config_file
Bjoern Ricks <bricks@intevation.de>
parents:
563
diff
changeset
|
12 import os.path |
a61046da30a1
Check if config file exists and set destination of --config option to config_file
Bjoern Ricks <bricks@intevation.de>
parents:
563
diff
changeset
|
13 import sys |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
14 import smtplib |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
15 import email |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
16 import email.Utils |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
17 from optparse import OptionParser |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
18 from ConfigParser import SafeConfigParser |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
19 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
20 import treepkgcmd |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
21 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
|
22 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
|
23 from treepkg.cmdexpand import cmdexpand |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
24 |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
25 notification_desc = [("build_user", str, ""), ("build_host", str, ""), |
576
8f62a825addb
Use shlex.split for build_listpanding command because the string may contain arguments like --config-file foo.cfg
Bjoern Ricks <bricks@intevation.de>
parents:
565
diff
changeset
|
26 ("build_listpending", shlex.split), |
8f62a825addb
Use shlex.split for build_listpanding command because the string may contain arguments like --config-file foo.cfg
Bjoern Ricks <bricks@intevation.de>
parents:
565
diff
changeset
|
27 "notification_template", |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
28 "smtp_host", ("smtp_port", int), |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
29 ] |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
30 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
31 def read_config(filename): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
32 parser = SafeConfigParser() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
33 parser.read([filename]) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
34 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
|
35 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
36 def parse_commandline(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
37 parser = OptionParser() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
38 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
|
39 "notification.cfg")) |
563
a4efa91bc3b2
Also use option --config to pass the config file
Bjoern Ricks <bricks@intevation.de>
parents:
466
diff
changeset
|
40 parser.add_option("--config", "--config-file", |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
41 help=("The configuration file." |
564
a61046da30a1
Check if config file exists and set destination of --config option to config_file
Bjoern Ricks <bricks@intevation.de>
parents:
563
diff
changeset
|
42 " Default notification.cfg"), dest="config_file") |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
43 return parser.parse_args() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
44 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
45 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 recipients = [addr[1] for addr |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
50 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
|
51 + msg.get_all("Cc", []))] |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
52 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
|
53 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
|
54 server.quit() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
55 |
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 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
|
58 config = read_config(config_filename) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
59 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
60 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
|
61 |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
62 if not config.get("build_user") or not config.get("build_host"): |
576
8f62a825addb
Use shlex.split for build_listpanding command because the string may contain arguments like --config-file foo.cfg
Bjoern Ricks <bricks@intevation.de>
parents:
565
diff
changeset
|
63 lines = capture_output(cmdexpand("@build_listpending", **config)) |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
64 else: |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
65 lines = capture_output(cmdexpand("ssh $build_user$@$build_host" |
576
8f62a825addb
Use shlex.split for build_listpanding command because the string may contain arguments like --config-file foo.cfg
Bjoern Ricks <bricks@intevation.de>
parents:
565
diff
changeset
|
66 " @build_listpending", |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
67 **config)) |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
68 for line in lines.splitlines(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
69 words = line.split() |
241
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
70 if len(words) == 4: |
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
71 status, track, revision, rules_revision = words |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
72 values = config.copy() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
73 values.update(locals()) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
74 send_mail(config, template % values) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
75 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
76 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
77 def main(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
78 options, args = parse_commandline() |
564
a61046da30a1
Check if config file exists and set destination of --config option to config_file
Bjoern Ricks <bricks@intevation.de>
parents:
563
diff
changeset
|
79 if not os.path.exists(options.config_file): |
565
7de7869962ef
Correct syntax and display abspath of config file
Bjoern Ricks <bricks@intevation.de>
parents:
564
diff
changeset
|
80 print >> sys.stderr, "File not found: %s" %\ |
7de7869962ef
Correct syntax and display abspath of config file
Bjoern Ricks <bricks@intevation.de>
parents:
564
diff
changeset
|
81 os.path.abspath(options.config_file) |
564
a61046da30a1
Check if config file exists and set destination of --config option to config_file
Bjoern Ricks <bricks@intevation.de>
parents:
563
diff
changeset
|
82 sys.exit(1) |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
83 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
|
84 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
85 main() |