Skip to content

Commit 155a371

Browse files
kartbennashif
authored andcommitted
script: ci: check_compliance: add LicenseAndCopyright compliance check
Implement a new compliance check to ensure that all modified files have correct licensing information (SPDX-License-Identifier as well as copyright info). Signed-off-by: Benjamin Cabé <[email protected]>
1 parent 7d510ac commit 155a371

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ tags
8888
.idea
8989

9090
# from check_compliance.py
91+
# zephyr-keep-sorted-start
9192
BinaryFiles.txt
9293
BoardYml.txt
9394
Checkpatch.txt
@@ -102,6 +103,7 @@ KconfigBasic.txt
102103
KconfigBasicNoModules.txt
103104
KconfigHWMv2.txt
104105
KeepSorted.txt
106+
LicenseAndCopyrightCheck.txt
105107
MaintainersFormat.txt
106108
ModulesMaintainers.txt
107109
Nits.txt
@@ -115,3 +117,4 @@ SysbuildKconfigBasicNoModules.txt
115117
TextEncoding.txt
116118
YAMLLint.txt
117119
ZephyrModuleFile.txt
120+
# zephyr-keep-sorted-stop

scripts/ci/check_compliance.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import tempfile
1919
import textwrap
2020
import traceback
21+
from collections.abc import Iterable
2122
from itertools import takewhile
2223
from pathlib import Path, PurePath
2324

@@ -26,6 +27,8 @@
2627
import yaml
2728
from dotenv import load_dotenv
2829
from junitparser import Error, Failure, JUnitXml, Skipped, TestCase, TestSuite
30+
from reuse.project import Project
31+
from reuse.report import ProjectReport, ProjectSubsetReport
2932
from west.manifest import Manifest, ManifestProject
3033
from yamllint import config, linter
3134

@@ -1557,6 +1560,68 @@ def run(self):
15571560
self.failure("\n".join(offending_lines))
15581561

15591562

1563+
class LicenseAndCopyrightCheck(ComplianceTest):
1564+
"""
1565+
Verify that every file touched by the patch set has correct SPDX headers and uses allowed
1566+
license.
1567+
"""
1568+
1569+
name = "LicenseAndCopyrightCheck"
1570+
doc = "Check SPDX headers and copyright lines with the reuse Python API."
1571+
1572+
def _report_violations(
1573+
self,
1574+
paths: Iterable[Path],
1575+
title: str,
1576+
severity: str,
1577+
desc: str | None = None,
1578+
) -> None:
1579+
for p in paths:
1580+
rel_path = os.path.relpath(str(p), GIT_TOP)
1581+
self.fmtd_failure(severity, title, rel_path, desc=desc or "", line=1)
1582+
1583+
def run(self) -> None:
1584+
changed_files = get_files(filter="d")
1585+
if not changed_files:
1586+
return
1587+
1588+
# Only scan text files for now, in the future we may want to leverage REUSE standard's
1589+
# ability to also associate license/copyright info with binary files.
1590+
for file in changed_files:
1591+
full_path = GIT_TOP / file
1592+
mime_type = magic.from_file(os.fspath(full_path), mime=True)
1593+
if not mime_type.startswith("text/"):
1594+
changed_files.remove(file)
1595+
1596+
project = Project.from_directory(GIT_TOP)
1597+
report = ProjectSubsetReport.generate(project, changed_files, multiprocessing=False)
1598+
1599+
self._report_violations(
1600+
report.files_without_licenses,
1601+
"License missing",
1602+
"warning",
1603+
"File has no SPDX-License-Identifier header, consider adding one.",
1604+
)
1605+
1606+
self._report_violations(
1607+
report.files_without_copyright,
1608+
"Copyright missing",
1609+
"warning",
1610+
"File has no SPDX-FileCopyrightText header, consider adding one.",
1611+
)
1612+
1613+
for lic_id, paths in getattr(report, "missing_licenses", {}).items():
1614+
self._report_violations(
1615+
paths,
1616+
"License may not be allowed",
1617+
"warning",
1618+
(
1619+
f"License file for '{lic_id}' not found in /LICENSES. Please check "
1620+
"https://docs.zephyrproject.org/latest/contribute/guidelines.html#components-using-other-licenses."
1621+
),
1622+
)
1623+
1624+
15601625
class GitLint(ComplianceTest):
15611626
"""
15621627
Runs gitlint on the commits and finds issues with style and syntax

0 commit comments

Comments
 (0)