patchreview.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #! /usr/bin/env python3
  2. #
  3. # Copyright OpenEmbedded Contributors
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. # TODO
  8. # - option to just list all broken files
  9. # - test suite
  10. # - validate signed-off-by
  11. status_values = ("accepted", "pending", "inappropriate", "backport", "submitted", "denied", "inactive-upstream")
  12. class Result:
  13. # Whether the patch has an Upstream-Status or not
  14. missing_upstream_status = False
  15. # If the Upstream-Status tag is malformed in some way (string for bad bit)
  16. malformed_upstream_status = None
  17. # If the Upstream-Status value is unknown (boolean)
  18. unknown_upstream_status = False
  19. # The upstream status value (Pending, etc)
  20. upstream_status = None
  21. # Whether the patch has a Signed-off-by or not
  22. missing_sob = False
  23. # Whether the Signed-off-by tag is malformed in some way
  24. malformed_sob = False
  25. # The Signed-off-by tag value
  26. sob = None
  27. # Whether a patch looks like a CVE but doesn't have a CVE tag
  28. missing_cve = False
  29. def blame_patch(patch):
  30. """
  31. From a patch filename, return a list of "commit summary (author name <author
  32. email>)" strings representing the history.
  33. """
  34. import subprocess
  35. return subprocess.check_output(("git", "log",
  36. "--follow", "--find-renames", "--diff-filter=A",
  37. "--format=%s (%aN <%aE>)",
  38. "--", patch)).decode("utf-8").splitlines()
  39. def patchreview(path, patches):
  40. import re, os.path
  41. # General pattern: start of line, optional whitespace, tag with optional
  42. # hyphen or spaces, maybe a colon, some whitespace, then the value, all case
  43. # insensitive.
  44. sob_re = re.compile(r"^[\t ]*(Signed[-_ ]off[-_ ]by:?)[\t ]*(.+)", re.IGNORECASE | re.MULTILINE)
  45. status_re = re.compile(r"^[\t ]*(Upstream[-_ ]Status:?)[\t ]*([\w-]*)", re.IGNORECASE | re.MULTILINE)
  46. cve_tag_re = re.compile(r"^[\t ]*(CVE:)[\t ]*(.*)", re.IGNORECASE | re.MULTILINE)
  47. cve_re = re.compile(r"cve-[0-9]{4}-[0-9]{4,6}", re.IGNORECASE)
  48. results = {}
  49. for patch in patches:
  50. fullpath = os.path.join(path, patch)
  51. result = Result()
  52. results[fullpath] = result
  53. content = open(fullpath, encoding='ascii', errors='ignore').read()
  54. # Find the Signed-off-by tag
  55. match = sob_re.search(content)
  56. if match:
  57. value = match.group(1)
  58. if value != "Signed-off-by:":
  59. result.malformed_sob = value
  60. result.sob = match.group(2)
  61. else:
  62. result.missing_sob = True
  63. # Find the Upstream-Status tag
  64. match = status_re.search(content)
  65. if match:
  66. value = match.group(1)
  67. if value != "Upstream-Status:":
  68. result.malformed_upstream_status = value
  69. value = match.group(2).lower()
  70. # TODO: check case
  71. if value not in status_values:
  72. result.unknown_upstream_status = True
  73. result.upstream_status = value
  74. else:
  75. result.missing_upstream_status = True
  76. # Check that patches which looks like CVEs have CVE tags
  77. if cve_re.search(patch) or cve_re.search(content):
  78. if not cve_tag_re.search(content):
  79. result.missing_cve = True
  80. # TODO: extract CVE list
  81. return results
  82. def analyse(results, want_blame=False, verbose=True):
  83. """
  84. want_blame: display blame data for each malformed patch
  85. verbose: display per-file results instead of just summary
  86. """
  87. # want_blame requires verbose, so disable blame if we're not verbose
  88. if want_blame and not verbose:
  89. want_blame = False
  90. total_patches = 0
  91. missing_sob = 0
  92. malformed_sob = 0
  93. missing_status = 0
  94. malformed_status = 0
  95. missing_cve = 0
  96. pending_patches = 0
  97. for patch in sorted(results):
  98. r = results[patch]
  99. total_patches += 1
  100. need_blame = False
  101. # Build statistics
  102. if r.missing_sob:
  103. missing_sob += 1
  104. if r.malformed_sob:
  105. malformed_sob += 1
  106. if r.missing_upstream_status:
  107. missing_status += 1
  108. if r.malformed_upstream_status or r.unknown_upstream_status:
  109. malformed_status += 1
  110. # Count patches with no status as pending
  111. pending_patches +=1
  112. if r.missing_cve:
  113. missing_cve += 1
  114. if r.upstream_status == "pending":
  115. pending_patches += 1
  116. # Output warnings
  117. if r.missing_sob:
  118. need_blame = True
  119. if verbose:
  120. print("Missing Signed-off-by tag (%s)" % patch)
  121. if r.malformed_sob:
  122. need_blame = True
  123. if verbose:
  124. print("Malformed Signed-off-by '%s' (%s)" % (r.malformed_sob, patch))
  125. if r.missing_cve:
  126. need_blame = True
  127. if verbose:
  128. print("Missing CVE tag (%s)" % patch)
  129. if r.missing_upstream_status:
  130. need_blame = True
  131. if verbose:
  132. print("Missing Upstream-Status tag (%s)" % patch)
  133. if r.malformed_upstream_status:
  134. need_blame = True
  135. if verbose:
  136. print("Malformed Upstream-Status '%s' (%s)" % (r.malformed_upstream_status, patch))
  137. if r.unknown_upstream_status:
  138. need_blame = True
  139. if verbose:
  140. print("Unknown Upstream-Status value '%s' (%s)" % (r.upstream_status, patch))
  141. if want_blame and need_blame:
  142. print("\n".join(blame_patch(patch)) + "\n")
  143. def percent(num):
  144. try:
  145. return "%d (%d%%)" % (num, round(num * 100.0 / total_patches))
  146. except ZeroDivisionError:
  147. return "N/A"
  148. if verbose:
  149. print()
  150. print("""Total patches found: %d
  151. Patches missing Signed-off-by: %s
  152. Patches with malformed Signed-off-by: %s
  153. Patches missing CVE: %s
  154. Patches missing Upstream-Status: %s
  155. Patches with malformed Upstream-Status: %s
  156. Patches in Pending state: %s""" % (total_patches,
  157. percent(missing_sob),
  158. percent(malformed_sob),
  159. percent(missing_cve),
  160. percent(missing_status),
  161. percent(malformed_status),
  162. percent(pending_patches)))
  163. def histogram(results):
  164. from toolz import recipes, dicttoolz
  165. import math
  166. counts = recipes.countby(lambda r: r.upstream_status, results.values())
  167. bars = dicttoolz.valmap(lambda v: "#" * int(math.ceil(float(v) / len(results) * 100)), counts)
  168. for k in bars:
  169. print("%-20s %s (%d)" % (k.capitalize() if k else "No status", bars[k], counts[k]))
  170. if __name__ == "__main__":
  171. import argparse, subprocess, os
  172. args = argparse.ArgumentParser(description="Patch Review Tool")
  173. args.add_argument("-b", "--blame", action="store_true", help="show blame for malformed patches")
  174. args.add_argument("-v", "--verbose", action="store_true", help="show per-patch results")
  175. args.add_argument("-g", "--histogram", action="store_true", help="show patch histogram")
  176. args.add_argument("-j", "--json", help="update JSON")
  177. args.add_argument("directory", help="directory to scan")
  178. args = args.parse_args()
  179. patches = subprocess.check_output(("git", "-C", args.directory, "ls-files", "recipes-*/**/*.patch", "recipes-*/**/*.diff")).decode("utf-8").split()
  180. results = patchreview(args.directory, patches)
  181. analyse(results, want_blame=args.blame, verbose=args.verbose)
  182. if args.json:
  183. import json, os.path, collections
  184. if os.path.isfile(args.json):
  185. data = json.load(open(args.json))
  186. else:
  187. data = []
  188. row = collections.Counter()
  189. row["total"] = len(results)
  190. row["date"] = subprocess.check_output(["git", "-C", args.directory, "show", "-s", "--pretty=format:%cd", "--date=format:%s"]).decode("utf-8").strip()
  191. row["commit"] = subprocess.check_output(["git", "-C", args.directory, "show", "-s", "--pretty=format:%H"]).decode("utf-8").strip()
  192. for r in results.values():
  193. if r.upstream_status in status_values:
  194. row[r.upstream_status] += 1
  195. if r.malformed_upstream_status or r.missing_upstream_status:
  196. row['malformed-upstream-status'] += 1
  197. if r.malformed_sob or r.missing_sob:
  198. row['malformed-sob'] += 1
  199. data.append(row)
  200. json.dump(data, open(args.json, "w"), sort_keys=True, indent="\t")
  201. if args.histogram:
  202. print()
  203. histogram(results)