view bin/sendnotificationmails.py @ 578:bccd69dedbe5

shlex.split all config parameters that may contain a command Return the command as list and use @ operator for the variable in cmdexpand
author Bjoern Ricks <bricks@intevation.de>
date Sat, 03 Sep 2011 11:53:12 +0000
parents 7a9841e4958f
children
line wrap: on
line source
#! /usr/bin/python
# Copyright (C) 2008, 2009 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""Send pending notification mails"""

import os
import os.path
import sys
import smtplib
import shlex
import email
import email.Utils
from optparse import OptionParser
from ConfigParser import SafeConfigParser

import treepkgcmd
from treepkg.readconfig import read_config_section
from treepkg.run import capture_output
from treepkg.cmdexpand import cmdexpand

notification_desc = [("build_user", str, ""), ("build_host", str, ""),
                     ("build_listpending", shlex.split),
                     "notification_template",
                     "smtp_host", ("smtp_port", int),
                     ]

def read_config(filename):
    parser = SafeConfigParser()
    parser.read([filename])
    return read_config_section(parser, "notification", notification_desc)

def parse_commandline():
    parser = OptionParser()
    parser.set_defaults(config_file=os.path.join(treepkgcmd.topdir,
                                                 "notification.cfg"))
    parser.add_option("--config", "--config-file",
                      help=("The configuration file."
                            " Default notification.cfg"), dest="config_file")
    return parser.parse_args()


def send_mail(config, raw_message):
    msg = email.message_from_string(raw_message)
    sender = email.Utils.parseaddr(msg["From"])[1]
    recipients = [addr[1] for addr
                  in email.Utils.getaddresses(msg.get_all("To", [])
                                              + msg.get_all("Cc", []))]
    server = smtplib.SMTP(config["smtp_host"], config["smtp_port"])
    server.sendmail(sender, recipients, raw_message)
    server.quit()


def send_notification_mails(config_filename):
    config = read_config(config_filename)

    template = open(config["notification_template"]).read()

    if not config.get("build_user") or not config.get("build_host"):
        lines = capture_output(cmdexpand("@build_listpending", **config))
    else:
        lines = capture_output(cmdexpand("ssh $build_user$@$build_host"
                                         " @build_listpending",
                                         **config))
    for line in lines.splitlines():
        words = line.split()
        if len(words) == 4:
            status, track, revision, rules_revision = words
            values = config.copy()
            values.update(locals())
            send_mail(config, template % values)


def main():
    options, args = parse_commandline()
    if not os.path.exists(options.config_file):
        print >> sys.stderr, "File not found: %s" %\
            os.path.abspath(options.config_file)
        sys.exit(1)
    send_notification_mails(options.config_file)

main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)