Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # patchtest: execute all unittest test cases discovered for a single patch |
| 6 | # Note that this script is currently under development and has been |
| 7 | # hard-coded with default values for testing purposes. This script |
| 8 | # should not be used without changing the default recipient, at minimum. |
| 9 | # |
| 10 | # Copyright (C) 2023 BayLibre Inc. |
| 11 | # |
| 12 | # SPDX-License-Identifier: GPL-2.0-only |
| 13 | # |
| 14 | |
| 15 | import argparse |
| 16 | import boto3 |
| 17 | import configparser |
| 18 | import mailbox |
| 19 | import os |
| 20 | import re |
| 21 | import sys |
| 22 | |
| 23 | greeting = """Thank you for your submission. Patchtest identified one |
| 24 | or more issues with the patch. Please see the log below for |
| 25 | more information:\n\n---\n""" |
| 26 | |
| 27 | suggestions = """\n---\n\nPlease address the issues identified and |
| 28 | submit a new revision of the patch, or alternatively, reply to this |
| 29 | email with an explanation of why the patch should be accepted. If you |
| 30 | believe these results are due to an error in patchtest, please submit a |
| 31 | bug at https://bugzilla.yoctoproject.org/ (use the 'Patchtest' category |
| 32 | under 'Yocto Project Subprojects'). For more information on specific |
| 33 | failures, see: https://wiki.yoctoproject.org/wiki/Patchtest. Thank |
| 34 | you!""" |
| 35 | |
| 36 | parser = argparse.ArgumentParser(description="Send patchtest results to a submitter for a given patch") |
| 37 | parser.add_argument("-p", "--patch", dest="patch", required=True, help="The patch file to summarize") |
| 38 | args = parser.parse_args() |
| 39 | |
| 40 | if not os.path.exists(args.patch): |
| 41 | print(f"Patch '{args.patch}' not found - did you provide the right path?") |
| 42 | sys.exit(1) |
| 43 | elif not os.path.exists(args.patch + ".testresult"): |
| 44 | print(f"Found patch '{args.patch}' but '{args.patch}.testresult' was not present. Have you run patchtest on the patch?") |
| 45 | sys.exit(1) |
| 46 | |
| 47 | result_file = args.patch + ".testresult" |
| 48 | result_basename = os.path.basename(args.patch) |
| 49 | testresult = None |
| 50 | |
| 51 | with open(result_file, "r") as f: |
| 52 | testresult = f.read() |
| 53 | |
| 54 | # we know these patch files will only contain a single patch, so only |
| 55 | # worry about the first element for getting the subject |
| 56 | mbox = mailbox.mbox(args.patch) |
| 57 | mbox_subject = mbox[0]['subject'] |
| 58 | subject_line = f"Patchtest results for {mbox_subject}" |
| 59 | |
| 60 | # extract the submitter email address and use it as the reply address |
| 61 | # for the results |
| 62 | reply_address = mbox[0]['from'] |
| 63 | |
| 64 | # extract the message ID and use that as the in-reply-to address |
| 65 | in_reply_to = re.findall("<(.*)>", mbox[0]['Message-ID'])[0] |
| 66 | |
| 67 | # the address the results email is sent from |
| 68 | from_address = "patchtest@automation.yoctoproject.org" |
| 69 | |
| 70 | # mailing list to CC |
| 71 | cc_address = "openembedded-core@lists.openembedded.org" |
| 72 | |
| 73 | if "FAIL" in testresult: |
| 74 | reply_contents = None |
| 75 | if len(max(open(result_file, 'r'), key=len)) > 220: |
| 76 | warning = "Tests failed for the patch, but the results log could not be processed due to excessive result line length." |
| 77 | reply_contents = greeting + warning + suggestions |
| 78 | else: |
| 79 | reply_contents = greeting + testresult + suggestions |
| 80 | |
| 81 | ses_client = boto3.client('ses', region_name='us-west-2') |
| 82 | raw_data = 'From: ' + from_address + '\nTo: ' + reply_address + \ |
| 83 | '\nCC: ' + cc_address + '\nSubject:' + subject_line + \ |
| 84 | '\nIn-Reply-To:' + in_reply_to + \ |
| 85 | '\nMIME-Version: 1.0" + \ |
| 86 | "\nContent-type: Multipart/Mixed;boundary="NextPart"\n\n--NextPart\nContent-Type: text/plain\n\n' + \ |
| 87 | reply_contents + '\n\n--NextPart' |
| 88 | response = ses_client.send_raw_email( |
| 89 | Source="patchtest@automation.yoctoproject.org", |
| 90 | RawMessage={ |
| 91 | "Data": raw_data, |
| 92 | }, |
| 93 | ) |
| 94 | |
| 95 | else: |
| 96 | print(f"No failures identified for {args.patch}.") |