Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/util/test/send_email.py b/util/test/send_email.py
- index 2447f6117b..1fc8c8d5d7 100755
- --- a/util/test/send_email.py
- +++ b/util/test/send_email.py
- @@ -1,7 +1,10 @@
- #!/usr/bin/env python
- """Portable email sender. Acts as replacement for mail, Mail, mailx,
- -email (cygwin). Message body is taken from stdin.
- +email (cygwin). Message body is taken from stdin. Option to save the
- +message as a script file instead of immediately sending thru SMTP;
- +later, run that script file to send the same message, perhaps from
- +some other system.
- """
- from __future__ import print_function
- @@ -14,6 +17,7 @@ import os
- import smtplib
- import socket
- import sys
- +import tempfile
- def main():
- @@ -23,7 +27,12 @@ def main():
- body = sys.stdin.read()
- # Send the email!
- - send_email(args.recipients, body, args.subject, args.header, args.sender, args.smtp_host)
- + if not args.proxy_file:
- + # to SMTP server
- + send_email(args.recipients, body, args.subject, args.header, args.sender, args.smtp_host)
- + else:
- + # to save as a file
- + write_proxy_file(args, body)
- def send_email(recipients, body, subject=None, headers=None, sender=None, smtp_host=None):
- @@ -82,8 +91,9 @@ def _parse_headers(option, opt, value, parser, *args, **kwargs):
- # Split the value provided.
- parsed_vals = value.split(',')
- for v in parsed_vals:
- - key, value = v.split('=')
- - value_dict[key] = value
- + if '=' in v:
- + key, value = v.split('=')
- + value_dict[key] = value
- # Set the updated dict to the oiption value.
- setattr(parser.values, option.dest, value_dict)
- @@ -100,6 +110,11 @@ def _default_smtp_host():
- """
- return os.environ.get('CHPL_UTIL_SMTP_HOST', 'localhost')
- +def _default_proxy_file():
- + """Return path to proxy file if CHPL_UTIL_PROXY_FILE is set in environment.
- + """
- + return os.environ.get('CHPL_UTIL_PROXY_FILE', None )
- +
- def _parse_args():
- """Parse and return command line arguments."""
- class NoWrapHelpFormatter(optparse.IndentedHelpFormatter):
- @@ -120,32 +135,46 @@ def _parse_args():
- help='Verbose output.'
- )
- - mail_group = optparse.OptionGroup(parser, 'Mail Options')
- + transport_group = optparse.OptionGroup(parser, 'Mail Transport Options')
- +
- + transport_group.add_option(
- + '--smtp-host',
- + default=_default_smtp_host(),
- + help='SMTP host to use when sending email.'
- + ' (default: %default ($CHPL_UTIL_SMTP_HOST, if defined; else "localhost"))'
- + )
- + transport_group.add_option(
- + '--proxy-file',
- + default=_default_proxy_file(),
- + help='Optional file path.'
- + ' If used, this e-mail message will not be sent to SMTP.'
- + ' Instead, a script to reproduce the message will be written to the "proxy file".'
- + ' (default: %default ($CHPL_UTIL_PROXY_FILE, if defined)'
- + )
- +
- + parser.add_option_group(transport_group)
- - mail_group.add_option(
- + message_group = optparse.OptionGroup(parser, 'Message Options')
- +
- + message_group.add_option(
- '-s', '--subject',
- default=None,
- help='Email subject.'
- )
- - mail_group.add_option(
- + message_group.add_option(
- '-H', '--header',
- action='callback', type='string',
- callback=_parse_headers,
- help=('Email header(s) of form NAME=VALUE. '
- 'Specify more than one with comma delimited list.')
- )
- - mail_group.add_option(
- + message_group.add_option(
- '-S', '--sender',
- default=_default_sender(),
- help='Sender email address. (default: %default)'
- )
- - mail_group.add_option(
- - '--smtp-host',
- - default=_default_smtp_host(),
- - help='SMTP host to use when sending email. (default: %default)'
- - )
- - parser.add_option_group(mail_group)
- + parser.add_option_group(message_group)
- opts, args = parser.parse_args()
- @@ -165,6 +194,98 @@ def _setup_logging(verbose=False):
- level=log_level)
- logging.debug('Verbose output enabled.')
- +def write_proxy_file(args, body):
- + """Writes the optional "proxy file", a shell script able to recreate this
- +exact email message and send it later, even from a different machine.
- +This feature allows Chapel tests to run on slaves without an SNMP server.
- +"""
- +
- + # Example proxy file:
- + # send_email.py <<\EoF0 \
- + # -s 'My example e-mail notification' \
- + # -S sender-address@mail.com \
- + # -H Return-Path=return-address@mail.com, \
- + # address1@mail.com address2@mail.com
- + # The message body goes here.
- + # The end.
- + # EoF0
- +
- + try:
- + from shlex import quote # Python 3
- + except ImportError:
- + from pipes import quote # Python 2.6+
- +
- + # build send_email.py command script in string buffer
- +
- + for eofd in range(0, 1000):
- + if not "\nEoF%d\n" % (eofd) in "%s\n" % (body.rstrip()):
- + break
- +
- + command = "send_email.py <<\\EoF%d" % (eofd)
- + if args.subject:
- + command += " \\\n -s %s" % (quote(args.subject))
- + if args.sender:
- + command += " \\\n -S %s" % (quote(args.sender))
- + if args.header:
- + command += " \\\n -H "
- + headers = ""
- + for k,v in args.header.items():
- + headers += "%s=%s," % (k,v)
- + command += "%s" % (quote(headers))
- + if args.recipients:
- + command += " \\\n "
- + for v in args.recipients:
- + command += " %s" % (quote(v))
- +
- + command += "\n%s\nEoF%d" % (body.rstrip(),eofd)
- +
- + # prepare the proxy file for output, based on args.proxy_file, yet with uniqueness
- +
- + logging.debug('Ready --proxy-file=%s' % (args.proxy_file))
- +
- + dirname = os.path.dirname(args.proxy_file)
- + basename= os.path.basename(args.proxy_file)
- +
- + # mkdir a path to the given args.proxy_file, and ignore any exceptions
- +
- + try:
- + os.makedirs(os.path.dirname(args.proxy_file))
- + except:
- + pass
- +
- + # generate mktemp arguments based on the given args.proxy_file, or defaults
- + # we want mktemp to generate a unique filename like "send_email-XXXXX.sh"
- +
- + if basename:
- + if len(basename.rsplit('.', 1)) == 2:
- + (pref, suff) = basename.rsplit('.', 1)
- + if (not pref) and (not suff):
- + pref = 'send_email'
- + suff = '.'
- + elif not pref:
- + pref = '.' + suff
- + suff = ''
- + elif not suff:
- + suff = '.'
- + else:
- + pref = basename
- + suff = ''
- + else:
- + pref = 'send_email'
- + suff = '.sh'
- + if pref:
- + pref = pref.rstrip('-') + '-'
- + if suff:
- + suff = '.' + suff.lstrip('.')
- +
- + # open the file and write the string buffer into it
- +
- + (fd, pth) = tempfile.mkstemp(suffix=suff, prefix=pref, dir=dirname, text=True)
- + logging.info('Writing proxy file: %s' % (pth))
- + with os.fdopen(fd, 'w') as fp:
- + logging.debug('Command:\n%s' % (command))
- + fp.write("%s" % (command))
- +
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment