Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2022 Google LLC. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | |
| 7 | # quick hacky script to check patches if they are candidates for lts. it checks |
| 8 | # only the non-merge commits. |
| 9 | |
| 10 | import pkg_resources |
| 11 | import os |
| 12 | import git |
| 13 | import re |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 14 | import sys |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 15 | import argparse |
| 16 | from io import StringIO |
| 17 | pkg_resources.require("unidiff>=0.7.4") |
| 18 | from unidiff import PatchSet |
| 19 | |
| 20 | global_debug = False |
| 21 | def debug_print(*args, **kwargs): |
| 22 | global global_var |
| 23 | if global_debug: |
| 24 | print(*args, **kwargs) |
| 25 | |
| 26 | def contains_re(pf, tok): |
| 27 | for hnk in pf: |
| 28 | for ln in hnk: |
| 29 | if ln.is_context: |
| 30 | continue |
| 31 | # here means the line is either added or removed |
| 32 | txt = ln.value.strip() |
| 33 | if tok.search(txt) is not None: |
| 34 | return True |
| 35 | |
| 36 | return False |
| 37 | |
| 38 | def process_ps(ps): |
| 39 | score = 0 |
| 40 | |
| 41 | cpu_tok = re.compile(CPU_PATH_TOKEN) |
| 42 | doc_tok = re.compile(DOC_PATH_TOKEN) |
| 43 | |
| 44 | for pf in ps: |
| 45 | if pf.is_binary_file or not pf.is_modified_file: |
| 46 | continue |
| 47 | if cpu_tok.search(pf.path) is not None: |
| 48 | debug_print("* change found in cpu path:", pf.path); |
| 49 | cpu_tok = re.compile(CPU_ERRATA_TOKEN) |
| 50 | if contains_re(pf, cpu_tok): |
| 51 | score = score + 1 |
| 52 | debug_print(" found", CPU_ERRATA_TOKEN) |
| 53 | |
| 54 | if doc_tok.search(pf.path) is not None: |
| 55 | debug_print("* change found in macros doc path:", pf.path); |
| 56 | doc_tok = re.compile(DOC_ERRATA_TOKEN) |
| 57 | if contains_re(pf, doc_tok): |
| 58 | score = score + 1 |
| 59 | debug_print(" found", DOC_ERRATA_TOKEN) |
| 60 | |
| 61 | return score |
| 62 | |
| 63 | SUBJECT_TOKENS = r'fix\(cpus\)|revert\(cpus\)|fix\(errata\)|\(security\)' |
| 64 | CPU_PATH_TOKEN = r'lib/cpus/aarch(32|64)/.*\.S' |
| 65 | CPU_ERRATA_TOKEN = r'^report_errata ERRATA_' |
| 66 | DOC_PATH_TOKEN = r'docs/design/cpu-specific-build-macros.rst' |
| 67 | DOC_ERRATA_TOKEN = r'^^-\s*``ERRATA_' |
Okash Khawaja | 66a3a7e | 2023-01-24 11:06:16 +0000 | [diff] [blame] | 68 | # REBASE_DEPTH is number of commits from tip of integration branch that we need |
| 69 | # to check to find the commit that the current patch set is based on |
| 70 | REBASE_DEPTH = 50 |
| 71 | # MAX_PATCHSET_DEPTH is the maximum number of patches that we expect in the current |
| 72 | # patch set. for each commit in the patch set we will look at past REBASE_DEPTH commits |
| 73 | # of integration branch. if there is a match we'd know the current patch set was based |
| 74 | # off of that matching commit. This is not necessarily the optimal method but I'm not |
| 75 | # familiar with gerrit API. If there is a way to do this better we should implement that. |
| 76 | MAX_PATCHSET_DEPTH = 50 |
| 77 | CHECK_AGAINST = 'integration' |
| 78 | TO_CHECK = 'to_check' |
| 79 | |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 80 | |
| 81 | ## TODO: for case like 921081049ec3 where we need to refactor first for security |
| 82 | # patch to be applied then we should: |
| 83 | # 1. find the security patch |
| 84 | # 2. from that patch find CVE number if any |
| 85 | # 3. look for all patches that contain that CVE number in commit message |
| 86 | |
| 87 | ## TODO: similar to errata macros and rst file additions, we have CVE macros and rst file |
| 88 | # additions. so we can use similar logic for that. |
| 89 | |
| 90 | ## TODO: for security we should look for CVE numbed regex match and if found flag it |
| 91 | def main(): |
| 92 | parser = argparse.ArgumentParser(prog="lts-triage.py", description="check patches for LTS candidacy") |
| 93 | parser.add_argument("--repo", required=True, help="path to tf-a git repo") |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 94 | parser.add_argument("--email_path", required=True, help="path including the filename for email file") |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 95 | parser.add_argument("--debug", help="print debug logs", action="store_true") |
| 96 | |
| 97 | args = parser.parse_args() |
| 98 | global global_debug |
| 99 | global_debug = args.debug |
| 100 | |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 101 | file_str = "Below is an interesting patchset. Patches are listed in format {Subject}: {Score}.\n\n" |
| 102 | at_least_one_match = False |
| 103 | |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 104 | repo = git.Repo(args.repo) |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 105 | |
Okash Khawaja | 66a3a7e | 2023-01-24 11:06:16 +0000 | [diff] [blame] | 106 | # collect the integration hashes in a list |
| 107 | rebase_hashes = [] |
| 108 | for cmt in repo.iter_commits(CHECK_AGAINST): |
| 109 | rebase_hashes.append(cmt.hexsha) |
| 110 | if len(rebase_hashes) == REBASE_DEPTH: |
| 111 | break |
| 112 | |
| 113 | cnt = MAX_PATCHSET_DEPTH |
| 114 | for cmt in repo.iter_commits(TO_CHECK): |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 115 | score = 0 |
| 116 | |
Okash Khawaja | 66a3a7e | 2023-01-24 11:06:16 +0000 | [diff] [blame] | 117 | # if we find a same commit hash among the ones we collected from integration branch |
| 118 | # then we have seen all the new patches in this patch set, so we should exit. |
| 119 | if cmt.hexsha in rebase_hashes: |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 120 | print("## stopping because found sha1 common between the two branches: ", cmt.hexsha) |
Okash Khawaja | 66a3a7e | 2023-01-24 11:06:16 +0000 | [diff] [blame] | 121 | break; |
| 122 | |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 123 | # don't process merge commits |
| 124 | if len(cmt.parents) > 1: |
| 125 | continue |
| 126 | |
| 127 | tok = re.compile(SUBJECT_TOKENS) |
| 128 | if tok.search(cmt.summary) is not None: |
| 129 | debug_print("## subject match") |
| 130 | score = score + 1 |
| 131 | |
| 132 | diff_text = repo.git.diff(cmt.hexsha + "~1", cmt.hexsha, ignore_blank_lines=True, ignore_space_at_eol=True) |
| 133 | ps = PatchSet(StringIO(diff_text)) |
| 134 | debug_print("# score before process_ps:", score) |
| 135 | score = score + process_ps(ps) |
| 136 | debug_print("# score after process_ps:", score) |
| 137 | |
Okash Khawaja | a5909ac | 2023-02-03 19:22:34 +0000 | [diff] [blame] | 138 | ln = f"{cmt.summary}: {score}" |
Okash Khawaja | a5909ac | 2023-02-03 19:22:34 +0000 | [diff] [blame] | 139 | print(ln) |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 140 | |
| 141 | if score > 0: |
Okash Khawaja | 0085948 | 2023-02-20 17:42:02 +0000 | [diff] [blame^] | 142 | file_str += ln + "\n" |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 143 | at_least_one_match = True |
| 144 | |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 145 | cnt = cnt - 1 |
| 146 | if cnt == 0: |
| 147 | break |
| 148 | |
Okash Khawaja | 6875150 | 2023-02-03 18:53:14 +0000 | [diff] [blame] | 149 | if at_least_one_match == True: |
| 150 | try: |
| 151 | with open(args.email_path, "x") as f: |
| 152 | f.write(file_str) |
| 153 | except: |
| 154 | print("\n\nERROR: Couldn't open email file due to error: ", sys.exc_info()[0]) |
| 155 | |
Okash Khawaja | 671bed2 | 2022-11-10 15:51:19 +0000 | [diff] [blame] | 156 | if __name__ == '__main__': |
| 157 | main() |