oe-build-perf-report-email.py 3.6 KB

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