Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import email.mime.multipart | 5 import email.mime.multipart |
| 6 import email.mime.text | 6 import email.mime.text |
| 7 import os | 7 import os |
| 8 import smtplib | 8 import smtplib |
| 9 import sys | 9 import sys |
| 10 | 10 |
| 11 # How much of the logfile to send in the email (calculated from the end). | 11 # How much of the logfile to send in the email (calculated from the end). |
| 12 MAX_BYTES_TO_SEND = 50000 | 12 MAX_BYTES_TO_SEND = 50000 |
| 13 MAX_LINES_TO_SEND = 500 | 13 MAX_LINES_TO_SEND = 500 |
| 14 SUMMARY_LINES_TO_SEND = 20 | 14 SUMMARY_LINES_TO_SEND = 20 |
| 15 | 15 |
| 16 def ParseMailConfig(json_config): | 16 def ParseMailConfig(json_config): |
| 17 local_scope = {} | 17 local_scope = {} |
| 18 global_scope = {} | 18 global_scope = {} |
| 19 exec(json_config, global_scope, local_scope) | 19 exec(json_config, global_scope, local_scope) |
| 20 return local_scope['mail_config'] | 20 return local_scope['mail_config'] |
| 21 | 21 |
| 22 def SendMail(config, subject, body, log_attachment=None): | 22 def SendMail(config, subject, body, log_attachment=None): |
| 23 message = email.mime.multipart.MIMEMultipart() | 23 message = email.mime.multipart.MIMEMultipart() |
| 24 message['From'] = 'DevToolsFE Uploader <%s>' % config['FROM_ADDRESS'] | 24 message['From'] = 'DevToolsFE Uploader <%s>' % config['FROM_ADDRESS'] |
| 25 message['To'] = config['TO_ADDRESS'] | 25 message['To'] = config['TO_ADDRESS'] |
|
chenwilliam
2016/09/20 21:49:31
This needs to be a string because it's an email he
| |
| 26 message['Subject'] = subject | 26 message['Subject'] = subject |
| 27 message.attach(email.mime.text.MIMEText(body)) | 27 message.attach(email.mime.text.MIMEText(body)) |
| 28 if log_attachment: | 28 if log_attachment: |
| 29 attachment = email.mime.text.MIMEText(log_attachment) | 29 attachment = email.mime.text.MIMEText(log_attachment) |
| 30 attachment.add_header('Content-Disposition', 'attachment', | 30 attachment.add_header('Content-Disposition', 'attachment', |
| 31 filename='log.txt') | 31 filename='log.txt') |
| 32 message.attach(attachment) | 32 message.attach(attachment) |
| 33 | 33 |
| 34 server = smtplib.SMTP('smtp.gmail.com:587') | 34 server = smtplib.SMTP('smtp.gmail.com:587') |
| 35 server.starttls() | 35 server.starttls() |
| 36 server.login(config['FROM_ADDRESS'], config['PASSWORD']) | 36 server.login(config['FROM_ADDRESS'], config['PASSWORD']) |
| 37 server.sendmail(config['FROM_ADDRESS'], config['TO_ADDRESS'], message.as_strin g()) | 37 recipients = config['TO_ADDRESS'].split(',') |
| 38 server.sendmail(config['FROM_ADDRESS'], recipients, message.as_string()) | |
| 38 server.quit() | 39 server.quit() |
| 39 | 40 |
| 40 def SendErrorMail(config, reason, repeat, logpath): | 41 def SendErrorMail(config, reason, repeat, logpath): |
| 41 intro = ("The uploader encountered an error (during step '%s').\n" | 42 intro = ("The uploader encountered an error (during step '%s').\n" |
| 42 "This has happened %d time%s in a row.\n\n" | 43 "This has happened %d time%s in a row.\n\n" |
| 43 "Last %d lines of log (more of the log is attached):\n\n" | 44 "Last %d lines of log (more of the log is attached):\n\n" |
| 44 ) % (reason, repeat, 's' if repeat != 1 else '', | 45 ) % (reason, repeat, 's' if repeat != 1 else '', |
| 45 SUMMARY_LINES_TO_SEND) | 46 SUMMARY_LINES_TO_SEND) |
| 46 | 47 |
| 47 with open(logpath, 'rb') as log: | 48 with open(logpath, 'rb') as log: |
| 48 log.seek(0, os.SEEK_END) | 49 log.seek(0, os.SEEK_END) |
| 49 if log.tell() < MAX_BYTES_TO_SEND: | 50 if log.tell() < MAX_BYTES_TO_SEND: |
| 50 log.seek(0, os.SEEK_SET) | 51 log.seek(0, os.SEEK_SET) |
| 51 else: | 52 else: |
| 52 log.seek(-MAX_BYTES_TO_SEND, os.SEEK_END) | 53 log.seek(-MAX_BYTES_TO_SEND, os.SEEK_END) |
| 53 lines = log.readlines() | 54 lines = log.readlines() |
| 54 lines = lines[-MAX_LINES_TO_SEND:] | 55 lines = lines[-MAX_LINES_TO_SEND:] |
| 55 | 56 |
| 56 summary = ''.join(lines[-SUMMARY_LINES_TO_SEND:]) | 57 summary = ''.join(lines[-SUMMARY_LINES_TO_SEND:]) |
| 57 full_log = ''.join(lines) | 58 full_log = ''.join(lines) |
| 58 | 59 |
| 59 SendMail(config, 'Uploader failure: %s' % reason, intro + summary, full_log) | 60 SendMail(config, 'Uploader failure: %s' % reason, intro + summary, full_log) |
| 60 | 61 |
| 61 | 62 |
| 62 def SendAllClearMail(config): | 63 def SendAllClearMail(config): |
| 63 SendMail(config, | 64 SendMail(config, |
| 64 'Uploader all clear', | 65 'Uploader all clear', |
| 65 'The uploader cycled successfully; previous errors are fixed.') | 66 'The uploader cycled successfully; previous errors are fixed.') |
| OLD | NEW |