source: rtems-source-builder/source-builder/sb/mailer.py @ fbb39e7

4.104.114.95
Last change on this file since fbb39e7 was 97a685f, checked in by Chris Johns <chrisj@…>, on 04/30/13 at 01:20:54

Add mail support to mail reports.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2013 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Permission to use, copy, modify, and/or distribute this software for any
9# purpose with or without fee is hereby granted, provided that the above
10# copyright notice and this permission notice appear in all copies.
11#
12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#
21# Manage emailing results or reports.
22#
23
24import os
25import smtplib
26import socket
27
28import error
29import options
30import path
31
32def append_options(opts):
33    opts['--mail'] = 'Send email report or results.'
34    opts['--smtp-host'] = 'SMTP host to send via.'
35    opts['--mail-to'] = 'Email address to send the email too.'
36    opts['--mail-from'] = 'Email address the report is from.'
37
38class mail:
39    def __init__(self, opts):
40        self.opts = opts
41
42    def from_address(self):
43
44        def _clean(l):
45            if '#' in l:
46                l = l[:l.index('#')]
47            if '\r' in l:
48                l = l[:l.index('r')]
49            if '\n' in l:
50                l = l[:l.index('\n')]
51            return l.strip()
52
53        addr = self.opts.get_arg('--mail-from')
54        if addr is not None:
55            return addr[1]
56        mailrc = None
57        if 'MAILRC' in os.environ:
58            mailrc = os.environ['MAILRC']
59        if mailrc is None and 'HOME' in os.environ:
60            mailrc = path.join(os.environ['HOME'], '.mailrc')
61        if mailrc is not None and path.exists(mailrc):
62            # set from="Joe Blow <joe@blow.org>"
63            try:
64                mrc = open(mailrc, 'r')
65                lines = mrc.readlines()
66                mrc.close()
67            except IOError, err:
68                raise error.general('error reading: %s' % (mailrc))
69            for l in lines:
70                l = _clean(l)
71                if 'from' in l:
72                    fa = l[l.index('from') + len('from'):]
73                    if '=' in fa:
74                        addr = fa[fa.index('=') + 1:].replace('"', ' ').strip()
75            if addr is not None:
76                return addr
77        addr = self.opts.defaults.get_value('%{_sbgit_mail}')
78        return addr
79
80    def smtp_host(self):
81        host = self.opts.get_arg('--smtp-host')
82        if host is not None:
83            return host[1]
84        host = self.opts.defaults.get_value('%{_mail_smtp_host}')
85        if host is not None:
86            return host
87        return 'localhost'
88
89    def send(self, to_addr, subject, body):
90        from_addr = self.from_address()
91        msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
92            (from_addr, to_addr, subject) + body
93        try:
94            s = smtplib.SMTP(self.smtp_host())
95            s.sendmail(from_addr, [to_addr], msg)
96        except smtplib.SMTPException, se:
97            raise error.general('sending mail: %s' % (str(se)))
98        except socket.error, se:
99            raise error.general('sending mail: %s' % (str(se)))
100
101if __name__ == '__main__':
102    import sys
103    optargs = {}
104    append_options(optargs)
105    opts = options.load(sys.argv, optargs = optargs, defaults = 'defaults.mc')
106    m = mail(opts)
107    print 'From: %s' % (m.from_address())
108    print 'SMTP Host: %s' % (m.smtp_host())
109    m.send(m.from_address(), 'Test mailer.py', 'This is a test')
Note: See TracBrowser for help on using the repository browser.