copyright/license: Extend file header check

- tighten the copyright regex to include all necessary info
- add checking for proper license identifier
- output only in case of error and put it in stderr

Signed-off-by: Rafal Stefanowski <rafal.stefanowski@intel.com>
This commit is contained in:
Rafal Stefanowski 2022-05-12 13:07:11 +02:00
parent 46229dceed
commit de863c7ec1

View File

@ -5,20 +5,32 @@
# SPDX-License-Identifier: BSD-3-Clause
#
# COPYRIGHT_REGEX is lowercase, because the whole line is
# converted to lowercase before test against this regex.
COPYRIGHT_REGEX="(copyright|\(c\))\s*([0-9]{4}(\s*-\s*([0-9]{4}))?)"
LICENSE_REGEX="SPDX-License-Identifier: BSD-3-Clause$"
YEAR=$(date +"%Y")
REGEX="Copyright\(c\) [0-9]{4}-([0-9]{4}) |Copyright\(c\) ([0-9]{4}) "
while read -r line; do
if [[ "$line" =~ $REGEX ]]; then
echo ${BASH_REMATCH[0]}
if [[ $YEAR == ${BASH_REMATCH[1]} || $YEAR == ${BASH_REMATCH[2]} ]]; then
echo $1 have appropriate license header
exit 0
fi
echo $1 have wrong license header year
exit 1
fi
unset copyright_header license_header
# Read lines until proper copyright and license headers are found.
while read -r line && [[ ! "$copyright_header" || ! "$license_header" ]]; do
if [[ "${line,,}" =~ $COPYRIGHT_REGEX ]]; then
# If the fourth regex group (from year range) doesn't exist,
# use the second regex group instead (from a single year).
copyright_year=${BASH_REMATCH[4]:-${BASH_REMATCH[2]}}
if [[ $copyright_year == $YEAR ]]; then
copyright_header="correct_copyright_header_found"
fi
elif [[ "$line" =~ $LICENSE_REGEX ]]; then
license_header="correct_license_header_found"
fi
done < "$1"
echo $1 does not contain appropriate license header
# Proper copyright and license info were found - all good.
[[ "$copyright_header" && "$license_header" ]] && exit 0
[[ ! "$copyright_header" ]] && echo >&2 "error: file '$1' does not contain any appropriate copyright info"
[[ ! "$license_header" ]] && echo >&2 "error: file '$1' does not contain appropriate license identifier"
exit 1