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