diff options
Diffstat (limited to 'scripts/contrib/patchreview.py')
-rwxr-xr-x | scripts/contrib/patchreview.py | 82 |
1 files changed, 62 insertions, 20 deletions
diff --git a/scripts/contrib/patchreview.py b/scripts/contrib/patchreview.py index 62c509f51c..d8d7b214e5 100755 --- a/scripts/contrib/patchreview.py +++ b/scripts/contrib/patchreview.py | |||
@@ -1,14 +1,29 @@ | |||
1 | #! /usr/bin/env python3 | 1 | #! /usr/bin/env python3 |
2 | # | 2 | # |
3 | # Copyright OpenEmbedded Contributors | ||
4 | # | ||
3 | # SPDX-License-Identifier: GPL-2.0-only | 5 | # SPDX-License-Identifier: GPL-2.0-only |
4 | # | 6 | # |
5 | 7 | ||
8 | import argparse | ||
9 | import collections | ||
10 | import json | ||
11 | import os | ||
12 | import os.path | ||
13 | import pathlib | ||
14 | import re | ||
15 | import subprocess | ||
16 | |||
17 | import sys | ||
18 | sys.path.append(os.path.join(sys.path[0], '../../meta/lib')) | ||
19 | import oe.qa | ||
20 | |||
6 | # TODO | 21 | # TODO |
7 | # - option to just list all broken files | 22 | # - option to just list all broken files |
8 | # - test suite | 23 | # - test suite |
9 | # - validate signed-off-by | 24 | # - validate signed-off-by |
10 | 25 | ||
11 | status_values = ("accepted", "pending", "inappropriate", "backport", "submitted", "denied") | 26 | status_values = ("accepted", "pending", "inappropriate", "backport", "submitted", "denied", "inactive-upstream") |
12 | 27 | ||
13 | class Result: | 28 | class Result: |
14 | # Whether the patch has an Upstream-Status or not | 29 | # Whether the patch has an Upstream-Status or not |
@@ -33,20 +48,18 @@ def blame_patch(patch): | |||
33 | From a patch filename, return a list of "commit summary (author name <author | 48 | From a patch filename, return a list of "commit summary (author name <author |
34 | email>)" strings representing the history. | 49 | email>)" strings representing the history. |
35 | """ | 50 | """ |
36 | import subprocess | ||
37 | return subprocess.check_output(("git", "log", | 51 | return subprocess.check_output(("git", "log", |
38 | "--follow", "--find-renames", "--diff-filter=A", | 52 | "--follow", "--find-renames", "--diff-filter=A", |
39 | "--format=%s (%aN <%aE>)", | 53 | "--format=%s (%aN <%aE>)", |
40 | "--", patch)).decode("utf-8").splitlines() | 54 | "--", patch), cwd=os.path.dirname(patch)).decode("utf-8").splitlines() |
41 | 55 | ||
42 | def patchreview(path, patches): | 56 | def patchreview(patches): |
43 | import re, os.path | ||
44 | 57 | ||
45 | # General pattern: start of line, optional whitespace, tag with optional | 58 | # General pattern: start of line, optional whitespace, tag with optional |
46 | # hyphen or spaces, maybe a colon, some whitespace, then the value, all case | 59 | # hyphen or spaces, maybe a colon, some whitespace, then the value, all case |
47 | # insensitive. | 60 | # insensitive. |
48 | sob_re = re.compile(r"^[\t ]*(Signed[-_ ]off[-_ ]by:?)[\t ]*(.+)", re.IGNORECASE | re.MULTILINE) | 61 | sob_re = re.compile(r"^[\t ]*(Signed[-_ ]off[-_ ]by:?)[\t ]*(.+)", re.IGNORECASE | re.MULTILINE) |
49 | status_re = re.compile(r"^[\t ]*(Upstream[-_ ]Status:?)[\t ]*(\w*)", re.IGNORECASE | re.MULTILINE) | 62 | status_re = re.compile(r"^[\t ]*(Upstream[-_ ]Status:?)[\t ]*([\w-]*)", re.IGNORECASE | re.MULTILINE) |
50 | cve_tag_re = re.compile(r"^[\t ]*(CVE:)[\t ]*(.*)", re.IGNORECASE | re.MULTILINE) | 63 | cve_tag_re = re.compile(r"^[\t ]*(CVE:)[\t ]*(.*)", re.IGNORECASE | re.MULTILINE) |
51 | cve_re = re.compile(r"cve-[0-9]{4}-[0-9]{4,6}", re.IGNORECASE) | 64 | cve_re = re.compile(r"cve-[0-9]{4}-[0-9]{4,6}", re.IGNORECASE) |
52 | 65 | ||
@@ -54,11 +67,10 @@ def patchreview(path, patches): | |||
54 | 67 | ||
55 | for patch in patches: | 68 | for patch in patches: |
56 | 69 | ||
57 | fullpath = os.path.join(path, patch) | ||
58 | result = Result() | 70 | result = Result() |
59 | results[fullpath] = result | 71 | results[patch] = result |
60 | 72 | ||
61 | content = open(fullpath, encoding='ascii', errors='ignore').read() | 73 | content = open(patch, encoding='ascii', errors='ignore').read() |
62 | 74 | ||
63 | # Find the Signed-off-by tag | 75 | # Find the Signed-off-by tag |
64 | match = sob_re.search(content) | 76 | match = sob_re.search(content) |
@@ -70,12 +82,11 @@ def patchreview(path, patches): | |||
70 | else: | 82 | else: |
71 | result.missing_sob = True | 83 | result.missing_sob = True |
72 | 84 | ||
73 | |||
74 | # Find the Upstream-Status tag | 85 | # Find the Upstream-Status tag |
75 | match = status_re.search(content) | 86 | match = status_re.search(content) |
76 | if match: | 87 | if match: |
77 | value = match.group(1) | 88 | value = oe.qa.check_upstream_status(patch) |
78 | if value != "Upstream-Status:": | 89 | if value: |
79 | result.malformed_upstream_status = value | 90 | result.malformed_upstream_status = value |
80 | 91 | ||
81 | value = match.group(2).lower() | 92 | value = match.group(2).lower() |
@@ -191,29 +202,56 @@ Patches in Pending state: %s""" % (total_patches, | |||
191 | def histogram(results): | 202 | def histogram(results): |
192 | from toolz import recipes, dicttoolz | 203 | from toolz import recipes, dicttoolz |
193 | import math | 204 | import math |
205 | |||
194 | counts = recipes.countby(lambda r: r.upstream_status, results.values()) | 206 | counts = recipes.countby(lambda r: r.upstream_status, results.values()) |
195 | bars = dicttoolz.valmap(lambda v: "#" * int(math.ceil(float(v) / len(results) * 100)), counts) | 207 | bars = dicttoolz.valmap(lambda v: "#" * int(math.ceil(float(v) / len(results) * 100)), counts) |
196 | for k in bars: | 208 | for k in bars: |
197 | print("%-20s %s (%d)" % (k.capitalize() if k else "No status", bars[k], counts[k])) | 209 | print("%-20s %s (%d)" % (k.capitalize() if k else "No status", bars[k], counts[k])) |
198 | 210 | ||
211 | def find_layers(candidate): | ||
212 | # candidate can either be the path to a layer directly (eg meta-intel), or a | ||
213 | # repository that contains other layers (meta-arm). We can determine what by | ||
214 | # looking for a conf/layer.conf file. If that file exists then it's a layer, | ||
215 | # otherwise its a repository of layers and we can assume they're called | ||
216 | # meta-*. | ||
217 | |||
218 | if (candidate / "conf" / "layer.conf").exists(): | ||
219 | return [candidate.absolute()] | ||
220 | else: | ||
221 | return [d.absolute() for d in candidate.iterdir() if d.is_dir() and (d.name == "meta" or d.name.startswith("meta-"))] | ||
222 | |||
223 | # TODO these don't actually handle dynamic-layers/ | ||
224 | |||
225 | def gather_patches(layers): | ||
226 | patches = [] | ||
227 | for directory in layers: | ||
228 | filenames = subprocess.check_output(("git", "-C", directory, "ls-files", "recipes-*/**/*.patch", "recipes-*/**/*.diff"), universal_newlines=True).split() | ||
229 | patches += [os.path.join(directory, f) for f in filenames] | ||
230 | return patches | ||
231 | |||
232 | def count_recipes(layers): | ||
233 | count = 0 | ||
234 | for directory in layers: | ||
235 | output = subprocess.check_output(["git", "-C", directory, "ls-files", "recipes-*/**/*.bb"], universal_newlines=True) | ||
236 | count += len(output.splitlines()) | ||
237 | return count | ||
199 | 238 | ||
200 | if __name__ == "__main__": | 239 | if __name__ == "__main__": |
201 | import argparse, subprocess, os | ||
202 | |||
203 | args = argparse.ArgumentParser(description="Patch Review Tool") | 240 | args = argparse.ArgumentParser(description="Patch Review Tool") |
204 | args.add_argument("-b", "--blame", action="store_true", help="show blame for malformed patches") | 241 | args.add_argument("-b", "--blame", action="store_true", help="show blame for malformed patches") |
205 | args.add_argument("-v", "--verbose", action="store_true", help="show per-patch results") | 242 | args.add_argument("-v", "--verbose", action="store_true", help="show per-patch results") |
206 | args.add_argument("-g", "--histogram", action="store_true", help="show patch histogram") | 243 | args.add_argument("-g", "--histogram", action="store_true", help="show patch histogram") |
207 | args.add_argument("-j", "--json", help="update JSON") | 244 | args.add_argument("-j", "--json", help="update JSON") |
208 | args.add_argument("directory", help="directory to scan") | 245 | args.add_argument("directory", type=pathlib.Path, metavar="DIRECTORY", help="directory to scan (layer, or repository of layers)") |
209 | args = args.parse_args() | 246 | args = args.parse_args() |
210 | 247 | ||
211 | patches = subprocess.check_output(("git", "-C", args.directory, "ls-files", "recipes-*/**/*.patch", "recipes-*/**/*.diff")).decode("utf-8").split() | 248 | layers = find_layers(args.directory) |
212 | results = patchreview(args.directory, patches) | 249 | print(f"Found layers {' '.join((d.name for d in layers))}") |
250 | patches = gather_patches(layers) | ||
251 | results = patchreview(patches) | ||
213 | analyse(results, want_blame=args.blame, verbose=args.verbose) | 252 | analyse(results, want_blame=args.blame, verbose=args.verbose) |
214 | 253 | ||
215 | if args.json: | 254 | if args.json: |
216 | import json, os.path, collections | ||
217 | if os.path.isfile(args.json): | 255 | if os.path.isfile(args.json): |
218 | data = json.load(open(args.json)) | 256 | data = json.load(open(args.json)) |
219 | else: | 257 | else: |
@@ -221,7 +259,11 @@ if __name__ == "__main__": | |||
221 | 259 | ||
222 | row = collections.Counter() | 260 | row = collections.Counter() |
223 | row["total"] = len(results) | 261 | row["total"] = len(results) |
224 | row["date"] = subprocess.check_output(["git", "-C", args.directory, "show", "-s", "--pretty=format:%cd", "--date=format:%s"]).decode("utf-8").strip() | 262 | row["date"] = subprocess.check_output(["git", "-C", args.directory, "show", "-s", "--pretty=format:%cd", "--date=format:%s"], universal_newlines=True).strip() |
263 | row["commit"] = subprocess.check_output(["git", "-C", args.directory, "rev-parse", "HEAD"], universal_newlines=True).strip() | ||
264 | row['commit_count'] = subprocess.check_output(["git", "-C", args.directory, "rev-list", "--count", "HEAD"], universal_newlines=True).strip() | ||
265 | row['recipe_count'] = count_recipes(layers) | ||
266 | |||
225 | for r in results.values(): | 267 | for r in results.values(): |
226 | if r.upstream_status in status_values: | 268 | if r.upstream_status in status_values: |
227 | row[r.upstream_status] += 1 | 269 | row[r.upstream_status] += 1 |
@@ -231,7 +273,7 @@ if __name__ == "__main__": | |||
231 | row['malformed-sob'] += 1 | 273 | row['malformed-sob'] += 1 |
232 | 274 | ||
233 | data.append(row) | 275 | data.append(row) |
234 | json.dump(data, open(args.json, "w")) | 276 | json.dump(data, open(args.json, "w"), sort_keys=True, indent="\t") |
235 | 277 | ||
236 | if args.histogram: | 278 | if args.histogram: |
237 | print() | 279 | print() |