| 1 |
#!/usr/bin/env python3 |
| 2 |
import subprocess |
| 3 |
import urllib.request |
| 4 |
import os |
| 5 |
import re |
| 6 |
import tempfile |
| 7 |
import collections |
| 8 |
import copy |
| 9 |
import sys |
| 10 |
|
| 11 |
base = os.environ["SYSTEM_PULLREQUEST_TARGETBRANCH"] |
| 12 |
if not re.match("^[0-9a-fA-F]{40}$", base): |
| 13 |
base = "refs/remotes/origin/" + base |
| 14 |
|
| 15 |
with tempfile.TemporaryDirectory() as dfn: |
| 16 |
patches = subprocess.check_output( |
| 17 |
[ |
| 18 |
"git", "format-patch", |
| 19 |
"--output-directory", dfn, |
| 20 |
os.environ["SYSTEM_PULLREQUEST_SOURCECOMMITID"], "^" + base |
| 21 |
], |
| 22 |
universal_newlines=True).splitlines() |
| 23 |
if len(patches) == 0: |
| 24 |
sys.exit(0) |
| 25 |
|
| 26 |
ckp = os.path.join(dfn, "checkpatch.pl") |
| 27 |
opener = urllib.request.build_opener() |
| 28 |
opener.addheaders = [('User-agent', 'rdma-core/1.x ([email protected])')] |
| 29 |
urllib.request.install_opener(opener) |
| 30 |
urllib.request.urlretrieve( |
| 31 |
"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/scripts/checkpatch.pl", |
| 32 |
ckp) |
| 33 |
urllib.request.urlretrieve( |
| 34 |
"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/scripts/spelling.txt", |
| 35 |
os.path.join(dfn, "spelling.txt")) |
| 36 |
os.symlink( |
| 37 |
os.path.join(os.getcwd(), "buildlib/const_structs.checkpatch"), |
| 38 |
os.path.join(dfn, "const_structs.checkpatch")) |
| 39 |
checkpatch = [ |
| 40 |
"perl", ckp, "--no-tree", "--show-types", "--ignore", |
| 41 |
"PREFER_KERNEL_TYPES,FILE_PATH_CHANGES,EXECUTE_PERMISSIONS,USE_NEGATIVE_ERRNO,CONST_STRUCT", |
| 42 |
"--emacs", "--mailback", "--quiet", "--no-summary" |
| 43 |
] |
| 44 |
|
| 45 |
environ = copy.copy(os.environ) |
| 46 |
environ["GIT_DIR"] = subprocess.check_output(["git","rev-parse","--absolute-git-dir"]).decode().strip() |
| 47 |
|
| 48 |
warnings = False |
| 49 |
errors = False |
| 50 |
for fn in patches: |
| 51 |
proc = subprocess.run( |
| 52 |
checkpatch + [os.path.basename(fn)], |
| 53 |
cwd=dfn, |
| 54 |
stdout=subprocess.PIPE, |
| 55 |
universal_newlines=True, |
| 56 |
env=environ, |
| 57 |
stderr=subprocess.STDOUT) |
| 58 |
if proc.returncode == 0: |
| 59 |
assert (not proc.stdout) |
| 60 |
continue |
| 61 |
sys.stdout.write(proc.stdout) |
| 62 |
|
| 63 |
warnings = True |
| 64 |
for g in re.finditer( |
| 65 |
r"^\d+-.*:\d+: (\S+):(\S+): (.*)(?:\n#(\d+): (?:FILE: (.*):(\d+):)?)?$", |
| 66 |
proc.stdout, |
| 67 |
flags=re.MULTILINE): |
| 68 |
itms = {} |
| 69 |
if g.group(1) == "WARNING": |
| 70 |
itms["type"] = "warning" |
| 71 |
else: |
| 72 |
itms["type"] = "error" |
| 73 |
|
| 74 |
itms["code"]=g.group(2) |
| 75 |
|
| 76 |
if g.group(4): |
| 77 |
itms["sourcepath"] = g.group(5) |
| 78 |
itms["linenumber"] = g.group(6) |
| 79 |
|
| 80 |
# Bump some warnings to errors |
| 81 |
if itms["code"] == "UNKNOWN_COMMIT_ID": |
| 82 |
itms["type"] = "error" |
| 83 |
|
| 84 |
if itms["type"] == "error": |
| 85 |
errors = True |
| 86 |
print("##vso[task.logissue %s]%s" % (";".join( |
| 87 |
"%s=%s" % (k, v) |
| 88 |
for k, v in sorted(itms.items())), g.group(3))) |
| 89 |
|
| 90 |
if errors: |
| 91 |
print("##vso[task.complete result=Failed]azp-checkpatch") |
| 92 |
elif warnings: |
| 93 |
print("##vso[task.complete result=SucceededWithIssues]azp-checkpatch") |