source: branches/testScripts/NBemail.py @ 713

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

modified email message a little

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1#! /usr/bin/env python
2
3import os
4import sys
5from socket import gethostname
6import smtplib
7
8import NBprojectConfig
9import NBlogMessages
10
11execfile('NBuserParametersDefault.py')
12execfile('NBuserParameters.py')
13
14#------------------------------------------------------------------------
15#
16# This file contains the functions that deal with email
17#
18#------------------------------------------------------------------------
19
20#------------------------------------------------------------------------
21# Send email typically about an error.
22#  project: coin project name
23#  cmd: command being executed. perhaps: "svn update", "./configure",
24#       "make".
25#  cmdMsgs: the messages generated by cmd.  This will typically contain
26#       errors issued by cmd and additional information about the build.
27#------------------------------------------------------------------------
28def sendCmdMsgs(project,cmdMsgs,cmd):
29  curDir = os.getcwd()
30
31  toAddrs = [unscrambleAddress(MY_EMAIL_ADDR)]
32  if SEND_MAIL_TO_PROJECT_MANAGER and NBprojectConfig.PROJECT_EMAIL_ADDRS.has_key(project) :
33    scrambledEmailAddress=NBprojectConfig.PROJECT_EMAIL_ADDRS[project]
34    unscrambledEmailAddress=unscrambleAddress(scrambledEmailAddress)
35    toAddrs.append(unscrambledEmailAddress)
36
37  subject = "NightlyBuild on "+sys.platform+": "+project
38  if cmdMsgs.has_key('svn version') :
39    subject += " "+cmdMsgs['svn version']
40  subject+=" Problem with: '" + cmd +"'"
41
42  emailMsg = "Subject: "+subject+"\n\n"
43  emailMsg += "Dear "+project+" Project Manager,\n\n" \
44   +"The nightly build tests scripts reported a problem when building project "+project
45  if cmdMsgs.has_key('svn version') :
46    emailMsg += " from subversion version "+cmdMsgs['svn version']+'\n'
47  emailMsg += ". The failing command was\n\n\t"+cmd+"\n\n" \
48   +"Details on the problem can be found below.\n" \
49   +"The cause of the problem may be from one of the projects that "\
50   +project+" depends on (externals).\n" \
51   +"We hope you find this report useful.\n\n"
52
53  if cmdMsgs.has_key("configure flags") :
54    emailMsg += "Flags for configure: "+cmdMsgs['configure flags']+'\n'
55
56  emailMsg += "Operating System: "+sys.platform+" "+os.name+"\n"
57  emailMsg += "Host name: "+gethostname()+"\n"
58
59  if os.environ.has_key("HOSTTYPE") :
60    emailMsg += "Host type: "+os.environ["HOSTTYPE"]+"\n"
61  if os.environ.has_key("PROCESSOR_IDENTIFIER") :
62    emailMsg += "Processor: "+os.environ["PROCESSOR_IDENTIFIER"]+"\n"
63  if os.environ.has_key("NUMBER_OF_PROCESSORS") :
64    emailMsg += "Number of processors: "+os.environ["NUMBER_OF_PROCESSORS"]+"\n"
65
66  if os.environ.has_key("PATH") :
67    emailMsg += "PATH: "+os.environ["PATH"]+"\n"
68
69  emailMsg += "Directory: "+curDir+'\n'
70
71  if cmdMsgs.has_key('make test') :
72    emailMsg += "\n\n'make test' problem:\n"
73    emailMsg += cmdMsgs['make test']
74    emailMsg += "\n" 
75  if cmdMsgs.has_key('unitTest') :
76    emailMsg += "\n\nDetected problem:\n"
77    emailMsg += cmdMsgs['unitTest']
78    emailMsg += "\n" 
79
80  emailMsg +="\n"
81
82  emailMsg += "stderr messages are:\n" 
83  emailMsg += cmdMsgs['stderr']
84  emailMsg += "\n\nstdout messages are:\n"
85  emailMsg += cmdMsgs['stdout']
86  if cmdMsgs.has_key('config.log') :
87    emailMsg += "\n\nconfig.log messages are:\n"
88    emailMsg += cmdMsgs['config.log']
89
90  send(toAddrs,subject,emailMsg)
91  NBlogMessages.writeMessage( "  email sent regarding "+project+" running '"+cmd+"'" )
92
93#------------------------------------------------------------------------
94# Send email (or store in a file)
95#------------------------------------------------------------------------
96def send(toAddrs,subject,message):
97
98  sender = unscrambleAddress(SENDER_EMAIL_ADDR) 
99  msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
100       % (sender, ", ".join(toAddrs), subject))
101  msgWHeader += message
102
103  #store email in a file instead of sending
104  if len(EMAIL_STOREFILE) > 0 and not EMAIL_STOREFILE.isspace() :
105    NBlogMessages.writeMessage( '  store email in file '+EMAIL_STOREFILE)
106    emailfile=open(NIGHTLY_BUILD_ROOT_DIR+'/'+EMAIL_STOREFILE, 'a')
107    emailfile.write(msgWHeader)
108    emailfile.write("\n============ EMAIL END ======================================\n")
109    emailfile.close()
110    return
111
112  # Get smtp server password
113  if os.path.isfile(SMTP_PASSWORD_FILENAME) :
114    pwFilePtr = open(SMTP_PASSWORD_FILENAME,'r')
115    smtppass  = pwFilePtr.read().strip()
116    #print smtppass
117    pwFilePtr.close()
118  else :
119    NBlogMessages.writeMessage( "Failure reading pwFileName=" + SMTP_PASSWORD_FILENAME )
120    sys.exit(1)
121   
122  session = smtplib.SMTP(SMTP_SERVER_NAME,SMTP_SERVER_PORT)
123  #session.set_debuglevel(1)
124  if SMTP_SSL_SERVER==1 :
125    session.ehlo('x')
126    session.starttls()
127    session.ehlo('x') 
128  session.login(unscrambleAddress(SMTP_USER_NAME),smtppass)
129
130  rc = session.sendmail(sender,toAddrs,msgWHeader)
131  if rc!={} :
132    NBlogMessages.writeMessage( 'session.sendmail rc='  )
133    NBlogMessages.writeMessage( rc )
134  session.quit()
135
136#------------------------------------------------------------------------
137# Decrypt email address
138#------------------------------------------------------------------------
139def unscrambleAddress( scrambledEmailAddress ) :
140  retVal = scrambledEmailAddress
141  retVal = retVal.replace(' _AT_ ','@')
142  retVal = retVal.replace(' _DOT_ ','.')
143  return retVal
144
Note: See TracBrowser for help on using the repository browser.