Add year checking in PRESUBMIT.py

Fixes #47110

Change-Id: I88b20a1ad4cbc5be200db84563ad4024aebc419f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212469
Reviewed-by: William Hesse <whesse@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
This commit is contained in:
Ahmed Ashour 2023-02-01 16:31:32 +00:00 committed by Commit Queue
parent c14cc6d75b
commit 43dbc91025

View file

@ -8,6 +8,7 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into gcl.
"""
import datetime
import imp
import os
import os.path
@ -24,6 +25,10 @@ def is_cpp_file(path):
return path.endswith('.cc') or path.endswith('.h')
def is_dart_file(path):
return path.endswith('.dart')
def _CheckNnbdTestSync(input_api, output_api):
"""Make sure that any forked SDK tests are kept in sync. If a CL touches
a test, the test's counterpart (if it exists at all) should be in the CL
@ -353,6 +358,30 @@ def _CheckTestMatrixValid(input_api, output_api):
]
def _CheckCopyrightYear(input_api, output_api):
"""Check copyright year in new files."""
files = []
year = str(datetime.datetime.now().year)
for f in input_api.AffectedFiles(include_deletes=False):
path = f.LocalPath()
if (is_dart_file(path) or is_cpp_file(path)
) and f.Action() == 'A' and os.path.isfile(path):
with open(path) as f:
first_line = f.readline()
if 'Copyright' in first_line and year not in first_line:
files.append(path)
if not files:
return []
return [
output_api.PresubmitPromptWarning(
'Copyright year for new files should be ' + year + ':\n' +
'\n'.join(files))
]
def _CommonChecks(input_api, output_api):
results = []
results.extend(_CheckNnbdTestSync(input_api, output_api))
@ -364,6 +393,7 @@ def _CommonChecks(input_api, output_api):
results.extend(_CheckTestMatrixValid(input_api, output_api))
results.extend(
input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
results.extend(_CheckCopyrightYear(input_api, output_api))
return results