source: branches/testScripts/NBemail.py @ 761

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

more clean up. email should now include nightlyBuild log messages

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