patchreview: Various fixes/improvements

Add various fixes and improvements including the ability to export
patch statsitics as json data.

(From OE-Core rev: aa4a4b3ca799948047337e006ee9bf482be7b409)

(From OE-Core rev: 1d0eb08f016db5d5ab08b37dea654950731fcab3)

Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Signed-off-by: Armin Kuster <akuster808@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Ross Burton 2018-12-03 10:26:16 +00:00 committed by Richard Purdie
parent 2b91be4910
commit 436b5ceca4

View File

@ -5,6 +5,7 @@
# - test suite # - test suite
# - validate signed-off-by # - validate signed-off-by
status_values = ("accepted", "pending", "inappropriate", "backport", "submitted", "denied")
class Result: class Result:
# Whether the patch has an Upstream-Status or not # Whether the patch has an Upstream-Status or not
@ -35,25 +36,26 @@ def blame_patch(patch):
"--format=%s (%aN <%aE>)", "--format=%s (%aN <%aE>)",
"--", patch)).decode("utf-8").splitlines() "--", patch)).decode("utf-8").splitlines()
def patchreview(patches): def patchreview(path, patches):
import re import re, os.path
# General pattern: start of line, optional whitespace, tag with optional # General pattern: start of line, optional whitespace, tag with optional
# hyphen or spaces, maybe a colon, some whitespace, then the value, all case # hyphen or spaces, maybe a colon, some whitespace, then the value, all case
# insensitive. # insensitive.
sob_re = re.compile(r"^[\t ]*(Signed[-_ ]off[-_ ]by:?)[\t ]*(.+)", re.IGNORECASE | re.MULTILINE) sob_re = re.compile(r"^[\t ]*(Signed[-_ ]off[-_ ]by:?)[\t ]*(.+)", re.IGNORECASE | re.MULTILINE)
status_re = re.compile(r"^[\t ]*(Upstream[-_ ]Status:?)[\t ]*(\w*)", re.IGNORECASE | re.MULTILINE) status_re = re.compile(r"^[\t ]*(Upstream[-_ ]Status:?)[\t ]*(\w*)", re.IGNORECASE | re.MULTILINE)
status_values = ("accepted", "pending", "inappropriate", "backport", "submitted", "denied")
cve_tag_re = re.compile(r"^[\t ]*(CVE:)[\t ]*(.*)", re.IGNORECASE | re.MULTILINE) cve_tag_re = re.compile(r"^[\t ]*(CVE:)[\t ]*(.*)", re.IGNORECASE | re.MULTILINE)
cve_re = re.compile(r"cve-[0-9]{4}-[0-9]{4,6}", re.IGNORECASE) cve_re = re.compile(r"cve-[0-9]{4}-[0-9]{4,6}", re.IGNORECASE)
results = {} results = {}
for patch in patches: for patch in patches:
result = Result()
results[patch] = result
content = open(patch, encoding='ascii', errors='ignore').read() fullpath = os.path.join(path, patch)
result = Result()
results[fullpath] = result
content = open(fullpath, encoding='ascii', errors='ignore').read()
# Find the Signed-off-by tag # Find the Signed-off-by tag
match = sob_re.search(content) match = sob_re.search(content)
@ -122,6 +124,8 @@ def analyse(results, want_blame=False, verbose=True):
missing_status += 1 missing_status += 1
if r.malformed_upstream_status or r.unknown_upstream_status: if r.malformed_upstream_status or r.unknown_upstream_status:
malformed_status += 1 malformed_status += 1
# Count patches with no status as pending
pending_patches +=1
if r.missing_cve: if r.missing_cve:
missing_cve += 1 missing_cve += 1
if r.upstream_status == "pending": if r.upstream_status == "pending":
@ -132,7 +136,6 @@ def analyse(results, want_blame=False, verbose=True):
need_blame = True need_blame = True
if verbose: if verbose:
print("Missing Signed-off-by tag (%s)" % patch) print("Missing Signed-off-by tag (%s)" % patch)
if r.malformed_sob: if r.malformed_sob:
need_blame = True need_blame = True
if verbose: if verbose:
@ -198,14 +201,35 @@ if __name__ == "__main__":
args.add_argument("-b", "--blame", action="store_true", help="show blame for malformed patches") args.add_argument("-b", "--blame", action="store_true", help="show blame for malformed patches")
args.add_argument("-v", "--verbose", action="store_true", help="show per-patch results") args.add_argument("-v", "--verbose", action="store_true", help="show per-patch results")
args.add_argument("-g", "--histogram", action="store_true", help="show patch histogram") args.add_argument("-g", "--histogram", action="store_true", help="show patch histogram")
args.add_argument("directory", nargs="?", help="directory to scan") args.add_argument("-j", "--json", help="update JSON")
args.add_argument("directory", help="directory to scan")
args = args.parse_args() args = args.parse_args()
if args.directory: patches = subprocess.check_output(("git", "-C", args.directory, "ls-files", "recipes-*/**/*.patch", "recipes-*/**/*.diff")).decode("utf-8").split()
os.chdir(args.directory) results = patchreview(args.directory, patches)
patches = subprocess.check_output(("git", "ls-files", "recipes-*/**/*.patch", "recipes-*/**/*.diff")).decode("utf-8").split()
results = patchreview(patches)
analyse(results, want_blame=args.blame, verbose=args.verbose) analyse(results, want_blame=args.blame, verbose=args.verbose)
if args.json:
import json, os.path, collections
if os.path.isfile(args.json):
data = json.load(open(args.json))
else:
data = []
row = collections.Counter()
row["total"] = len(results)
row["date"] = subprocess.check_output(["git", "-C", args.directory, "show", "-s", "--pretty=format:%cd", "--date=format:%s"]).decode("utf-8").strip()
for r in results.values():
if r.upstream_status in status_values:
row[r.upstream_status] += 1
if r.malformed_upstream_status or r.missing_upstream_status:
row['malformed-upstream-status'] += 1
if r.malformed_sob or r.missing_sob:
row['malformed-sob'] += 1
data.append(row)
json.dump(data, open(args.json, "w"))
if args.histogram: if args.histogram:
print() print()
histogram(results) histogram(results)