Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Send build performance test report emails |
| 4 | # |
| 5 | # Copyright (c) 2017, Intel Corporation. |
| 6 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | import argparse |
| 11 | import base64 |
| 12 | import logging |
| 13 | import os |
| 14 | import pwd |
| 15 | import re |
| 16 | import shutil |
| 17 | import smtplib |
| 18 | import socket |
| 19 | import subprocess |
| 20 | import sys |
| 21 | import tempfile |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | from email.mime.text import MIMEText |
| 23 | |
| 24 | |
| 25 | # Setup logging |
| 26 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 27 | log = logging.getLogger('oe-build-perf-report') |
| 28 | |
| 29 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 30 | def 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 43 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 47 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 54 | |
| 55 | args = parser.parse_args(argv) |
| 56 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 57 | if not args.text: |
| 58 | parser.error("Please specify --text") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 59 | |
| 60 | return args |
| 61 | |
| 62 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 63 | def send_email(text_fn, subject, recipients, copy=[], blind_copy=[]): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 64 | # Generate email message |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 65 | with open(text_fn) as f: |
| 66 | msg = MIMEText("Yocto build performance test report.\n" + f.read(), 'plain') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 67 | |
| 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 74 | if copy: |
| 75 | msg['Cc'] = ', '.join(copy) |
| 76 | if blind_copy: |
| 77 | msg['Bcc'] = ', '.join(blind_copy) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 78 | msg['Subject'] = subject |
| 79 | |
| 80 | # Send email |
| 81 | with smtplib.SMTP('localhost') as smtp: |
| 82 | smtp.send_message(msg) |
| 83 | |
| 84 | |
| 85 | def 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 102 | if args.to: |
| 103 | log.info("Sending email to %s", ', '.join(args.to)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 104 | 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 108 | send_email(args.text, args.subject, args.to, args.cc, args.bcc) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 109 | except subprocess.CalledProcessError as err: |
| 110 | log.error("%s, with output:\n%s", str(err), err.output.decode()) |
| 111 | return 1 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | finally: |
| 113 | if not args.outdir: |
| 114 | log.debug("Wiping %s", outdir) |
| 115 | shutil.rmtree(outdir) |
| 116 | |
| 117 | return 0 |
| 118 | |
| 119 | |
| 120 | if __name__ == "__main__": |
| 121 | sys.exit(main()) |