source: branches/testScripts/NBemail.py @ 679

Last change on this file since 679 was 679, checked in by stefan, 13 years ago

forgot to take debug message out

  • Property svn:executable set to *
File size: 4.5 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 executed. 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 = "NightlyBuild: "+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  if cmdMsgs.has_key('make test') :
54    emailMsg += "\n\n'make test' problem:\n"
55    emailMsg += cmdMsgs['make test']
56    emailMsg += "\n" 
57  if cmdMsgs.has_key('unitTest') :
58    emailMsg += "\n\nDetected problem:\n"
59    emailMsg += cmdMsgs['unitTest']
60    emailMsg += "\n" 
61
62  emailMsg +="\n"
63
64  emailMsg += "stderr messages are:\n" 
65  emailMsg += cmdMsgs['stderr']
66  emailMsg += "\n\nstdout messages are:\n"
67  emailMsg += cmdMsgs['stdout']
68  if cmdMsgs.has_key('config.log') :
69    emailMsg += "\n\nconfig.log messages are:\n"
70    emailMsg += cmdMsgs['config.log']
71
72  send(toAddrs,subject,emailMsg)
73  NBlogMessages.writeMessage( "  email sent regarding "+project+" running '"+cmd+"'" )
74
75#------------------------------------------------------------------------
76# Send email (or store in a file)
77#------------------------------------------------------------------------
78def send(toAddrs,subject,message):
79
80  sender = unscrambleAddress(NBuserConfig.SENDER_EMAIL_ADDR) 
81  msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
82       % (sender, ", ".join(toAddrs), subject))
83  msgWHeader += message
84
85  #store email in a file instead of sending
86  if len(NBuserConfig.STORE_EMAIL) > 0 and not NBuserConfig.STORE_EMAIL.isspace() :
87    NBlogMessages.writeMessage( '  store email in file '+NBuserConfig.STORE_EMAIL)
88    emailfile=open(NBuserConfig.NIGHTLY_BUILD_ROOT_DIR+'/'+NBuserConfig.STORE_EMAIL, 'a')
89    emailfile.write(msgWHeader)
90    emailfile.write("\n============ EMAIL END ======================================\n")
91    emailfile.close()
92    return
93
94  # Get smtp server password
95  if os.path.isfile(NBuserConfig.SMTP_PASSWORD_FILENAME) :
96    pwFilePtr = open(NBuserConfig.SMTP_PASSWORD_FILENAME,'r')
97    smtppass  = pwFilePtr.read().strip()
98    #print smtppass
99    pwFilePtr.close()
100  else :
101    NBlogMessages.writeMessage( "Failure reading pwFileName=" + NBuserConfig.SMTP_PASSWORD_FILENAME )
102    sys.exit(1)
103   
104  session = smtplib.SMTP(NBuserConfig.SMTP_SERVER_NAME,NBuserConfig.SMTP_SERVER_PORT)
105  #session.set_debuglevel(1)
106  if NBuserConfig.SMTP_SSL_SERVER==1 :
107    session.ehlo('x')
108    session.starttls()
109    session.ehlo('x') 
110  session.login(unscrambleAddress(NBuserConfig.SMTP_USER_NAME),smtppass)
111
112  rc = session.sendmail(sender,toAddrs,msgWHeader)
113  if rc!={} :
114    NBlogMessages.writeMessage( 'session.sendmail rc='  )
115    NBlogMessages.writeMessage( rc )
116  session.quit()
117
118#------------------------------------------------------------------------
119# Decrypt email address
120#------------------------------------------------------------------------
121def unscrambleAddress( scrambledEmailAddress ) :
122  retVal = scrambledEmailAddress
123  retVal = retVal.replace(' _AT_ ','@')
124  retVal = retVal.replace(' _DOT_ ','.')
125  return retVal
126
Note: See TracBrowser for help on using the repository browser.