Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 3 | # Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | """ |
| 9 | Check if a given file includes the copyright boiler plate. |
| 10 | This checker supports the following comment styles: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 11 | /* |
| 12 | * |
| 13 | // |
| 14 | # |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 15 | """ |
| 16 | |
| 17 | import argparse |
| 18 | import datetime |
| 19 | import collections |
| 20 | import fnmatch |
| 21 | import shlex |
| 22 | import os |
| 23 | import re |
| 24 | import sys |
| 25 | import utils |
| 26 | from itertools import islice |
| 27 | |
| 28 | # File extensions to check |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 29 | VALID_FILE_EXTENSIONS = ('.c', '.conf', '.dts', '.dtsi', '.editorconfig', |
| 30 | '.h', '.i', '.ld', 'Makefile', '.mk', '.msvc', |
| 31 | '.py', '.S', '.scat', '.sh') |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 32 | |
| 33 | # Paths inside the tree to ignore. Hidden folders and files are always ignored. |
| 34 | # They mustn't end in '/'. |
| 35 | IGNORED_FOLDERS = ( |
| 36 | 'include/lib/libfdt', |
| 37 | 'lib/compiler-rt', |
| 38 | 'lib/libfdt', |
| 39 | 'lib/zlib' |
| 40 | ) |
| 41 | |
| 42 | # List of ignored files in folders that aren't ignored |
| 43 | IGNORED_FILES = ( |
| 44 | 'include/tools_share/uuid.h' |
| 45 | ) |
| 46 | |
| 47 | # Supported comment styles (Python regex) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 48 | COMMENT_PATTERN = '(\*|/\*|\#|//)' |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 49 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 50 | # Any combination of spaces and/or tabs |
| 51 | SPACING = '[ \t]*' |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 52 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 53 | # Line must start with a comment and optional spacing |
| 54 | LINE_START = '^' + SPACING + COMMENT_PATTERN + SPACING |
| 55 | |
| 56 | # Line end with optional spacing |
| 57 | EOL = SPACING + '$' |
| 58 | |
| 59 | # Year or period as YYYY or YYYY-YYYY |
| 60 | TIME_PERIOD = '[0-9]{4}(-[0-9]{4})?' |
| 61 | |
| 62 | # Any string with valid license ID, don't allow adding postfix |
Chris Kay | 345873c | 2021-04-27 13:24:51 +0100 | [diff] [blame] | 63 | LICENSE_ID = '.*(BSD-3-Clause|BSD-2-Clause-FreeBSD|MIT)([ ,.\);].*)?' |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 64 | |
| 65 | # File must contain both lines to pass the check |
| 66 | COPYRIGHT_LINE = LINE_START + 'Copyright' + '.*' + TIME_PERIOD + '.*' + EOL |
| 67 | LICENSE_ID_LINE = LINE_START + 'SPDX-License-Identifier:' + LICENSE_ID + EOL |
| 68 | |
| 69 | # Compiled license patterns |
| 70 | COPYRIGHT_PATTERN = re.compile(COPYRIGHT_LINE, re.MULTILINE) |
| 71 | LICENSE_ID_PATTERN = re.compile(LICENSE_ID_LINE, re.MULTILINE) |
| 72 | |
| 73 | CURRENT_YEAR = str(datetime.datetime.now().year) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 74 | |
| 75 | COPYRIGHT_OK = 0 |
| 76 | COPYRIGHT_ERROR = 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 77 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 78 | def check_copyright(path, args, encoding='utf-8'): |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 79 | '''Checks a file for a correct copyright header.''' |
| 80 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 81 | result = COPYRIGHT_OK |
| 82 | |
| 83 | with open(path, encoding=encoding) as file_: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 84 | file_content = file_.read() |
| 85 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 86 | copyright_line = COPYRIGHT_PATTERN.search(file_content) |
| 87 | if not copyright_line: |
| 88 | print("ERROR: Missing copyright in " + file_.name) |
| 89 | result = COPYRIGHT_ERROR |
| 90 | elif CURRENT_YEAR not in copyright_line.group(): |
| 91 | print("WARNING: Copyright is out of date in " + file_.name + ": '" + |
| 92 | copyright_line.group() + "'") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 93 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 94 | if not LICENSE_ID_PATTERN.search(file_content): |
| 95 | print("ERROR: License ID error in " + file_.name) |
| 96 | result = COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 97 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 98 | return result |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 99 | |
| 100 | def main(args): |
| 101 | print("Checking the copyrights in the code...") |
| 102 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 103 | if args.verbose: |
| 104 | print ("Copyright regexp: " + COPYRIGHT_LINE) |
| 105 | print ("License regexp: " + LICENSE_ID_LINE) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 106 | |
| 107 | if args.patch: |
Harrison Mutai | 7a93cd2 | 2022-09-29 12:31:31 +0100 | [diff] [blame^] | 108 | print("Checking files added between patches " + args.from_ref |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 109 | + " and " + args.to_ref + "...") |
| 110 | |
| 111 | (rc, stdout, stderr) = utils.shell_command(['git', 'diff', |
Harrison Mutai | 7a93cd2 | 2022-09-29 12:31:31 +0100 | [diff] [blame^] | 112 | '--diff-filter=ACRT', '--name-only', args.from_ref, args.to_ref ]) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 113 | if rc: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 114 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 115 | |
| 116 | files = stdout.splitlines() |
| 117 | |
| 118 | else: |
| 119 | print("Checking all files tracked by git...") |
| 120 | |
| 121 | (rc, stdout, stderr) = utils.shell_command([ 'git', 'ls-files' ]) |
| 122 | if rc: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 123 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 124 | |
| 125 | files = stdout.splitlines() |
| 126 | |
| 127 | count_ok = 0 |
| 128 | count_warning = 0 |
| 129 | count_error = 0 |
| 130 | |
| 131 | for f in files: |
| 132 | |
| 133 | if utils.file_is_ignored(f, VALID_FILE_EXTENSIONS, IGNORED_FILES, IGNORED_FOLDERS): |
| 134 | if args.verbose: |
| 135 | print("Ignoring file " + f) |
| 136 | continue |
| 137 | |
| 138 | if args.verbose: |
| 139 | print("Checking file " + f) |
| 140 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 141 | rc = check_copyright(f, args) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 142 | |
| 143 | if rc == COPYRIGHT_OK: |
| 144 | count_ok += 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 145 | elif rc == COPYRIGHT_ERROR: |
| 146 | count_error += 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 147 | |
| 148 | print("\nSummary:") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 149 | print("\t{} files analyzed".format(count_ok + count_error)) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 150 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 151 | if count_error == 0: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 152 | print("\tNo errors found") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 153 | return COPYRIGHT_OK |
| 154 | else: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 155 | print("\t{} errors found".format(count_error)) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 156 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 157 | |
| 158 | def parse_cmd_line(argv, prog_name): |
| 159 | parser = argparse.ArgumentParser( |
| 160 | prog=prog_name, |
| 161 | formatter_class=argparse.RawTextHelpFormatter, |
| 162 | description="Check copyright of all files of codebase", |
| 163 | epilog=""" |
| 164 | For each source file in the tree, checks that the copyright header |
| 165 | has the correct format. |
| 166 | """) |
| 167 | |
| 168 | parser.add_argument("--tree", "-t", |
| 169 | help="Path to the source tree to check (default: %(default)s)", |
| 170 | default=os.curdir) |
| 171 | |
| 172 | parser.add_argument("--verbose", "-v", |
| 173 | help="Increase verbosity to the source tree to check (default: %(default)s)", |
| 174 | action='store_true', default=False) |
| 175 | |
| 176 | parser.add_argument("--patch", "-p", |
| 177 | help=""" |
| 178 | Patch mode. |
| 179 | Instead of checking all files in the source tree, the script will consider |
| 180 | only files that are modified by the latest patch(es).""", |
| 181 | action="store_true") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 182 | |
Leonardo Sandoval | 9b3163e | 2020-10-13 12:55:24 -0500 | [diff] [blame] | 183 | (rc, stdout, stderr) = utils.shell_command(['git', 'merge-base', 'HEAD', 'refs/remotes/origin/master']) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 184 | if rc: |
| 185 | print("Git merge-base command failed. Cannot determine base commit.") |
| 186 | sys.exit(rc) |
| 187 | merge_bases = stdout.splitlines() |
| 188 | |
| 189 | # This should not happen, but it's better to be safe. |
| 190 | if len(merge_bases) > 1: |
| 191 | print("WARNING: Multiple merge bases found. Using the first one as base commit.") |
| 192 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 193 | parser.add_argument("--from-ref", |
| 194 | help="Base commit in patch mode (default: %(default)s)", |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 195 | default=merge_bases[0]) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 196 | parser.add_argument("--to-ref", |
| 197 | help="Final commit in patch mode (default: %(default)s)", |
| 198 | default="HEAD") |
| 199 | |
| 200 | args = parser.parse_args(argv) |
| 201 | return args |
| 202 | |
| 203 | |
| 204 | if __name__ == "__main__": |
| 205 | args = parse_cmd_line(sys.argv[1:], sys.argv[0]) |
| 206 | |
| 207 | os.chdir(args.tree) |
| 208 | |
| 209 | rc = main(args) |
| 210 | |
| 211 | sys.exit(rc) |