blob: 7192113c2814761065f5afc6999f0dae37cbe024 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#!/usr/bin/python3
2#
3# Send build performance test report emails
4#
5# Copyright (c) 2017, Intel Corporation.
6#
Brad Bishopc342db32019-05-15 21:57:59 -04007# SPDX-License-Identifier: GPL-2.0-only
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008#
Brad Bishopc342db32019-05-15 21:57:59 -04009
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010import argparse
11import base64
12import logging
13import os
14import pwd
15import re
16import shutil
17import smtplib
18import socket
19import subprocess
20import sys
21import tempfile
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022from email.mime.text import MIMEText
23
24
25# Setup logging
26logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
27log = logging.getLogger('oe-build-perf-report')
28
29
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030def parse_args(argv):
31 """Parse command line arguments"""
32 description = """Email build perf test report"""
33 parser = argparse.ArgumentParser(
34 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
35 description=description)
36
37 parser.add_argument('--debug', '-d', action='store_true',
38 help="Verbose logging")
39 parser.add_argument('--quiet', '-q', action='store_true',
40 help="Only print errors")
41 parser.add_argument('--to', action='append',
42 help="Recipients of the email")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043 parser.add_argument('--cc', action='append',
44 help="Carbon copy recipients of the email")
45 parser.add_argument('--bcc', action='append',
46 help="Blind carbon copy recipients of the email")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 parser.add_argument('--subject', default="Yocto build perf test report",
48 help="Email subject")
49 parser.add_argument('--outdir', '-o',
50 help="Store files in OUTDIR. Can be used to preserve "
51 "the email parts")
52 parser.add_argument('--text',
53 help="Plain text message")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054
55 args = parser.parse_args(argv)
56
Andrew Geissler9aee5002022-03-30 16:27:02 +000057 if not args.text:
58 parser.error("Please specify --text")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059
60 return args
61
62
Andrew Geissler9aee5002022-03-30 16:27:02 +000063def send_email(text_fn, subject, recipients, copy=[], blind_copy=[]):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064 # Generate email message
Andrew Geissler9aee5002022-03-30 16:27:02 +000065 with open(text_fn) as f:
66 msg = MIMEText("Yocto build performance test report.\n" + f.read(), 'plain')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050067
68 pw_data = pwd.getpwuid(os.getuid())
69 full_name = pw_data.pw_gecos.split(',')[0]
70 email = os.environ.get('EMAIL',
71 '{}@{}'.format(pw_data.pw_name, socket.getfqdn()))
72 msg['From'] = "{} <{}>".format(full_name, email)
73 msg['To'] = ', '.join(recipients)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 if copy:
75 msg['Cc'] = ', '.join(copy)
76 if blind_copy:
77 msg['Bcc'] = ', '.join(blind_copy)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050078 msg['Subject'] = subject
79
80 # Send email
81 with smtplib.SMTP('localhost') as smtp:
82 smtp.send_message(msg)
83
84
85def main(argv=None):
86 """Script entry point"""
87 args = parse_args(argv)
88 if args.quiet:
89 log.setLevel(logging.ERROR)
90 if args.debug:
91 log.setLevel(logging.DEBUG)
92
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 if args.outdir:
94 outdir = args.outdir
95 if not os.path.exists(outdir):
96 os.mkdir(outdir)
97 else:
98 outdir = tempfile.mkdtemp(dir='.')
99
100 try:
101 log.debug("Storing email parts in %s", outdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 if args.to:
103 log.info("Sending email to %s", ', '.join(args.to))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104 if args.cc:
105 log.info("Copying to %s", ', '.join(args.cc))
106 if args.bcc:
107 log.info("Blind copying to %s", ', '.join(args.bcc))
Andrew Geissler9aee5002022-03-30 16:27:02 +0000108 send_email(args.text, args.subject, args.to, args.cc, args.bcc)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 except subprocess.CalledProcessError as err:
110 log.error("%s, with output:\n%s", str(err), err.output.decode())
111 return 1
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 finally:
113 if not args.outdir:
114 log.debug("Wiping %s", outdir)
115 shutil.rmtree(outdir)
116
117 return 0
118
119
120if __name__ == "__main__":
121 sys.exit(main())