source: branches/testScripts/NBemail.py @ 634

Last change on this file since 634 was 626, checked in by jpfasano, 12 years ago

Added NBosCommand (nightly build operating system command).
This is an attempt to run commands and exe's in a subprocess, so if they
crash they don't bring down the script

  • Property svn:executable set to *
File size: 3.3 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\n"
43
44  emailMsg += "stderr messages are:\n" 
45  emailMsg += cmdMsgs['stderr']
46  emailMsg += "\n\nstdout messages are:\n"
47  emailMsg += cmdMsgs['stdout']
48  if cmdMsgs.has_key('config.log') :
49    emailMsg += "\n\nconfig.log messages are:\n"
50    emailMsg += cmdMsgs['config.log']
51  send(toAddrs,subject,emailMsg)
52  NBlogMessages.writeMessage( "  email sent regarding "+project+" running '"+cmd+"'" )
53
54#------------------------------------------------------------------------
55# Send email
56#------------------------------------------------------------------------
57def send(toAddrs,subject,message):
58
59  sender = unscrambleAddress(NBuserConfig.SENDER_EMAIL_ADDR) 
60  msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
61       % (sender, ", ".join(toAddrs), subject))
62  msgWHeader += message
63 
64  # Get smpt server password
65  if os.path.isfile(NBuserConfig.SMTP_PASSWORD_FILENAME) :
66    pwFilePtr = open(NBuserConfig.SMTP_PASSWORD_FILENAME,'r')
67    smtppass  = pwFilePtr.read().strip()
68    #print smtppass
69    pwFilePtr.close()
70  else :
71    NBlogMessages.writeMessage( "Failure reading pwFileName=" + NBuserConfig.SMTP_PASSWORD_FILENAME )
72    sys.exit(1)
73   
74  session = smtplib.SMTP(NBuserConfig.SMTP_SERVER_NAME,NBuserConfig.SMTP_SERVER_PORT)
75  #session.set_debuglevel(1)
76  if NBuserConfig.SMTP_SSL_SERVER==1 :
77    session.ehlo('x')
78    session.starttls()
79    session.ehlo('x') 
80  session.login(unscrambleAddress(NBuserConfig.SMTP_USER_NAME),smtppass)
81
82  rc = session.sendmail(sender,toAddrs,msgWHeader)
83  if rc!={} :
84    NBlogMessages.writeMessage( 'session.sendmail rc='  )
85    NBlogMessages.writeMessage( rc )
86  session.quit()
87
88#------------------------------------------------------------------------
89# Decrypt email address
90#------------------------------------------------------------------------
91def unscrambleAddress( scrambledEmailAddress ) :
92  retVal = scrambledEmailAddress
93  retVal = retVal.replace(' _AT_ ','@')
94  retVal = retVal.replace(' _DOT_ ','.')
95  return retVal
96
Note: See TracBrowser for help on using the repository browser.