1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
16 # This file should be executed from tox, as the assumed working directory
17 # is different from where this file is located.
18 # This file does not have executable flag nor shebang,
19 # to dissuade non-tox callers.
21 # This script runs a few grep-based commands and fails
22 # if it detects any file edited or added since HEAD~
23 # containing a copyright notice in first 3 lines,
24 # but not GPL-based license.
25 # The offending files are stored to gpl_license.log (overwriting).
27 # "set -eu" handles failures from the following two lines.
28 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
29 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
30 source "${BASH_FUNCTION_DIR}/common.sh" || {
31 echo "Source failed." >&2
36 gpl_files=($(git diff --name-only HEAD~ | grep '^GPL/' || true))
38 logfile="gpl_license.log"
39 truncate -s 0 "${logfile}" || die
40 # A change can have thousands of files, supress console output in the cycle.
42 for fil in "${gpl_files[@]}"; do
43 if head -n 3 "${fil}" | fgrep -iq 'Copyright'; then
44 # Copyrighted file, processed below.
47 # Uncopyrighted files are allowed.
48 # TODO: Should we have list of extesions that require Copyright?
51 if fgrep -q 'GNU General Public License v2.0 or later' "${fil}"; then
52 # This can be GPL only or the OR license, we accept both.
53 # TODO: Should we require "Apache-2.0 OR GPL-2.0-or-later"?
56 echo "GPL license not detected: ${fil}" >> "${logfile}"
60 lines="$(< "${logfile}" wc -l)"
61 if [ "${lines}" != "0" ]; then
62 # TODO: Decide which text goes to stdout and which to stderr.
63 warn "Wrong licensed files in GPL directory detected: ${lines}"
64 # TODO: Disable when output size does more harm than good.
68 warn "GPL license checker: FAIL"
73 warn "GPL license checker: PASS"