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

4.11
Last change on this file since f88fcf3 was f88fcf3, checked in by Chris Johns <chrisj@…>, on Mar 7, 2016 at 12:56:02 AM

sb: Update code base to support Python3 and Python2.

Fix Windows support to allow MSYS2 Python to be used.

Updates #2619.

  • Property mode set to 100644
File size: 3.8 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
24from __future__ import print_function
25
26import os
27import smtplib
28import socket
29
30import error
31import options
32import path
33
34def append_options(opts):
35    opts['--mail'] = 'Send email report or results.'
36    opts['--smtp-host'] = 'SMTP host to send via.'
37    opts['--mail-to'] = 'Email address to send the email too.'
38    opts['--mail-from'] = 'Email address the report is from.'
39
40class mail:
41    def __init__(self, opts):
42        self.opts = opts
43
44    def from_address(self):
45
46        def _clean(l):
47            if '#' in l:
48                l = l[:l.index('#')]
49            if '\r' in l:
50                l = l[:l.index('r')]
51            if '\n' in l:
52                l = l[:l.index('\n')]
53            return l.strip()
54
55        addr = self.opts.get_arg('--mail-from')
56        if addr is not None:
57            return addr[1]
58        mailrc = None
59        if 'MAILRC' in os.environ:
60            mailrc = os.environ['MAILRC']
61        if mailrc is None and 'HOME' in os.environ:
62            mailrc = path.join(os.environ['HOME'], '.mailrc')
63        if mailrc is not None and path.exists(mailrc):
64            # set from="Joe Blow <joe@blow.org>"
65            try:
66                mrc = open(mailrc, 'r')
67                lines = mrc.readlines()
68                mrc.close()
69            except IOError as err:
70                raise error.general('error reading: %s' % (mailrc))
71            for l in lines:
72                l = _clean(l)
73                if 'from' in l:
74                    fa = l[l.index('from') + len('from'):]
75                    if '=' in fa:
76                        addr = fa[fa.index('=') + 1:].replace('"', ' ').strip()
77            if addr is not None:
78                return addr
79        addr = self.opts.defaults.get_value('%{_sbgit_mail}')
80        return addr
81
82    def smtp_host(self):
83        host = self.opts.get_arg('--smtp-host')
84        if host is not None:
85            return host[1]
86        host = self.opts.defaults.get_value('%{_mail_smtp_host}')
87        if host is not None:
88            return host
89        return 'localhost'
90
91    def send(self, to_addr, subject, body):
92        from_addr = self.from_address()
93        msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
94            (from_addr, to_addr, subject) + body
95        try:
96            s = smtplib.SMTP(self.smtp_host())
97            s.sendmail(from_addr, [to_addr], msg)
98        except smtplib.SMTPException as se:
99            raise error.general('sending mail: %s' % (str(se)))
100        except socket.error as se:
101            raise error.general('sending mail: %s' % (str(se)))
102
103if __name__ == '__main__':
104    import sys
105    optargs = {}
106    append_options(optargs)
107    opts = options.load(sys.argv, optargs = optargs, defaults = 'defaults.mc')
108    m = mail(opts)
109    print('From: %s' % (m.from_address()))
110    print('SMTP Host: %s' % (m.smtp_host()))
111    m.send(m.from_address(), 'Test mailer.py', 'This is a test')
Note: See TracBrowser for help on using the repository browser.