source: branches/testScripts/NBemail.py @ 635

Last change on this file since 635 was 635, checked in by jpfasano, 14 years ago

Updated to display path with error message.

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1#! /usr/bin/env python
2
3import os
4import sys
5from socket import gethostname
6import smtplib
7
8import NBuserConfig
9import NBprojectConfig
10import NBlogMessages
11
12#------------------------------------------------------------------------
13#
14# This file contains the functions that deal with email
15#
16#------------------------------------------------------------------------
17
18#------------------------------------------------------------------------
19# Send email typically about an error.
20#  project: coin project name
21#  cmd: command being exucuted. perhaps: "svn update", "./configure",
22#       "make".
23#  cmdMsgs: the messages generated by cmd.  This will typically contain
24#       errors issued by cmd.
25#------------------------------------------------------------------------
26def sendCmdMsgs(project,cmdMsgs,cmd):
27  curDir = os.getcwd()
28 
29  toAddrs = [unscrambleAddress(NBuserConfig.MY_EMAIL_ADDR)]
30  if NBprojectConfig.PROJECT_EMAIL_ADDRS.has_key(project) \
31     and \
32     NBuserConfig.SEND_MAIL_TO_PROJECT_MANAGER:
33    scrambledEmailAddress=NBprojectConfig.PROJECT_EMAIL_ADDRS[project]
34    unscrambledEmailAddress=unscrambleAddress(scrambledEmailAddress)
35    toAddrs.append(unscrambledEmailAddress)
36
37  subject = project + " problem when running '" + cmd +"'"
38
39  emailMsg  = "'" + cmd + "' from directory " + curDir + " failed.\n\n"
40
41  emailMsg += "Operating System: "+sys.platform+" "+os.name+"\n"
42  emailMsg += "Host name: "+gethostname()+"\n"
43
44  if os.environ.has_key("PROCESSOR_IDENTIFIER") :
45    emailMsg += "Processor: "+os.environ["PROCESSOR_IDENTIFIER"]+"\n"
46                                         
47  if os.environ.has_key("NUMBER_OF_PROCESSORS") :
48    emailMsg += "Number of processors: "+os.environ["NUMBER_OF_PROCESSORS"]+"\n"
49   
50  if os.environ.has_key("PATH") :
51    emailMsg += "PATH: "+os.environ["PATH"]+"\n"
52
53  emailMsg +="\n"
54
55  emailMsg += "stderr messages are:\n" 
56  emailMsg += cmdMsgs['stderr']
57  emailMsg += "\n\nstdout messages are:\n"
58  emailMsg += cmdMsgs['stdout']
59  if cmdMsgs.has_key('config.log') :
60    emailMsg += "\n\nconfig.log messages are:\n"
61    emailMsg += cmdMsgs['config.log']
62  send(toAddrs,subject,emailMsg)
63  NBlogMessages.writeMessage( "  email sent regarding "+project+" running '"+cmd+"'" )
64
65#------------------------------------------------------------------------
66# Send email
67#------------------------------------------------------------------------
68def send(toAddrs,subject,message):
69
70  sender = unscrambleAddress(NBuserConfig.SENDER_EMAIL_ADDR) 
71  msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
72       % (sender, ", ".join(toAddrs), subject))
73  msgWHeader += message
74 
75  # Get smpt server password
76  if os.path.isfile(NBuserConfig.SMTP_PASSWORD_FILENAME) :
77    pwFilePtr = open(NBuserConfig.SMTP_PASSWORD_FILENAME,'r')
78    smtppass  = pwFilePtr.read().strip()
79    #print smtppass
80    pwFilePtr.close()
81  else :
82    NBlogMessages.writeMessage( "Failure reading pwFileName=" + NBuserConfig.SMTP_PASSWORD_FILENAME )
83    sys.exit(1)
84   
85  session = smtplib.SMTP(NBuserConfig.SMTP_SERVER_NAME,NBuserConfig.SMTP_SERVER_PORT)
86  #session.set_debuglevel(1)
87  if NBuserConfig.SMTP_SSL_SERVER==1 :
88    session.ehlo('x')
89    session.starttls()
90    session.ehlo('x') 
91  session.login(unscrambleAddress(NBuserConfig.SMTP_USER_NAME),smtppass)
92
93  rc = session.sendmail(sender,toAddrs,msgWHeader)
94  if rc!={} :
95    NBlogMessages.writeMessage( 'session.sendmail rc='  )
96    NBlogMessages.writeMessage( rc )
97  session.quit()
98
99#------------------------------------------------------------------------
100# Decrypt email address
101#------------------------------------------------------------------------
102def unscrambleAddress( scrambledEmailAddress ) :
103  retVal = scrambledEmailAddress
104  retVal = retVal.replace(' _AT_ ','@')
105  retVal = retVal.replace(' _DOT_ ','.')
106  return retVal
107
Note: See TracBrowser for help on using the repository browser.