source: branches/testScripts/NBemail.py @ 740

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

fixed more typos

  • 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('make test') :
79    emailMsg += "\n\n'make test' problem:\n"
80    emailMsg += cmdMsgs['make test']
81    emailMsg += "\n" 
82  if cmdMsgs.has_key('unitTest') :
83    emailMsg += "\n\nDetected problem:\n"
84    emailMsg += cmdMsgs['unitTest']
85    emailMsg += "\n" 
86
87  emailMsg +="\n"
88
89  emailMsg += "stderr messages are:\n" 
90  emailMsg += cmdMsgs['stderr']
91  emailMsg += "\n\nstdout messages are:\n"
92  emailMsg += cmdMsgs['stdout']
93  if cmdMsgs.has_key('config.log') :
94    emailMsg += "\n\nconfig.log messages are:\n"
95    emailMsg += cmdMsgs['config.log']
96
97  send(toAddrs,subject,emailMsg)
98  NBlogMessages.writeMessage( "  email sent regarding "+project+" running '"+cmd+"'" )
99
100#------------------------------------------------------------------------
101# Send email (or store in a file)
102#------------------------------------------------------------------------
103def send(toAddrs,subject,message):
104
105  sender = unscrambleAddress(SENDER_EMAIL_ADDR) 
106  msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
107       % (sender, ", ".join(toAddrs), subject))
108  msgWHeader += message
109
110  #store email in a file instead of sending
111  if len(EMAIL_STOREFILE) > 0 and not EMAIL_STOREFILE.isspace() :
112    NBlogMessages.writeMessage( '  store email in file '+EMAIL_STOREFILE)
113    emailfile=open(NIGHTLY_BUILD_ROOT_DIR+'/'+EMAIL_STOREFILE, 'a')
114    emailfile.write(msgWHeader)
115    emailfile.write("\n============ EMAIL END ======================================\n")
116    emailfile.close()
117    return
118
119  # Get smtp server password
120  if os.path.isfile(SMTP_PASSWORD_FILENAME) :
121    pwFilePtr = open(SMTP_PASSWORD_FILENAME,'r')
122    smtppass  = pwFilePtr.read().strip()
123    #print smtppass
124    pwFilePtr.close()
125  else :
126    NBlogMessages.writeMessage( "Failure reading pwFileName=" + SMTP_PASSWORD_FILENAME )
127    sys.exit(1)
128   
129  session = smtplib.SMTP(SMTP_SERVER_NAME,SMTP_SERVER_PORT)
130  #session.set_debuglevel(1)
131  if SMTP_SSL_SERVER==1 :
132    session.ehlo('x')
133    session.starttls()
134    session.ehlo('x') 
135  session.login(unscrambleAddress(SMTP_USER_NAME),smtppass)
136
137  rc = session.sendmail(sender,toAddrs,msgWHeader)
138  if rc!={} :
139    NBlogMessages.writeMessage( 'session.sendmail rc='  )
140    NBlogMessages.writeMessage( rc )
141  session.quit()
142
143#------------------------------------------------------------------------
144# Decrypt email address
145#------------------------------------------------------------------------
146def unscrambleAddress( scrambledEmailAddress ) :
147  retVal = scrambledEmailAddress
148  retVal = retVal.replace(' _AT_ ','@')
149  retVal = retVal.replace(' _DOT_ ','.')
150  return retVal
151
Note: See TracBrowser for help on using the repository browser.