From 06c24301759c65cbc445cdc0eb60fda461a5368c Mon Sep 17 00:00:00 2001 From: Hasan Wan Date: Thu, 9 Aug 2012 18:17:42 +0800 Subject: [PATCH] adapt mail sender to mail_sender jobs Change-Id: Id7d7a977b66c866fa31dcf22222386d49c3487da Signed-off-by: Hasan Wan --- buildcheck.py | 14 +++++++++++++- common/send_mail.py | 8 ++++++-- mail_sender.py | 10 ++++++---- submitobs.py | 16 +++++++++++++++- submitobs_orig.py | 15 ++++++++++++++- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/buildcheck.py b/buildcheck.py index eee5582..676fd95 100755 --- a/buildcheck.py +++ b/buildcheck.py @@ -19,7 +19,6 @@ from common import git from common import runner from common.envparas import * from common import buildservice -from common.send_mail import sendmail import gbp.rpm import gbp @@ -35,6 +34,7 @@ envparas = ['JENKINS_HOME', 'GERRIT_PATCHSET_NUMBER', 'GERRIT_BRANCH', 'BUILD_NUMBER', + 'BUILD_TAG', 'OBS_USERNAME', 'OBS_API_URL', 'OBS_OSCRC_PATH'] @@ -42,6 +42,18 @@ export(envparas, locals()) GERRIT_CMD = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME) GIT_URL = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT) +def sendmail(subject, body, mail_from, mail_to): + """ Prepare the envorinment parameter for trigger mail_sender job""" + mail_env = {} + mail_env['subject'] = subject + mail_env['body'] = body.replace('\n', '\\n') + mail_env['From'] = mail_from + mail_env['To'] = mail_to + + with open('%s.env' %(BUILD_TAG),'w') as f: + for key in mail_env.keys(): + f.write('%s=%s\n' %(key, mail_env[key])) + def end(rc = 0): shutil.rmtree(tmpdir) exit(rc) diff --git a/common/send_mail.py b/common/send_mail.py index 537c9a0..c2e013b 100644 --- a/common/send_mail.py +++ b/common/send_mail.py @@ -11,13 +11,17 @@ from email.Header import Header from email.Utils import parseaddr, formataddr def sendmail(from_email, to, msg, smtp_server): + + if type(to) != list: + to = [to] + try: s = smtplib.SMTP(smtp_server) - s.sendmail(from_email, to, msg.as_string()) + s.sendmail(from_email, ','.join(to), msg.as_string()) s.quit() print "Email to %s sent succeeded" %(to) except smtplib.SMTPException, e: - print "Error: unable to send email: %s" % ( e ) + raise Exception, "Error: unable to send email: %s" % ( e ) def makemail(subject = '', body = '', from_email = None, to = [], cc = [], attachment = None, extra_headers = None): diff --git a/mail_sender.py b/mail_sender.py index 3e30382..406d833 100644 --- a/mail_sender.py +++ b/mail_sender.py @@ -10,10 +10,6 @@ envparas = ['From', 'Cc', 'subject', 'body', - 'targetpackage', - 'template', - 'reqinfo', - 'comment', 'attachment', 'extra_headers', 'SMTP_SERVER'] @@ -21,6 +17,12 @@ envparas = ['From', export(envparas, locals()) +if To.startswith('[') and To.endswith(']'): + To = eval(To) + +if Cc.startswith('[') and Cc.endswith(']'): + Cc = eval(Cc) + message = makemail(subject = subject, body = body, from_email = From, to = To, cc = Cc, attachment = attachment, extra_headers = extra_headers) diff --git a/submitobs.py b/submitobs.py index 7ef7eb1..0af8d64 100755 --- a/submitobs.py +++ b/submitobs.py @@ -19,7 +19,6 @@ from common import obspkg from common.envparas import * from common import errors from common import mysql -from common.send_mail import sendmail import gbp.rpm @@ -42,6 +41,7 @@ envparas = ['GERRIT_EVENT_TYPE', 'MYSQL_HOSTNAME', 'MYSQL_USERNAME', 'MYSQL_PASSWORD', + 'BUILD_TAG', 'MYSQL_DB_NAME'] export(envparas, locals()) GERRIT_CMD = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME) @@ -59,6 +59,20 @@ ret = { 'rc' : {'success' : 0, 'failure' : 0, 'retry' : 1}, 'newRev' : GERRIT_NEWREV}}, } + + +def sendmail(subject, body, mail_from, mail_to): + """ Prepare the envorinment parameter for trigger mail_sender job""" + mail_env = {} + mail_env['subject'] = subject + mail_env['body'] = body.replace('\n', '\\n') + mail_env['From'] = mail_from + mail_env['To'] = mail_to + + with open('%s.env' %(BUILD_TAG),'w') as f: + for key in mail_env.keys(): + f.write('%s=%s\n' %(key, mail_env[key])) + def end(result = 'success'): print 'execute result: %s' % result db = mysql.Database(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DB_NAME) diff --git a/submitobs_orig.py b/submitobs_orig.py index 272e677..56f1b76 100755 --- a/submitobs_orig.py +++ b/submitobs_orig.py @@ -19,7 +19,6 @@ from common import obspkg from common.envparas import * from common import errors from common import mysql -from common.send_mail import sendmail import gbp.rpm @@ -39,6 +38,7 @@ envparas = ['GERRIT_EVENT_TYPE', 'JENKINS_HOME', 'OBS_API_URL', 'OBS_OSCRC_PATH', + 'BUILD_TAG', 'MYSQL_HOSTNAME', 'MYSQL_USERNAME', 'MYSQL_PASSWORD', @@ -58,6 +58,19 @@ ret = { 'rc' : {'success' : 0, 'failure' : 0, 'retry' : 1}, 'newRev' : GERRIT_NEWREV}}, } + +def sendmail(subject, body, mail_from, mail_to): + """ Prepare the envorinment parameter for trigger mail_sender job""" + mail_env = {} + mail_env['subject'] = subject + mail_env['body'] = body.replace('\n', '\\n') + mail_env['From'] = mail_from + mail_env['To'] = mail_to + + with open('%s.env' %(BUILD_TAG),'w') as f: + for key in mail_env.keys(): + f.write('%s=%s\n' %(key, mail_env[key])) + def end(result = 'success'): print 'execute result: %s' % result # cleanup workspace -- 2.7.4