1 # Copyright (c) 2019 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 examines any file edited since HEAD~ (filtered by extension),
22 # and fails if no line with "Copyright" and the current year is found.
23 # The list of offending files is written to copyright.log (overwriting).
24 # The log also specifies whether the copyright is missing or outdated.
26 # As copyright notice usually gives readers also a license to use it,
27 # any file without copyright is potentially unusable (by non-authors).
28 # In the interest of open source code reuse, CSIT wants any content
29 # to be available under appropriate license, which depends on whether the file
30 # is deemed "code" (Apache License, Version 2.0), "documentation"
31 # (Creative Commons Attribution 4.0 International), or something else.
32 # Note that this checker does not check licenses,
33 # it assumes any copyright notice includes the correct data
34 # (all contributing people/organizations, all applicabe licenses).
36 # Unfortunately, some file types are not designed to hold copyright notice,
37 # or at least the correct way to include it is not known yet.
38 # Or the file in question is processed by code which is not ready
39 # for (otherwise allowed) comment lines holding the copyright.
40 # That is why currently we need to explicitly whitelist filename patterns.
42 # TODO: Figure out how to add copyright notice into .svg files.
43 # Do the usual .xml style comments work?
44 # TODO: Make the code processing text files (example: VPP_REPO_URL)
45 # tolerate comment lines.
46 # TODO: Verify more extensions are safe to whitelist (.virl)
47 # and that tools processing some format allows comments (.json).
48 # TODO: Ultimately remove filtering to start checking every file.
50 # "set -eu" handles failures from the following two lines.
51 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
52 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
53 source "${BASH_FUNCTION_DIR}/common.sh" || {
54 echo "Source failed." >&2
59 year="${fulldate##* }"
60 # Regexp selecting only files we expect to allow copyright.
61 # Start from an array, each item is to be prepended by ., appended by $,
63 extensions_array=("gitgnore" "ini" "md" "py" "robot" "rst" "sh" "txt" "yaml")
65 for extension in "${extensions_array[@]}"; do
66 pattern+="\.${extension}"'$'"\|"
68 # One more "extension" just to avoid trailing pipe.
69 pattern+="/Dockerfile"'$'
70 # Get array of edited files.
71 # Commands to expand, pipe has to be left out.
72 cmd1="git diff --name-only HEAD~"
73 cmd2="grep \"${pattern}\""
74 # When calling, commands has to be without quotes.
75 readarray -t file_array <<<$(${cmd1} | ${cmd2}) || {
76 # We need to tolerate changes that only edit other files.
77 errors=$(${cmd1} | ${cmd2} 2>&1 || true)
78 if [[ "${errors}" ]]; then
79 # TODO: do we need to echo errors?
80 die "Failure at getting list of files to check copyright in."
82 # Empty file array is accepted.
83 # Accidentally, if "git diff" fails, we still proceed with empty array.
85 logfile="copyright.log"
86 truncate --size 0 "${logfile}" || die "truncate failed"
87 # Temporary +x so big changes do not spam.
89 for filename in "${file_array[@]}"; do
90 if ! fgrep -q "Copyright" "${filename}"; then
91 echo "No copyright found in file: ${filename}" >> "${logfile}"
92 elif ! fgrep "Copyright" "${filename}" | fgrep -q "${year}"; then
93 echo "No year ${year} copyright found in: ${filename}" >> "${logfile}"
97 if [ -s "${logfile}" ]; then
98 warn "Copyright violations detected."
99 # TODO: Disable when output size does more harm than good.
102 warn "Copyright checker: FAIL"
106 warn "Copyright checker: PASS"