Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright (c) 2021 Intel Corporation |
| 4 | |
| 5 | import os |
| 6 | import sh |
| 7 | import argparse |
| 8 | import re |
| 9 | from unidiff import PatchSet |
| 10 | |
| 11 | if "ZEPHYR_BASE" not in os.environ: |
| 12 | exit("$ZEPHYR_BASE environment variable undefined.") |
| 13 | |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 14 | coccinelle_scripts = ["/scripts/coccinelle/reserved_names.cocci", |
| 15 | "/scripts/coccinelle/same_identifier.cocci", |
Anas Nashif | 01c1bcf | 2021-05-06 14:17:29 -0400 | [diff] [blame] | 16 | #"/scripts/coccinelle/identifier_length.cocci", |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 17 | ] |
| 18 | |
| 19 | |
| 20 | def parse_coccinelle(contents: str, violations: dict): |
Anas Nashif | b669f0c | 2021-04-30 23:01:03 -0400 | [diff] [blame] | 21 | reg = re.compile("([a-zA-Z0-9_/]*\\.[ch]:[0-9]*)(:[0-9\\-]*: )(.*)") |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 22 | for line in contents.split("\n"): |
| 23 | r = reg.match(line) |
| 24 | if r: |
| 25 | f = r.group(1) |
| 26 | if f in violations: |
| 27 | violations[f].append(r.group(3)) |
| 28 | else: |
| 29 | violations[r.group(1)] = [r.group(3)] |
| 30 | |
| 31 | |
| 32 | def parse_args(): |
| 33 | parser = argparse.ArgumentParser( |
Jordan Yates | ac78b97 | 2023-12-10 16:04:57 +1000 | [diff] [blame] | 34 | description="Check commits against Cocccinelle rules", allow_abbrev=False) |
| 35 | parser.add_argument('-r', "--repository", required=False, |
| 36 | help="Path to repository") |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 37 | parser.add_argument('-c', '--commits', default=None, |
| 38 | help="Commit range in the form: a..b") |
Flavio Ceolin | 58fdc01 | 2021-03-24 15:54:16 -0700 | [diff] [blame] | 39 | parser.add_argument("-o", "--output", required=False, |
| 40 | help="Print violation into a file") |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 41 | return parser.parse_args() |
| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | args = parse_args() |
| 46 | if not args.commits: |
| 47 | exit("missing commit range") |
| 48 | |
Jordan Yates | ac78b97 | 2023-12-10 16:04:57 +1000 | [diff] [blame] | 49 | if args.repository is None: |
| 50 | repository_path = os.environ['ZEPHYR_BASE'] |
| 51 | else: |
| 52 | repository_path = args.repository |
| 53 | |
| 54 | sh_special_args = { |
| 55 | '_tty_out': False, |
| 56 | '_cwd': repository_path |
| 57 | } |
| 58 | |
Flavio Ceolin | 0bbe5e4 | 2021-03-24 16:18:43 -0700 | [diff] [blame] | 59 | # pylint does not like the 'sh' library |
| 60 | # pylint: disable=too-many-function-args,unexpected-keyword-arg |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 61 | commit = sh.git("diff", args.commits, **sh_special_args) |
| 62 | patch_set = PatchSet(commit) |
| 63 | zephyr_base = os.getenv("ZEPHYR_BASE") |
| 64 | violations = {} |
| 65 | numViolations = 0 |
| 66 | |
| 67 | for f in patch_set: |
| 68 | if not f.path.endswith(".c") and not f.path.endswith(".h") or not os.path.exists(zephyr_base + "/" + f.path): |
| 69 | continue |
| 70 | |
| 71 | for script in coccinelle_scripts: |
| 72 | script_path = os.getenv("ZEPHYR_BASE") + "/" + script |
Anas Nashif | 8ebc67e | 2021-05-03 10:40:42 -0400 | [diff] [blame] | 73 | print(f"Running {script} on {f.path}") |
| 74 | try: |
| 75 | cocci = sh.coccicheck( |
| 76 | "--mode=report", |
| 77 | "--cocci=" + |
| 78 | script_path, |
| 79 | f.path, |
| 80 | _timeout=10, |
| 81 | **sh_special_args) |
| 82 | parse_coccinelle(cocci, violations) |
| 83 | except sh.TimeoutException: |
| 84 | print("we timed out waiting, skipping...") |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 85 | |
| 86 | for hunk in f: |
| 87 | for line in hunk: |
| 88 | if line.is_added: |
| 89 | violation = "{}:{}".format(f.path, line.target_line_no) |
| 90 | if violation in violations: |
| 91 | numViolations += 1 |
Flavio Ceolin | 58fdc01 | 2021-03-24 15:54:16 -0700 | [diff] [blame] | 92 | if args.output: |
| 93 | with open(args.output, "a+") as fp: |
| 94 | fp.write("{}:{}\n".format( |
| 95 | violation, "\t\n".join( |
| 96 | violations[violation]))) |
| 97 | else: |
| 98 | print( |
| 99 | "{}:{}".format( |
| 100 | violation, "\t\n".join( |
| 101 | violations[violation]))) |
Flavio Ceolin | 8552542 | 2021-03-23 15:06:01 -0700 | [diff] [blame] | 102 | |
| 103 | return numViolations |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | ret = main() |
| 108 | exit(ret) |