Mercurial > treepkg
annotate bin/sendnotificationmails.py @ 571:7d5abf0bba91
Pull needs the local dir
Also use the repo to pull and not the workincopy object
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 11:50:37 +0000 |
parents | 7de7869962ef |
children | 8f62a825addb |
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, ""), |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
26 "build_listpending", "notification_template", |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
27 "smtp_host", ("smtp_port", int), |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
28 ] |
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 def read_config(filename): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
31 parser = SafeConfigParser() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
32 parser.read([filename]) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
33 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
|
34 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
35 def parse_commandline(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
36 parser = OptionParser() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
37 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
|
38 "notification.cfg")) |
563
a4efa91bc3b2
Also use option --config to pass the config file
Bjoern Ricks <bricks@intevation.de>
parents:
466
diff
changeset
|
39 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
|
40 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
|
41 " 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
|
42 return parser.parse_args() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
43 |
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 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
|
46 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
|
47 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
|
48 recipients = [addr[1] for addr |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
49 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
|
50 + msg.get_all("Cc", []))] |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
51 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
|
52 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
|
53 server.quit() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
54 |
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 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
|
57 config = read_config(config_filename) |
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 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
|
60 |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
61 if not config.get("build_user") or not config.get("build_host"): |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
62 lines = capture_output(cmdexpand("$build_listpending", **config)) |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
63 else: |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
64 lines = capture_output(cmdexpand("ssh $build_user$@$build_host" |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
65 " $build_listpending", |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
66 **config)) |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
67 for line in lines.splitlines(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
68 words = line.split() |
241
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
69 if len(words) == 4: |
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
70 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
|
71 values = config.copy() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
72 values.update(locals()) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
73 send_mail(config, template % values) |
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 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
76 def main(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 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
|
81 sys.exit(1) |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
82 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
|
83 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
84 main() |