|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates |
| 2 | + |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from datetime import datetime, timezone |
| 6 | +import json |
| 7 | +import pathlib |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | + |
| 12 | +HOLDER = "Arm" |
| 13 | +CURRENT_YEAR = str(datetime.now(timezone.utc).year) |
| 14 | + |
| 15 | + |
| 16 | +# Ensures all modified files with Arm copyright have the current year; |
| 17 | +# lists the paths of files that need updating |
| 18 | +def main(): |
| 19 | + outdated_year_files = [] |
| 20 | + report = json.loads(pathlib.Path("scancode_report.json").read_text()) |
| 21 | + repo_root = pathlib.Path(os.getcwd()).resolve() |
| 22 | + path_prefix = f"{repo_root.parent.name}/{repo_root.name}/" |
| 23 | + |
| 24 | + # Builds a dictionary of all files and their corresponding copyright |
| 25 | + all_copyrights = {} |
| 26 | + for file in report.get("files", []): |
| 27 | + scanned_path = file.get("path", "") |
| 28 | + if not scanned_path.startswith(path_prefix): |
| 29 | + continue |
| 30 | + relative_path = scanned_path[slice(len(path_prefix), None)] |
| 31 | + all_copyrights[relative_path] = [ |
| 32 | + cp["value"] for cp in file.get("copyrights", []) |
| 33 | + ] |
| 34 | + |
| 35 | + # Determine what files have been modified by looking at the commit range |
| 36 | + base = os.getenv("CI_MERGE_REQUEST_DIFF_BASE_SHA") or "HEAD^" |
| 37 | + head = os.getenv("CI_COMMIT_SHA") or "HEAD" |
| 38 | + changed = set( |
| 39 | + subprocess.check_output( |
| 40 | + ["git", "diff", "--name-only", f"{base}...{head}"], text=True |
| 41 | + ).splitlines() |
| 42 | + ) |
| 43 | + |
| 44 | + for relative_path in changed: |
| 45 | + file_copyrights = all_copyrights.get(relative_path, []) |
| 46 | + # If a file doesn't have a copyright, or it is correctly formatted, skip it |
| 47 | + if (not file_copyrights) or ( |
| 48 | + any(HOLDER in cp and CURRENT_YEAR in cp for cp in file_copyrights) |
| 49 | + ): |
| 50 | + continue |
| 51 | + if any(HOLDER in cp for cp in file_copyrights): |
| 52 | + outdated_year_files.append(relative_path) |
| 53 | + |
| 54 | + if outdated_year_files: |
| 55 | + print( |
| 56 | + "Please update the copyright year to", |
| 57 | + CURRENT_YEAR, |
| 58 | + "in the following files:", |
| 59 | + ) |
| 60 | + print("\n".join(f" - {p}" for p in outdated_year_files)) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + main() |
0 commit comments