Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Zachary Leaf | b6d8630 | 2024-10-29 10:29:15 +0000 | [diff] [blame] | 3 | # Copyright (c) 2019-2024, 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', |
Zachary Leaf | b6d8630 | 2024-10-29 10:29:15 +0000 | [diff] [blame] | 31 | '.py', '.S', '.scat', '.sh', '.rs') |
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 | 345873c | 2021-04-27 13:24:51 +0100 | [diff] [blame] | 65 | LICENSE_ID = '.*(BSD-3-Clause|BSD-2-Clause-FreeBSD|MIT)([ ,.\);].*)?' |
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 |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 69 | RUST_COPYRIGHT_LINE = LINE_START + 'Copyright The Rusted Firmware-A Contributors.' + EOL |
| 70 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 71 | LICENSE_ID_LINE = LINE_START + 'SPDX-License-Identifier:' + LICENSE_ID + EOL |
| 72 | |
| 73 | # Compiled license patterns |
| 74 | COPYRIGHT_PATTERN = re.compile(COPYRIGHT_LINE, re.MULTILINE) |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 75 | RUST_COPYRIGHT_PATTERN = re.compile(RUST_COPYRIGHT_LINE, re.MULTILINE) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 76 | LICENSE_ID_PATTERN = re.compile(LICENSE_ID_LINE, re.MULTILINE) |
| 77 | |
| 78 | CURRENT_YEAR = str(datetime.datetime.now().year) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 79 | |
| 80 | COPYRIGHT_OK = 0 |
| 81 | COPYRIGHT_ERROR = 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 82 | |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 83 | def check_copyright(path, args, rusted=False, encoding='utf-8'): |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 84 | '''Checks a file for a correct copyright header.''' |
| 85 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 86 | result = COPYRIGHT_OK |
| 87 | |
| 88 | with open(path, encoding=encoding) as file_: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 89 | file_content = file_.read() |
| 90 | |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 91 | if rusted: |
| 92 | copyright_line = RUST_COPYRIGHT_PATTERN.search(file_content) |
| 93 | else: |
| 94 | copyright_line = COPYRIGHT_PATTERN.search(file_content) |
| 95 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 96 | if not copyright_line: |
| 97 | print("ERROR: Missing copyright in " + file_.name) |
| 98 | result = COPYRIGHT_ERROR |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 99 | elif not rusted and CURRENT_YEAR not in copyright_line.group(): |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 100 | print("WARNING: Copyright is out of date in " + file_.name + ": '" + |
| 101 | copyright_line.group() + "'") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 102 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 103 | if not LICENSE_ID_PATTERN.search(file_content): |
| 104 | print("ERROR: License ID error in " + file_.name) |
| 105 | result = COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 106 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 107 | return result |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 108 | |
| 109 | def main(args): |
| 110 | print("Checking the copyrights in the code...") |
| 111 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 112 | if args.verbose: |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 113 | if args.rusted: |
| 114 | print ("Copyright regexp: " + RUST_COPYRIGHT_LINE) |
| 115 | else: |
| 116 | print ("Copyright regexp: " + COPYRIGHT_LINE) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 117 | print ("License regexp: " + LICENSE_ID_LINE) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 118 | |
| 119 | if args.patch: |
Harrison Mutai | 7a93cd2 | 2022-09-29 12:31:31 +0100 | [diff] [blame] | 120 | print("Checking files added between patches " + args.from_ref |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 121 | + " and " + args.to_ref + "...") |
| 122 | |
| 123 | (rc, stdout, stderr) = utils.shell_command(['git', 'diff', |
Harrison Mutai | 7a93cd2 | 2022-09-29 12:31:31 +0100 | [diff] [blame] | 124 | '--diff-filter=ACRT', '--name-only', args.from_ref, args.to_ref ]) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 125 | if rc: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 126 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 127 | |
| 128 | files = stdout.splitlines() |
| 129 | |
| 130 | else: |
| 131 | print("Checking all files tracked by git...") |
| 132 | |
| 133 | (rc, stdout, stderr) = utils.shell_command([ 'git', 'ls-files' ]) |
| 134 | if rc: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 135 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 136 | |
| 137 | files = stdout.splitlines() |
| 138 | |
| 139 | count_ok = 0 |
| 140 | count_warning = 0 |
| 141 | count_error = 0 |
| 142 | |
| 143 | for f in files: |
| 144 | |
| 145 | if utils.file_is_ignored(f, VALID_FILE_EXTENSIONS, IGNORED_FILES, IGNORED_FOLDERS): |
| 146 | if args.verbose: |
| 147 | print("Ignoring file " + f) |
| 148 | continue |
| 149 | |
| 150 | if args.verbose: |
| 151 | print("Checking file " + f) |
| 152 | |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 153 | rc = check_copyright(f, args, rusted=args.rusted) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 154 | |
| 155 | if rc == COPYRIGHT_OK: |
| 156 | count_ok += 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 157 | elif rc == COPYRIGHT_ERROR: |
| 158 | count_error += 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 159 | |
| 160 | print("\nSummary:") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 161 | print("\t{} files analyzed".format(count_ok + count_error)) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 162 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 163 | if count_error == 0: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 164 | print("\tNo errors found") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 165 | return COPYRIGHT_OK |
| 166 | else: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 167 | print("\t{} errors found".format(count_error)) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 168 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 169 | |
| 170 | def parse_cmd_line(argv, prog_name): |
| 171 | parser = argparse.ArgumentParser( |
| 172 | prog=prog_name, |
| 173 | formatter_class=argparse.RawTextHelpFormatter, |
| 174 | description="Check copyright of all files of codebase", |
| 175 | epilog=""" |
| 176 | For each source file in the tree, checks that the copyright header |
| 177 | has the correct format. |
| 178 | """) |
| 179 | |
| 180 | parser.add_argument("--tree", "-t", |
| 181 | help="Path to the source tree to check (default: %(default)s)", |
| 182 | default=os.curdir) |
| 183 | |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame^] | 184 | parser.add_argument("--rusted", "-r", |
| 185 | help="Check for Rusted Firmware CopyRight style (default: %(default)s)", |
| 186 | action='store_true', default=False) |
| 187 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 188 | parser.add_argument("--verbose", "-v", |
| 189 | help="Increase verbosity to the source tree to check (default: %(default)s)", |
| 190 | action='store_true', default=False) |
| 191 | |
| 192 | parser.add_argument("--patch", "-p", |
| 193 | help=""" |
| 194 | Patch mode. |
| 195 | Instead of checking all files in the source tree, the script will consider |
| 196 | only files that are modified by the latest patch(es).""", |
| 197 | action="store_true") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 198 | |
Leonardo Sandoval | 9b3163e | 2020-10-13 12:55:24 -0500 | [diff] [blame] | 199 | (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] | 200 | if rc: |
| 201 | print("Git merge-base command failed. Cannot determine base commit.") |
| 202 | sys.exit(rc) |
| 203 | merge_bases = stdout.splitlines() |
| 204 | |
| 205 | # This should not happen, but it's better to be safe. |
| 206 | if len(merge_bases) > 1: |
| 207 | print("WARNING: Multiple merge bases found. Using the first one as base commit.") |
| 208 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 209 | parser.add_argument("--from-ref", |
| 210 | help="Base commit in patch mode (default: %(default)s)", |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 211 | default=merge_bases[0]) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 212 | parser.add_argument("--to-ref", |
| 213 | help="Final commit in patch mode (default: %(default)s)", |
| 214 | default="HEAD") |
| 215 | |
| 216 | args = parser.parse_args(argv) |
| 217 | return args |
| 218 | |
| 219 | |
| 220 | if __name__ == "__main__": |
| 221 | args = parse_cmd_line(sys.argv[1:], sys.argv[0]) |
| 222 | |
| 223 | os.chdir(args.tree) |
| 224 | |
| 225 | rc = main(args) |
| 226 | |
| 227 | sys.exit(rc) |