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 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 28 | class TfaConfig: |
| 29 | def __init__(self): |
| 30 | # File extensions to check |
| 31 | self.valid_file_ext = ( |
| 32 | '.c', '.conf', '.dts', '.dtsi', '.editorconfig', |
| 33 | '.h', '.i', '.ld', 'Makefile', '.mk', '.msvc', |
| 34 | '.py', '.S', '.scat', '.sh', '.rs' |
| 35 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 36 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 37 | # Paths inside the tree to ignore. Hidden folders and files are always |
| 38 | # ignored. They mustn't end in '/'. |
| 39 | self.ignored_folders = ( |
| 40 | 'include/lib/hob', |
| 41 | 'include/lib/libfdt', |
| 42 | 'lib/compiler-rt', |
| 43 | 'lib/hob', |
| 44 | 'lib/libfdt', |
| 45 | 'lib/zlib' |
| 46 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 47 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 48 | # List of ignored files in folders that aren't ignored |
| 49 | self.ignored_files = ( |
| 50 | 'include/tools_share/uuid.h', |
| 51 | ) |
| 52 | |
| 53 | self.copyright_line = LINE_START + 'Copyright' + '.*' + TIME_PERIOD + '.*' + EOL |
| 54 | self.copyright_pattern = re.compile(self.copyright_line, re.MULTILINE) |
| 55 | self.copyright_check_year = True |
| 56 | |
| 57 | class RfaConfig: |
| 58 | def __init__(self): |
| 59 | # File extensions to check |
| 60 | self.valid_file_ext = ( |
| 61 | '.c', '.conf', '.dts', '.dtsi', '.editorconfig', |
| 62 | '.h', '.i', '.ld', 'Makefile', '.mk', '.msvc', |
| 63 | '.py', '.S', '.scat', '.sh', '.rs' |
| 64 | ) |
| 65 | |
| 66 | # Paths inside the tree to ignore. Hidden folders and files are always |
| 67 | # ignored. They mustn't end in '/'. |
| 68 | self.ignored_folders = ( |
| 69 | 'include/lib/hob', |
| 70 | 'include/lib/libfdt', |
| 71 | 'lib/compiler-rt', |
| 72 | 'lib/hob', |
| 73 | 'lib/libfdt', |
| 74 | 'lib/zlib' |
| 75 | ) |
| 76 | |
| 77 | # List of ignored files in folders that aren't ignored |
| 78 | self.ignored_files = ( |
| 79 | 'include/tools_share/uuid.h', |
| 80 | ) |
| 81 | |
| 82 | self.copyright_line = LINE_START + 'Copyright The Rusted Firmware-A Contributors.' + EOL |
| 83 | self.copyright_pattern = re.compile(self.copyright_line, re.MULTILINE) |
| 84 | self.copyright_check_year = False |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 85 | |
| 86 | # Supported comment styles (Python regex) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 87 | COMMENT_PATTERN = '(\*|/\*|\#|//)' |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 88 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 89 | # Any combination of spaces and/or tabs |
| 90 | SPACING = '[ \t]*' |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 91 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 92 | # Line must start with a comment and optional spacing |
| 93 | LINE_START = '^' + SPACING + COMMENT_PATTERN + SPACING |
| 94 | |
| 95 | # Line end with optional spacing |
| 96 | EOL = SPACING + '$' |
| 97 | |
| 98 | # Year or period as YYYY or YYYY-YYYY |
| 99 | TIME_PERIOD = '[0-9]{4}(-[0-9]{4})?' |
| 100 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 101 | CURRENT_YEAR = str(datetime.datetime.now().year) |
| 102 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 103 | # Any string with valid license ID, don't allow adding postfix |
Chris Kay | 345873c | 2021-04-27 13:24:51 +0100 | [diff] [blame] | 104 | LICENSE_ID = '.*(BSD-3-Clause|BSD-2-Clause-FreeBSD|MIT)([ ,.\);].*)?' |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 105 | LICENSE_ID_LINE = LINE_START + 'SPDX-License-Identifier:' + LICENSE_ID + EOL |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 106 | LICENSE_ID_PATTERN = re.compile(LICENSE_ID_LINE, re.MULTILINE) |
| 107 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 108 | COPYRIGHT_OK = 0 |
| 109 | COPYRIGHT_ERROR = 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 110 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 111 | def check_copyright(path, copyright_pattern, check_year, encoding='utf-8'): |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 112 | '''Checks a file for a correct copyright header.''' |
| 113 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 114 | result = COPYRIGHT_OK |
| 115 | |
| 116 | with open(path, encoding=encoding) as file_: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 117 | file_content = file_.read() |
| 118 | |
Tomás González | 3923b55 | 2025-06-04 14:04:00 +0100 | [diff] [blame] | 119 | copyright_line = copyright_pattern.search(file_content) |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame] | 120 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 121 | if not copyright_line: |
| 122 | print("ERROR: Missing copyright in " + file_.name) |
| 123 | result = COPYRIGHT_ERROR |
Tomás González | 3923b55 | 2025-06-04 14:04:00 +0100 | [diff] [blame] | 124 | elif check_year and CURRENT_YEAR not in copyright_line.group(): |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 125 | print("WARNING: Copyright is out of date in " + file_.name + ": '" + |
| 126 | copyright_line.group() + "'") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 127 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 128 | if not LICENSE_ID_PATTERN.search(file_content): |
| 129 | print("ERROR: License ID error in " + file_.name) |
| 130 | result = COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 131 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 132 | return result |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 133 | |
| 134 | def main(args): |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 135 | # Load the project's configuration (either TF-A's or RF-A's). |
| 136 | if not args.rusted: |
| 137 | config = TfaConfig() |
| 138 | else: |
| 139 | config = RfaConfig() |
| 140 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 141 | print("Checking the copyrights in the code...") |
| 142 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 143 | if args.verbose: |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 144 | print ("Copyright regexp: " + config.copyright_line) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 145 | print ("License regexp: " + LICENSE_ID_LINE) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 146 | |
| 147 | if args.patch: |
Harrison Mutai | 7a93cd2 | 2022-09-29 12:31:31 +0100 | [diff] [blame] | 148 | print("Checking files added between patches " + args.from_ref |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 149 | + " and " + args.to_ref + "...") |
| 150 | |
| 151 | (rc, stdout, stderr) = utils.shell_command(['git', 'diff', |
Harrison Mutai | 7a93cd2 | 2022-09-29 12:31:31 +0100 | [diff] [blame] | 152 | '--diff-filter=ACRT', '--name-only', args.from_ref, args.to_ref ]) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 153 | if rc: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 154 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 155 | |
| 156 | files = stdout.splitlines() |
| 157 | |
| 158 | else: |
| 159 | print("Checking all files tracked by git...") |
| 160 | |
| 161 | (rc, stdout, stderr) = utils.shell_command([ 'git', 'ls-files' ]) |
| 162 | if rc: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 163 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 164 | |
| 165 | files = stdout.splitlines() |
| 166 | |
| 167 | count_ok = 0 |
| 168 | count_warning = 0 |
| 169 | count_error = 0 |
| 170 | |
| 171 | for f in files: |
| 172 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 173 | if utils.file_is_ignored(f, config.valid_file_ext, config.ignored_files, config.ignored_folders): |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 174 | if args.verbose: |
| 175 | print("Ignoring file " + f) |
| 176 | continue |
| 177 | |
| 178 | if args.verbose: |
| 179 | print("Checking file " + f) |
| 180 | |
Sandrine Afsa | e37c35b | 2025-07-09 10:38:50 +0200 | [diff] [blame^] | 181 | rc = check_copyright(f, config.copyright_pattern, config.copyright_check_year) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 182 | |
| 183 | if rc == COPYRIGHT_OK: |
| 184 | count_ok += 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 185 | elif rc == COPYRIGHT_ERROR: |
| 186 | count_error += 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 187 | |
| 188 | print("\nSummary:") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 189 | print("\t{} files analyzed".format(count_ok + count_error)) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 190 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 191 | if count_error == 0: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 192 | print("\tNo errors found") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 193 | return COPYRIGHT_OK |
| 194 | else: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 195 | print("\t{} errors found".format(count_error)) |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 196 | return COPYRIGHT_ERROR |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 197 | |
| 198 | def parse_cmd_line(argv, prog_name): |
| 199 | parser = argparse.ArgumentParser( |
| 200 | prog=prog_name, |
| 201 | formatter_class=argparse.RawTextHelpFormatter, |
| 202 | description="Check copyright of all files of codebase", |
| 203 | epilog=""" |
| 204 | For each source file in the tree, checks that the copyright header |
| 205 | has the correct format. |
| 206 | """) |
| 207 | |
| 208 | parser.add_argument("--tree", "-t", |
| 209 | help="Path to the source tree to check (default: %(default)s)", |
| 210 | default=os.curdir) |
| 211 | |
Tomás González | 1150fb8 | 2025-06-04 10:05:21 +0100 | [diff] [blame] | 212 | parser.add_argument("--rusted", "-r", |
| 213 | help="Check for Rusted Firmware CopyRight style (default: %(default)s)", |
| 214 | action='store_true', default=False) |
| 215 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 216 | parser.add_argument("--verbose", "-v", |
| 217 | help="Increase verbosity to the source tree to check (default: %(default)s)", |
| 218 | action='store_true', default=False) |
| 219 | |
| 220 | parser.add_argument("--patch", "-p", |
| 221 | help=""" |
| 222 | Patch mode. |
| 223 | Instead of checking all files in the source tree, the script will consider |
| 224 | only files that are modified by the latest patch(es).""", |
| 225 | action="store_true") |
| 226 | parser.add_argument("--from-ref", |
| 227 | help="Base commit in patch mode (default: %(default)s)", |
Harrison Mutai | 1f1e6cf | 2025-06-24 09:28:18 +0000 | [diff] [blame] | 228 | default="remotes/origin/integration") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 229 | parser.add_argument("--to-ref", |
| 230 | help="Final commit in patch mode (default: %(default)s)", |
| 231 | default="HEAD") |
| 232 | |
| 233 | args = parser.parse_args(argv) |
| 234 | return args |
| 235 | |
| 236 | |
| 237 | if __name__ == "__main__": |
| 238 | args = parse_cmd_line(sys.argv[1:], sys.argv[0]) |
| 239 | |
| 240 | os.chdir(args.tree) |
| 241 | |
| 242 | rc = main(args) |
| 243 | |
| 244 | sys.exit(rc) |