perf: add TCP Nginx+LDPRELOAD suites
[csit.git] / resources / libraries / bash / entry / check / gpl_license.sh
1 # Copyright (c) 2020 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 set -exuo pipefail
15
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.
20
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).
26
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
32     exit 1
33 }
34
35 IFS=$'\n'
36 gpl_files=($(git diff --name-only HEAD~ | grep '^GPL/' || true))
37 unset IFS
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.
41 set +x
42 for fil in "${gpl_files[@]}"; do
43     if head -n 3 "${fil}" | fgrep -iq 'Copyright'; then
44         # Copyrighted file, processed below.
45         true
46     else
47         # Uncopyrighted files are allowed.
48         # TODO: Should we have list of extesions that require Copyright?
49         continue
50     fi
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"?
54         continue
55     else
56         echo "GPL license not detected: ${fil}" >> "${logfile}"
57     fi
58 done
59 set -x
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.
65     pwd
66     cat "${logfile}" >&2
67     warn
68     warn "GPL license checker: FAIL"
69     exit 1
70 fi
71
72 warn
73 warn "GPL license checker: PASS"