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 | |
---|
8 | import os |
---|
9 | import sys |
---|
10 | from socket import gethostname |
---|
11 | import smtplib |
---|
12 | |
---|
13 | import NBprojectConfig |
---|
14 | import NBlogMessages |
---|
15 | |
---|
16 | execfile('NBuserParametersDefault.py') |
---|
17 | execfile('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 | #------------------------------------------------------------------------ |
---|
33 | def 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 | if cmdMsgs.has_key('command history') : |
---|
84 | emailMsg += "\nHistory of commands called for this build:\n" |
---|
85 | for cmditem in cmdMsgs['command history'] : |
---|
86 | emailMsg += " "+cmditem+"\n" |
---|
87 | |
---|
88 | emailMsg +="\nnightlyBuildScript log:\n" |
---|
89 | emailMsg +=NBlogMessages.getMessages() |
---|
90 | |
---|
91 | emailMsg +="\n" |
---|
92 | |
---|
93 | emailMsg += "stderr messages are:\n" |
---|
94 | emailMsg += cmdMsgs['stderr'] |
---|
95 | emailMsg += "\n\nstdout messages are:\n" |
---|
96 | emailMsg += cmdMsgs['stdout'] |
---|
97 | if cmdMsgs.has_key('config.log') : |
---|
98 | emailMsg += "\n\nconfig.log messages are:\n" |
---|
99 | emailMsg += cmdMsgs['config.log'] |
---|
100 | |
---|
101 | send(toAddrs,subject,emailMsg) |
---|
102 | NBlogMessages.writeMessage( " email sent regarding "+project+" running '"+cmd+"'" ) |
---|
103 | |
---|
104 | #------------------------------------------------------------------------ |
---|
105 | # Send email (or store in a file) |
---|
106 | #------------------------------------------------------------------------ |
---|
107 | def send(toAddrs,subject,message): |
---|
108 | |
---|
109 | sender = unscrambleAddress(SENDER_EMAIL_ADDR) |
---|
110 | msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" |
---|
111 | % (sender, ", ".join(toAddrs), subject)) |
---|
112 | msgWHeader += message |
---|
113 | |
---|
114 | #store email in a file instead of sending |
---|
115 | if len(EMAIL_STOREFILE) > 0 and not EMAIL_STOREFILE.isspace() : |
---|
116 | NBlogMessages.writeMessage( ' store email in file '+EMAIL_STOREFILE) |
---|
117 | emailfile=open(NIGHTLY_BUILD_ROOT_DIR+'/'+EMAIL_STOREFILE, 'a') |
---|
118 | emailfile.write(msgWHeader) |
---|
119 | emailfile.write("\n============ EMAIL END ======================================\n") |
---|
120 | emailfile.close() |
---|
121 | return |
---|
122 | |
---|
123 | # Get smtp server password |
---|
124 | if os.path.isfile(SMTP_PASSWORD_FILENAME) : |
---|
125 | pwFilePtr = open(SMTP_PASSWORD_FILENAME,'r') |
---|
126 | smtppass = pwFilePtr.read().strip() |
---|
127 | #print smtppass |
---|
128 | pwFilePtr.close() |
---|
129 | else : |
---|
130 | NBlogMessages.writeMessage( "Failure reading pwFileName=" + SMTP_PASSWORD_FILENAME ) |
---|
131 | sys.exit(1) |
---|
132 | |
---|
133 | session = smtplib.SMTP(SMTP_SERVER_NAME,SMTP_SERVER_PORT) |
---|
134 | #session.set_debuglevel(1) |
---|
135 | if SMTP_SSL_SERVER==1 : |
---|
136 | session.ehlo('x') |
---|
137 | session.starttls() |
---|
138 | session.ehlo('x') |
---|
139 | session.login(unscrambleAddress(SMTP_USER_NAME),smtppass) |
---|
140 | |
---|
141 | rc = session.sendmail(sender,toAddrs,msgWHeader) |
---|
142 | if rc!={} : |
---|
143 | NBlogMessages.writeMessage( 'session.sendmail rc=' ) |
---|
144 | NBlogMessages.writeMessage( rc ) |
---|
145 | session.quit() |
---|
146 | |
---|
147 | #------------------------------------------------------------------------ |
---|
148 | # Decrypt email address |
---|
149 | #------------------------------------------------------------------------ |
---|
150 | def unscrambleAddress( scrambledEmailAddress ) : |
---|
151 | retVal = scrambledEmailAddress |
---|
152 | retVal = retVal.replace(' _AT_ ','@') |
---|
153 | retVal = retVal.replace(' _DOT_ ','.') |
---|
154 | return retVal |
---|
155 | |
---|