bh@287: #! /usr/bin/python bh@241: # Copyright (C) 2008, 2009 by Intevation GmbH bh@99: # Authors: bh@99: # Bernhard Herzog bh@99: # bh@99: # This program is free software under the GPL (>=v2) bh@99: # Read the file COPYING coming with the software for details. bh@99: bh@99: """Send pending notification mails""" bh@99: bh@99: import os bricks@564: import os.path bricks@564: import sys bh@99: import smtplib bh@99: import email bh@99: import email.Utils bh@99: from optparse import OptionParser bh@99: from ConfigParser import SafeConfigParser bh@99: bh@99: import treepkgcmd bh@99: from treepkg.readconfig import read_config_section bh@99: from treepkg.run import capture_output bh@99: from treepkg.cmdexpand import cmdexpand bh@99: aheinecke@466: notification_desc = [("build_user", str, ""), ("build_host", str, ""), aheinecke@466: "build_listpending", "notification_template", bh@99: "smtp_host", ("smtp_port", int), bh@99: ] bh@99: bh@99: def read_config(filename): bh@99: parser = SafeConfigParser() bh@99: parser.read([filename]) bh@99: return read_config_section(parser, "notification", notification_desc) bh@99: bh@99: def parse_commandline(): bh@99: parser = OptionParser() bh@99: parser.set_defaults(config_file=os.path.join(treepkgcmd.topdir, bh@99: "notification.cfg")) bricks@563: parser.add_option("--config", "--config-file", bh@99: help=("The configuration file." bricks@564: " Default notification.cfg"), dest="config_file") bh@99: return parser.parse_args() bh@99: bh@99: bh@99: def send_mail(config, raw_message): bh@99: msg = email.message_from_string(raw_message) bh@99: sender = email.Utils.parseaddr(msg["From"])[1] bh@99: recipients = [addr[1] for addr bh@99: in email.Utils.getaddresses(msg.get_all("To", []) bh@99: + msg.get_all("Cc", []))] bh@99: server = smtplib.SMTP(config["smtp_host"], config["smtp_port"]) bh@99: server.sendmail(sender, recipients, raw_message) bh@99: server.quit() bh@99: bh@99: bh@99: def send_notification_mails(config_filename): bh@99: config = read_config(config_filename) bh@99: bh@99: template = open(config["notification_template"]).read() bh@99: aheinecke@466: if not config.get("build_user") or not config.get("build_host"): aheinecke@466: lines = capture_output(cmdexpand("$build_listpending", **config)) aheinecke@466: else: aheinecke@466: lines = capture_output(cmdexpand("ssh $build_user$@$build_host" aheinecke@466: " $build_listpending", aheinecke@466: **config)) bh@99: for line in lines.splitlines(): bh@99: words = line.split() bh@241: if len(words) == 4: bh@241: status, track, revision, rules_revision = words bh@99: values = config.copy() bh@99: values.update(locals()) bh@99: send_mail(config, template % values) bh@99: bh@99: bh@99: def main(): bh@99: options, args = parse_commandline() bricks@564: if not os.path.exists(options.config_file): bricks@565: print >> sys.stderr, "File not found: %s" %\ bricks@565: os.path.abspath(options.config_file) bricks@564: sys.exit(1) bh@99: send_notification_mails(options.config_file) bh@99: bh@99: main()