source: branches/testScripts/NBemail.py @ 706

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

Some cleanup dealing with removal of NBuserConfig.py

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