<s> import os <EOL> import sys <EOL> import smtplib <EOL> import copy <EOL> from email . mime . text import MIMEText <EOL> from email . utils import formatdate , make_msgid <EOL> from superlance . process_state_monitor import ProcessStateMonitor <EOL> doc = """<STR_LIT>""" <EOL> class ProcessStateEmailMonitor ( ProcessStateMonitor ) : <EOL> COMMASPACE = '<STR_LIT:U+002CU+0020>' <EOL> @ classmethod <EOL> def _get_opt_parser ( cls ) : <EOL> from optparse import OptionParser <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:float>" , default = <NUM_LIT:1.0> , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT:localhost>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> @ classmethod <EOL> def parse_cmd_line_options ( cls ) : <EOL> parser = cls . _get_opt_parser ( ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> return options <EOL> @ classmethod <EOL> def validate_cmd_line_options ( cls , options ) : <EOL> parser = cls . _get_opt_parser ( ) <EOL> if not options . to_emails : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not options . from_email : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> validated = copy . copy ( options ) <EOL> validated . to_emails = [ x . strip ( ) for x in options . to_emails . split ( "<STR_LIT:U+002C>" ) ] <EOL> return validated <EOL> @ classmethod <EOL> def get_cmd_line_options ( cls ) : <EOL> return cls . validate_cmd_line_options ( cls . parse_cmd_line_options ( ) ) <EOL> @ classmethod <EOL> def create_from_cmd_line ( cls ) : <EOL> options = cls . get_cmd_line_options ( ) <EOL> if not '<STR_LIT>' in os . environ : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return cls ( ** options . __dict__ ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> ProcessStateMonitor . __init__ ( self , ** kwargs ) <EOL> self . from_email = kwargs [ '<STR_LIT>' ] <EOL> self . to_emails = kwargs [ '<STR_LIT>' ] <EOL> self . subject = kwargs . get ( '<STR_LIT>' ) <EOL> self . smtp_host = kwargs . get ( '<STR_LIT>' , '<STR_LIT:localhost>' ) <EOL> self . smtp_user = kwargs . get ( '<STR_LIT>' ) <EOL> self . smtp_password = kwargs . get ( '<STR_LIT>' ) <EOL> self . digest_len = <NUM_LIT> <EOL> def send_batch_notification ( self ) : <EOL> email = self . get_batch_email ( ) <EOL> if email : <EOL> self . send_email ( email ) <EOL> self . log_email ( email ) <EOL> def log_email ( self , email ) : <EOL> email_for_log = copy . copy ( email ) <EOL> email_for_log [ '<STR_LIT:to>' ] = self . COMMASPACE . join ( email [ '<STR_LIT:to>' ] ) <EOL> if len ( email_for_log [ '<STR_LIT:body>' ] ) > self . digest_len : <EOL> email_for_log [ '<STR_LIT:body>' ] = '<STR_LIT>' % email_for_log [ '<STR_LIT:body>' ] [ : self . digest_len ] <EOL> self . write_stderr ( "<STR_LIT>" % email_for_log ) <EOL> def get_batch_email ( self ) : <EOL> if len ( self . batchmsgs ) : <EOL> return { <EOL> '<STR_LIT:to>' : self . to_emails , <EOL> '<STR_LIT>' : self . from_email , <EOL> '<STR_LIT>' : self . subject , <EOL> '<STR_LIT:body>' : '<STR_LIT:\n>' . join ( self . get_batch_msgs ( ) ) , <EOL> } <EOL> return None <EOL> def send_email ( self , email ) : <EOL> msg = MIMEText ( email [ '<STR_LIT:body>' ] ) <EOL> if self . subject : <EOL> msg [ '<STR_LIT>' ] = email [ '<STR_LIT>' ] <EOL> msg [ '<STR_LIT>' ] = email [ '<STR_LIT>' ] <EOL> msg [ '<STR_LIT>' ] = self . COMMASPACE . join ( email [ '<STR_LIT:to>' ] ) <EOL> msg [ '<STR_LIT>' ] = formatdate ( ) <EOL> msg [ '<STR_LIT>' ] = make_msgid ( ) <EOL> try : <EOL> self . send_smtp ( msg , email [ '<STR_LIT:to>' ] ) <EOL> except Exception as e : <EOL> self . write_stderr ( "<STR_LIT>" % e ) <EOL> def send_smtp ( self , mime_msg , to_emails ) : <EOL> s = smtplib . SMTP ( self . smtp_host ) <EOL> try : <EOL> if self . smtp_user and self . smtp_password : <EOL> s . login ( self . smtp_user , self . smtp_password ) <EOL> s . sendmail ( mime_msg [ '<STR_LIT>' ] , to_emails , mime_msg . as_string ( ) ) <EOL> except : <EOL> s . quit ( ) <EOL> raise <EOL> s . quit ( ) </s>
