diff --git a/tools/check_source/check_licenses.py b/tools/check_source/check_licenses.py index 1efc513b328..dd649414317 100644 --- a/tools/check_source/check_licenses.py +++ b/tools/check_source/check_licenses.py @@ -232,6 +232,18 @@ def check_contents(filepath: str, text: str) -> None: """ text_header = text[:EXPECT_SPDX_IN_FIRST_CHARS] + # Use the license to limit the copyright search, + # so code-generation that includes copyright headers don't cause false alarms. + license_id = " SPDX-License-Identifier: " + license_id_beg = text_header.find(license_id) + if license_id_beg == -1: + # Allow completely empty files (sometimes `__init__.py`). + if not text.rstrip(): + return + # Empty file already accounted for. + print("Missing {:s}{:s}".format(license_id, filepath)) + return + # Check copyright text, reading multiple (potentially multi-line indented) blocks. copyright_id = " SPDX-FileCopyrightText: " @@ -242,7 +254,7 @@ def check_contents(filepath: str, text: str) -> None: text_header, copyright_id, copyright_id_step, - EXPECT_SPDX_IN_FIRST_CHARS, + license_id_beg, )) != (-1, -1)): if copyright_id_end == -1: # Set once. @@ -262,9 +274,6 @@ def check_contents(filepath: str, text: str) -> None: del copyright_id_item, copyright_id_step if copyright_id_beg == -1: - # Allow completely empty files (sometimes `__init__.py`). - if not text.rstrip(): - return print("Missing {:s}{:s}".format(copyright_id, filepath)) # Maintain statistics. @@ -279,13 +288,6 @@ def check_contents(filepath: str, text: str) -> None: if blank_lines > 0: print("SPDX \"{:s}\" not on first line: {:s}".format(copyright_id, filepath)) - license_id = " SPDX-License-Identifier: " - license_id_beg = text_header.find(license_id, copyright_id_end) - if license_id_beg == -1: - # Empty file already accounted for. - print("Missing {:s}{:s}".format(license_id, filepath)) - return - # Leading char. leading_char = text_header[txt_prev_bol(text_header, license_id_beg, 0):license_id_beg].strip() text_blank_line = text_header[copyright_id_end:license_id_beg]