Mercurial > treepkg
annotate bin/sendnotificationmails.py @ 557:9824e409388b
Refactor git branching
If a checkout is already available and the branch is changed in
the config git command would always fail because it doesn't know
the branch to track. Therefore always check if the branch is
locally available and if not checkout the remote branch
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 08:45:28 +0000 |
parents | e4c0beab5328 |
children | a4efa91bc3b2 |
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 |
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 |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
23 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
|
24 "build_listpending", "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 |
466
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
59 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
|
60 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
|
61 else: |
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("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
|
63 " $build_listpending", |
e4c0beab5328
Allow sending notifications from a local treepkg via sendnotificationmails.py
Andre Heinecke <aheinecke@intevation.de>
parents:
287
diff
changeset
|
64 **config)) |
99
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
65 for line in lines.splitlines(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
66 words = line.split() |
241
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
67 if len(words) == 4: |
df3065e4c76b
Include the rules revision in the output of listpendingnotifications.py
Bernhard Herzog <bh@intevation.de>
parents:
100
diff
changeset
|
68 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
|
69 values = config.copy() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
70 values.update(locals()) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
71 send_mail(config, template % values) |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
72 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
73 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
74 def main(): |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
75 options, args = parse_commandline() |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
76 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
|
77 |
7888fe374e11
Add support for notification mails in case of build errors
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
78 main() |