Tools: Don't update the copyright year automatically in the headers

This commit is contained in:
Roland Dobai
2022-01-14 12:33:18 +01:00
parent 6050388f51
commit f4bf2a0fd7

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
# SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
""" """
Check files for copyright headers: Check files for copyright headers:
@@ -22,7 +22,7 @@ import os
import re import re
import sys import sys
import textwrap import textwrap
from typing import List, Tuple from typing import List, Optional, Tuple
import pathspec import pathspec
import yaml import yaml
@@ -251,7 +251,7 @@ def has_valid_copyright(file_name: str, mime: str, is_on_ignore: bool, config_se
if matches: if matches:
detected_notices.append((matches.group(1), comment.line_number())) detected_notices.append((matches.group(1), comment.line_number()))
try: try:
year = extract_year_from_espressif_notice(matches.group(1)) years = extract_years_from_espressif_notice(matches.group(1))
except NotFound as e: except NotFound as e:
if args.verbose: if args.verbose:
print(f'{TERMINAL_GRAY}Not an {e.thing} {file_name}:{comment.line_number()}{TERMINAL_RESET}') print(f'{TERMINAL_GRAY}Not an {e.thing} {file_name}:{comment.line_number()}{TERMINAL_RESET}')
@@ -263,13 +263,17 @@ def has_valid_copyright(file_name: str, mime: str, is_on_ignore: bool, config_se
template = '/* SPDX-FileCopyrightText: ' + config_section['espressif_copyright'] template = '/* SPDX-FileCopyrightText: ' + config_section['espressif_copyright']
if mime == MIME['python']: if mime == MIME['python']:
template = '# SPDX-FileCopyrightText: ' + config_section['espressif_copyright'] template = '# SPDX-FileCopyrightText: ' + config_section['espressif_copyright']
code_lines[comment.line_number() - 1] = template.format(years=format_years(year, file_name)) candidate_line = template.format(years=format_years(years[0], file_name))
no_time_update = template.format(years=format_years(years[0], file_name, years[1]))
if code_lines[comment.line_number() - 1] != no_time_update:
# update the line only in cases when not only the dates are changing
code_lines[comment.line_number() - 1] = candidate_line
matches = re.search(r'SPDX-FileContributor: ?(.*)', comment.text(), re.IGNORECASE) matches = re.search(r'SPDX-FileContributor: ?(.*)', comment.text(), re.IGNORECASE)
if matches: if matches:
detected_contributors.append((matches.group(1), comment.line_number())) detected_contributors.append((matches.group(1), comment.line_number()))
try: try:
year = extract_year_from_espressif_notice(matches.group(1)) years = extract_years_from_espressif_notice(matches.group(1))
except NotFound as e: except NotFound as e:
if args.debug: if args.debug:
print(f'{TERMINAL_GRAY}Not an {e.thing} {file_name}:{comment.line_number()}{TERMINAL_RESET}') print(f'{TERMINAL_GRAY}Not an {e.thing} {file_name}:{comment.line_number()}{TERMINAL_RESET}')
@@ -281,7 +285,11 @@ def has_valid_copyright(file_name: str, mime: str, is_on_ignore: bool, config_se
template = '/* SPDX-FileContributor: ' + config_section['espressif_copyright'] template = '/* SPDX-FileContributor: ' + config_section['espressif_copyright']
if mime == MIME['python']: if mime == MIME['python']:
template = '# SPDX-FileContributor: ' + config_section['espressif_copyright'] template = '# SPDX-FileContributor: ' + config_section['espressif_copyright']
code_lines[comment.line_number() - 1] = template.format(years=format_years(year, file_name)) candidate_line = template.format(years=format_years(years[0], file_name))
no_time_update = template.format(years=format_years(years[0], file_name, years[1]))
if code_lines[comment.line_number() - 1] != no_time_update:
# update the line only in cases when not only the dates are changing
code_lines[comment.line_number() - 1] = candidate_line
matches = re.search(r'SPDX-License-Identifier: ?(.*)', comment.text(), re.IGNORECASE) matches = re.search(r'SPDX-License-Identifier: ?(.*)', comment.text(), re.IGNORECASE)
if matches: if matches:
@@ -342,13 +350,15 @@ def insert_copyright(code_lines: list, file_name: str, mime: str, config_section
return new_code_lines return new_code_lines
def extract_year_from_espressif_notice(notice: str) -> int: def extract_years_from_espressif_notice(notice: str) -> Tuple[int, Optional[int]]:
""" """
Extracts copyright year (creation date) from a Espressif copyright notice Extracts copyright years from a Espressif copyright notice. It returns a tuple (x, y) where x is the first year of
the copyright and y is the second year. y is None if the copyright notice contains only one year.
""" """
matches = re.search(r'(\d{4})(?:-\d{4})? Espressif Systems', notice, re.IGNORECASE) matches = re.search(r'(\d{4})(-(\d{4}))? Espressif Systems', notice, re.IGNORECASE)
if matches: if matches:
return int(matches.group(1)) years = matches.group(1, 3)
return (int(years[0]), int(years[1]) if years[1] else None)
raise NotFound('Espressif copyright notice') raise NotFound('Espressif copyright notice')
@@ -390,7 +400,7 @@ def detect_old_header_style(file_name: str, comments: list, args: argparse.Names
if comment.line_number() > args.max_lines: if comment.line_number() > args.max_lines:
break break
try: try:
year = extract_year_from_espressif_notice(comment.text()) year = extract_years_from_espressif_notice(comment.text())[0]
except NotFound: except NotFound:
pass pass
else: else:
@@ -398,23 +408,23 @@ def detect_old_header_style(file_name: str, comments: list, args: argparse.Names
raise NotFound('Old Espressif header') raise NotFound('Old Espressif header')
def format_years(past: int, file_name: str) -> str: def format_years(past: int, file_name: str, today: Optional[int]=None) -> str:
""" """
Function to format a year: Function to format a year:
- just current year -> output: [year] - just current year -> output: [year]
- some year in the past -> output: [past year]-[current year] - some year in the past -> output: [past year]-[current year]
""" """
today = datetime.datetime.now().year _today = today or datetime.datetime.now().year
if past == 0: if past == 0:
# use the current year # use the current year
past = today past = _today
if past == today: if past == _today:
return str(past) return str(past)
if past > today or past < 1972: if past > _today or past < 1972:
error_msg = f'{file_name}: invalid year in the copyright header detected. ' \ error_msg = f'{file_name}: invalid year in the copyright header detected. ' \
+ 'Check your system clock and the copyright header.' + 'Check your system clock and the copyright header.'
raise ValueError(error_msg) raise ValueError(error_msg)
return '{past}-{today}'.format(past=past, today=today) return '{past}-{today}'.format(past=past, today=_today)
def check_copyrights(args: argparse.Namespace, config: configparser.ConfigParser) -> Tuple[List, List]: def check_copyrights(args: argparse.Namespace, config: configparser.ConfigParser) -> Tuple[List, List]: