Fix frontend.py to send email when asked, and not use broken machines

Signed-off-by: Martin Bligh <mbligh@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@2553 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py
index ec5a92f..77d7e81 100644
--- a/client/common_lib/utils.py
+++ b/client/common_lib/utils.py
@@ -4,7 +4,7 @@
 
 import os, pickle, random, re, resource, select, shutil, signal, StringIO
 import socket, struct, subprocess, sys, time, textwrap, urllib, urlparse
-import warnings
+import warnings, smtplib
 from autotest_lib.client.common_lib import error, barrier, debug
 
 def deprecated(func):
@@ -39,6 +39,7 @@
         self.stdout_file = stdout_file
         self.stderr_file = stderr_file
 
+
     def process_output(self, stdout=True, final_read=False):
         """output_prepare must be called prior to calling this"""
         if stdout:
@@ -85,7 +86,6 @@
 
 
 def create_subnet_mask(bits):
-    # ~ does weird things in python...but this does work
     return (1 << 32) - (1 << 32-bits)
 
 
@@ -110,6 +110,31 @@
                    '%d %d\n' % (lower, upper))
 
 
+
+def send_email(mail_from, mail_to, subject, body):
+    """
+    Sends an email via smtp
+
+    mail_from: string with email address of sender
+    mail_to: string or list with email address(es) of recipients
+    subject: string with subject of email
+    body: (multi-line) string with body of email
+    """
+    if isinstance(mail_to, str):
+        mail_to = [mail_to]
+    msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (mail_from, ','.join(mail_to),
+                                                   subject, body)
+    try:
+        mailer = smtplib.SMTP('localhost')
+        try:
+            mailer.sendmail(mail_from, mail_to, msg)
+        finally:
+            mailer.quit()
+    except Exception, e:
+        # Emails are non-critical, not errors, but don't raise them
+        print "Sending email failed. Reason: %s" % repr(e)
+
+
 def read_one_line(filename):
     return open(filename, 'r').readline().rstrip('\n')
 
@@ -307,6 +332,7 @@
 
     return bg_job.result
 
+
 def run_parallel(commands, timeout=None, ignore_status=False,
                  stdout_tee=None, stderr_tee=None):
     """Beahves the same as run with the following exceptions:
@@ -474,7 +500,6 @@
             pass
 
 
-
 def system(command, timeout=None, ignore_status=False):
     """This function returns the exit status of command."""
     return run(command, timeout=timeout, ignore_status=ignore_status,