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