Skip to content

Commit 73f3882

Browse files
Angelinaaggarg
authored andcommitted
ci-license: Check for current year in the license job
When a file with an Arm copyright header is modified, CI will fail if the header does not include the current year, and it will print the paths of the offending files. Signed-off-by: Angelina Dobardzieva <[email protected]>
1 parent 5260140 commit 73f3882

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ci-license: Check for current year in the license job

tools/ci/pipeline-baseline-fri.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ license:
2727
tags:
2828
- iotmsw-amd64
2929
script:
30-
- scancode -l --ignore "**/*.tmp/**" --json-pp scancode_report.json $PWD/..
30+
- scancode -lc --ignore "**/*.tmp/**" --json-pp scancode_report.json $PWD/..
3131
- jsonschema -i scancode_report.json $PWD/tools/ci/license/license.schema
32+
- python3 ${PWD}/tools/scripts/copyright_date_check.py
3233
artifacts:
3334
paths:
3435
- scancode_report.json
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)