summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/oe-build-perf-report-email.py
diff options
context:
space:
mode:
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>2017-10-25 14:39:38 +0300
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-12-04 17:23:57 +0000
commit852c71956be60225191c45357fb85e8feafadb1c (patch)
treec2ff218328af0f302295908c21175a9341356afa /scripts/contrib/oe-build-perf-report-email.py
parent254013ce5f346927184916848c369ebd4cb74952 (diff)
downloadpoky-852c71956be60225191c45357fb85e8feafadb1c.tar.gz
oe-build-perf-report-email.py: add images as MIME objects
Add images as separate MIME objects instead of directly embedding images in the html (as base64 encoded pngs). This makes the emails better suited for certain email servers/clients. (From OE-Core rev: fbbc84d9919d9cc18add03fc617637330721f5d9) Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com> Signed-off-by: Ross Burton <ross.burton@intel.com> Signed-off-by: Armin Kuster <akuster@mvista.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/contrib/oe-build-perf-report-email.py')
-rwxr-xr-xscripts/contrib/oe-build-perf-report-email.py42
1 files changed, 21 insertions, 21 deletions
diff --git a/scripts/contrib/oe-build-perf-report-email.py b/scripts/contrib/oe-build-perf-report-email.py
index 64e85c26ad..913847bbed 100755
--- a/scripts/contrib/oe-build-perf-report-email.py
+++ b/scripts/contrib/oe-build-perf-report-email.py
@@ -25,6 +25,7 @@ import socket
25import subprocess 25import subprocess
26import sys 26import sys
27import tempfile 27import tempfile
28from email.mime.image import MIMEImage
28from email.mime.multipart import MIMEMultipart 29from email.mime.multipart import MIMEMultipart
29from email.mime.text import MIMEText 30from email.mime.text import MIMEText
30 31
@@ -111,15 +112,6 @@ def decode_png(infile, outfile):
111 subprocess.check_output(['optipng', outfile], stderr=subprocess.STDOUT) 112 subprocess.check_output(['optipng', outfile], stderr=subprocess.STDOUT)
112 113
113 114
114def encode_png(pngfile):
115 """Encode png into a <img> html element"""
116 with open(pngfile, 'rb') as f:
117 data = f.read()
118
119 b64_data = base64.b64encode(data)
120 return '<img src="data:image/png;base64,' + b64_data.decode('utf-8') + '">\n'
121
122
123def mangle_html_report(infile, outfile, pngs): 115def mangle_html_report(infile, outfile, pngs):
124 """Mangle html file into a email compatible format""" 116 """Mangle html file into a email compatible format"""
125 paste = True 117 paste = True
@@ -144,9 +136,7 @@ def mangle_html_report(infile, outfile, pngs):
144 # Replace charts with <img> elements 136 # Replace charts with <img> elements
145 match = re.match('<div id="(?P<id>\w+)"', stripped) 137 match = re.match('<div id="(?P<id>\w+)"', stripped)
146 if match and match.group('id') in pngs: 138 if match and match.group('id') in pngs:
147 #f_out.write('<img src="{}">\n'.format(match.group('id') + '.png')) 139 f_out.write('<img src="cid:{}"\n'.format(match.group('id')))
148 png_file = os.path.join(png_dir, match.group('id') + '.png')
149 f_out.write(encode_png(png_file))
150 else: 140 else:
151 f_out.write(line) 141 f_out.write(line)
152 142
@@ -170,7 +160,7 @@ def scrape_html_report(report, outdir, phantomjs_extra_args=None):
170 stderr=subprocess.STDOUT) 160 stderr=subprocess.STDOUT)
171 161
172 pngs = [] 162 pngs = []
173 attachments = [] 163 images = []
174 for fname in os.listdir(tmpdir): 164 for fname in os.listdir(tmpdir):
175 base, ext = os.path.splitext(fname) 165 base, ext = os.path.splitext(fname)
176 if ext == '.png': 166 if ext == '.png':
@@ -178,7 +168,7 @@ def scrape_html_report(report, outdir, phantomjs_extra_args=None):
178 decode_png(os.path.join(tmpdir, fname), 168 decode_png(os.path.join(tmpdir, fname),
179 os.path.join(outdir, fname)) 169 os.path.join(outdir, fname))
180 pngs.append(base) 170 pngs.append(base)
181 attachments.append(fname) 171 images.append(fname)
182 elif ext in ('.html', '.htm'): 172 elif ext in ('.html', '.htm'):
183 report_file = fname 173 report_file = fname
184 else: 174 else:
@@ -188,11 +178,13 @@ def scrape_html_report(report, outdir, phantomjs_extra_args=None):
188 log.debug("Mangling html report file %s", report_file) 178 log.debug("Mangling html report file %s", report_file)
189 mangle_html_report(os.path.join(tmpdir, report_file), 179 mangle_html_report(os.path.join(tmpdir, report_file),
190 os.path.join(outdir, report_file), pngs) 180 os.path.join(outdir, report_file), pngs)
191 return report_file, attachments 181 return (os.path.join(outdir, report_file),
182 [os.path.join(outdir, i) for i in images])
192 finally: 183 finally:
193 shutil.rmtree(tmpdir) 184 shutil.rmtree(tmpdir)
194 185
195def send_email(text_fn, html_fn, subject, recipients, copy=[], blind_copy=[]): 186def send_email(text_fn, html_fn, image_fns, subject, recipients, copy=[],
187 blind_copy=[]):
196 """Send email""" 188 """Send email"""
197 # Generate email message 189 # Generate email message
198 text_msg = html_msg = None 190 text_msg = html_msg = None
@@ -201,8 +193,16 @@ def send_email(text_fn, html_fn, subject, recipients, copy=[], blind_copy=[]):
201 text_msg = MIMEText("Yocto build performance test report.\n" + 193 text_msg = MIMEText("Yocto build performance test report.\n" +
202 f.read(), 'plain') 194 f.read(), 'plain')
203 if html_fn: 195 if html_fn:
196 html_msg = msg = MIMEMultipart('related')
204 with open(html_fn) as f: 197 with open(html_fn) as f:
205 html_msg = MIMEText(f.read(), 'html') 198 html_msg.attach(MIMEText(f.read(), 'html'))
199 for img_fn in image_fns:
200 # Expect that content id is same as the filename
201 cid = os.path.splitext(os.path.basename(img_fn))[0]
202 with open(img_fn, 'rb') as f:
203 image_msg = MIMEImage(f.read())
204 image_msg['Content-ID'] = '<{}>'.format(cid)
205 html_msg.attach(image_msg)
206 206
207 if text_msg and html_msg: 207 if text_msg and html_msg:
208 msg = MIMEMultipart('alternative') 208 msg = MIMEMultipart('alternative')
@@ -251,10 +251,10 @@ def main(argv=None):
251 251
252 try: 252 try:
253 log.debug("Storing email parts in %s", outdir) 253 log.debug("Storing email parts in %s", outdir)
254 html_report = None 254 html_report = images = None
255 if args.html: 255 if args.html:
256 scrape_html_report(args.html, outdir, args.phantomjs_args) 256 html_report, images = scrape_html_report(args.html, outdir,
257 html_report = os.path.join(outdir, os.path.basename(args.html)) 257 args.phantomjs_args)
258 258
259 if args.to: 259 if args.to:
260 log.info("Sending email to %s", ', '.join(args.to)) 260 log.info("Sending email to %s", ', '.join(args.to))
@@ -262,7 +262,7 @@ def main(argv=None):
262 log.info("Copying to %s", ', '.join(args.cc)) 262 log.info("Copying to %s", ', '.join(args.cc))
263 if args.bcc: 263 if args.bcc:
264 log.info("Blind copying to %s", ', '.join(args.bcc)) 264 log.info("Blind copying to %s", ', '.join(args.bcc))
265 send_email(args.text, html_report, args.subject, 265 send_email(args.text, html_report, images, args.subject,
266 args.to, args.cc, args.bcc) 266 args.to, args.cc, args.bcc)
267 except subprocess.CalledProcessError as err: 267 except subprocess.CalledProcessError as err:
268 log.error("%s, with output:\n%s", str(err), err.output.decode()) 268 log.error("%s, with output:\n%s", str(err), err.output.decode())