Mon, 28 May 2018 10:42:25 +0800
Merge
aoqi@0 | 1 | #! /bin/sh -f |
aoqi@0 | 2 | # |
aoqi@0 | 3 | # Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 4 | # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 5 | # |
aoqi@0 | 6 | # This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 7 | # under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 8 | # published by the Free Software Foundation. |
aoqi@0 | 9 | # |
aoqi@0 | 10 | # This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 13 | # version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 14 | # accompanied this code). |
aoqi@0 | 15 | # |
aoqi@0 | 16 | # You should have received a copy of the GNU General Public License version |
aoqi@0 | 17 | # 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 18 | # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 19 | # |
aoqi@0 | 20 | # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 21 | # or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 22 | # questions. |
aoqi@0 | 23 | # |
aoqi@0 | 24 | |
aoqi@0 | 25 | # |
aoqi@0 | 26 | # This script checks a copyright notice. |
aoqi@0 | 27 | # |
aoqi@0 | 28 | # The script should be located in the main jdk repository under make/scripts. |
aoqi@0 | 29 | # It works with the templates in the make/templates directory of the jdk source. |
aoqi@0 | 30 | # |
aoqi@0 | 31 | # Usage: "lic_check.sh [-gpl] or [-gplcp] or [-bsd] file(s)" |
aoqi@0 | 32 | |
aoqi@0 | 33 | script_directory=`dirname $0` |
aoqi@0 | 34 | script_name=`basename $0` |
aoqi@0 | 35 | first_option=$1 |
aoqi@0 | 36 | |
aoqi@0 | 37 | # parse the first argument |
aoqi@0 | 38 | |
aoqi@0 | 39 | case "$1" in |
aoqi@0 | 40 | "-gpl") |
aoqi@0 | 41 | header="gpl-header" |
aoqi@0 | 42 | ;; |
aoqi@0 | 43 | "-gplcp") |
aoqi@0 | 44 | header="gpl-cp-header" |
aoqi@0 | 45 | ;; |
aoqi@0 | 46 | "-bsd") |
aoqi@0 | 47 | header="bsd-header" |
aoqi@0 | 48 | ;; |
aoqi@0 | 49 | *) |
aoqi@0 | 50 | echo "Usage: $0 [-gpl] or [-gplcp] or [-bsd] file(s)" 1>&2 |
aoqi@0 | 51 | exit 1 |
aoqi@0 | 52 | ;; |
aoqi@0 | 53 | esac |
aoqi@0 | 54 | shift |
aoqi@0 | 55 | |
aoqi@0 | 56 | #initialize error status |
aoqi@0 | 57 | error_status=0 |
aoqi@0 | 58 | |
aoqi@0 | 59 | # determine and set the absolute path for the script directory |
aoqi@0 | 60 | D=`dirname "${script_directory}"` |
aoqi@0 | 61 | B=`basename "${script_directory}"` |
aoqi@0 | 62 | script_dir="`cd \"${D}\" 2>/dev/null && pwd || echo \"${D}\"`/${B}" |
aoqi@0 | 63 | |
aoqi@0 | 64 | # set up a variable for the templates directory |
aoqi@0 | 65 | template_dir=${script_dir}/../templates |
aoqi@0 | 66 | |
aoqi@0 | 67 | # Check existence of the template directory. |
aoqi@0 | 68 | if [ ! -d ${template_dir} ] ; then |
aoqi@0 | 69 | echo "ERROR: The templates directory "${template_dir}" doesn't exist." 1>&2 |
aoqi@0 | 70 | exit 1 |
aoqi@0 | 71 | fi |
aoqi@0 | 72 | |
aoqi@0 | 73 | # set the temporary file location |
aoqi@0 | 74 | tmpfile=/tmp/source_file.$$ |
aoqi@0 | 75 | rm -f ${tmpfile} |
aoqi@0 | 76 | |
aoqi@0 | 77 | # check number of lines in the template file |
aoqi@0 | 78 | lines=`cat ${template_dir}/${header} | wc -l` |
aoqi@0 | 79 | |
aoqi@0 | 80 | # the template file has one empty line at the end, we need to ignore it |
aoqi@0 | 81 | lines=`expr ${lines} - 1` |
aoqi@0 | 82 | |
aoqi@0 | 83 | # A loop through the all script parameters: |
aoqi@0 | 84 | # |
aoqi@0 | 85 | # 1. Given a set of source files and a license template header, read a file name of each source file. |
aoqi@0 | 86 | # 2. Check if a given file exists. When a directory is encountered, dive in and process all sources in those directories. |
aoqi@0 | 87 | # 3. Read each line of the given file and check it for a copyright string. |
aoqi@0 | 88 | # 4. If a copyright string found, check the correctness of the years format in the string and replace years with %YEARS%. |
aoqi@0 | 89 | # 5. Continue reading the file until the number of lines is equal to the length of the license template header ($lines) and remove a comment prefix for each line. |
aoqi@0 | 90 | # 6. Store the result (the license header from a given file) into a temporary file. |
aoqi@0 | 91 | # 7. If a temporary file is not empty, compare it with a template file to verify if the license text is the same as in a template. |
aoqi@0 | 92 | # 8. Produce a error in case a temporary file is empty, it means we didn't find a copyright string, or it's not correct |
aoqi@0 | 93 | # |
aoqi@0 | 94 | while [ "$#" -gt "0" ] ; do |
aoqi@0 | 95 | touch ${tmpfile} |
aoqi@0 | 96 | |
aoqi@0 | 97 | # In case of the directory as a parameter check recursively every file inside. |
aoqi@0 | 98 | if [ -d $1 ] ; then |
aoqi@0 | 99 | curdir=`pwd` |
aoqi@0 | 100 | cd $1 |
aoqi@0 | 101 | echo "*** Entering directory: "`pwd` |
aoqi@0 | 102 | echo "***" |
aoqi@0 | 103 | files=`ls .` |
aoqi@0 | 104 | sh ${script_dir}/${script_name} ${first_option} ${files} |
aoqi@0 | 105 | status=$? |
aoqi@0 | 106 | if [ ${error_status} -ne 1 ] ; then |
aoqi@0 | 107 | error_status=${status} |
aoqi@0 | 108 | fi |
aoqi@0 | 109 | cd ${curdir} |
aoqi@0 | 110 | shift |
aoqi@0 | 111 | continue |
aoqi@0 | 112 | else |
aoqi@0 | 113 | echo "### Checking copyright notice in the file: "$1 |
aoqi@0 | 114 | echo "###" |
aoqi@0 | 115 | fi |
aoqi@0 | 116 | |
aoqi@0 | 117 | # Check the existence of the source file. |
aoqi@0 | 118 | if [ ! -f $1 ] ; then |
aoqi@0 | 119 | echo "ERROR: The source file "$1" doesn't exist." 1>&2 |
aoqi@0 | 120 | error_status=1 |
aoqi@0 | 121 | shift |
aoqi@0 | 122 | continue |
aoqi@0 | 123 | fi |
aoqi@0 | 124 | |
aoqi@0 | 125 | # read the source file and determine where the header starts, then get license header without prefix |
aoqi@0 | 126 | counter=0 |
aoqi@0 | 127 | while read line ; do |
aoqi@0 | 128 | # remove windows "line feed" character from the line (if any) |
aoqi@0 | 129 | line=`echo "${line}" | tr -d '\r'` |
aoqi@0 | 130 | # check if the given line contains copyright |
aoqi@0 | 131 | check_copyright=`echo "${line}" | grep "Copyright (c) "` |
aoqi@0 | 132 | if [ "${check_copyright}" != "" ] ; then |
aoqi@0 | 133 | # determine the comment prefix |
aoqi@0 | 134 | prefix=`echo "${line}" | cut -d "C" -f 1` |
aoqi@0 | 135 | # remove prefix (we use "_" as a sed delimiter, since the prefix could be like //) |
aoqi@0 | 136 | copyright_without_prefix=`echo "${line}" | sed s_"^${prefix}"__g` |
aoqi@0 | 137 | # copyright years |
aoqi@0 | 138 | year1=`echo "${copyright_without_prefix}" | cut -d " " -f 3` |
aoqi@0 | 139 | year2=`echo "${copyright_without_prefix}" | cut -d " " -f 4` |
aoqi@0 | 140 | # Processing the first year in the copyright string |
aoqi@0 | 141 | length=`expr "${year1}" : '.*'` |
aoqi@0 | 142 | if [ ${length} -ne 5 ] ; then |
aoqi@0 | 143 | break |
aoqi@0 | 144 | fi |
aoqi@0 | 145 | check_year1=`echo ${year1} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"` |
aoqi@0 | 146 | if [ "${check_year1}" = "" ] ; then |
aoqi@0 | 147 | break |
aoqi@0 | 148 | fi |
aoqi@0 | 149 | # Processing the second year in the copyright string |
aoqi@0 | 150 | if [ "${year2}" != "Oracle" ] ; then |
aoqi@0 | 151 | length=`expr "${year2}" : '.*'` |
aoqi@0 | 152 | if [ ${length} -ne 5 ] ; then |
aoqi@0 | 153 | break |
aoqi@0 | 154 | else |
aoqi@0 | 155 | check_year2=`echo ${year2} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"` |
aoqi@0 | 156 | if [ "${check_year2}" = "" ] ; then |
aoqi@0 | 157 | break |
aoqi@0 | 158 | fi |
aoqi@0 | 159 | fi |
aoqi@0 | 160 | fi |
aoqi@0 | 161 | |
aoqi@0 | 162 | # copyright string without copyright years |
aoqi@0 | 163 | no_years=`echo "${copyright_without_prefix}" | sed 's/[0-9,]*//g'` |
aoqi@0 | 164 | # copyright string before years |
aoqi@0 | 165 | before_years=`echo "${no_years}" | cut -d "O" -f 1` |
aoqi@0 | 166 | # copyright string after years |
aoqi@0 | 167 | after_years=`echo "${no_years}" | cut -d ")" -f 2` |
aoqi@0 | 168 | # form a new copyright string with %YEARS% |
aoqi@0 | 169 | new_copyright=`echo ${before_years}"%YEARS%"${after_years}` |
aoqi@0 | 170 | # save the new copyright string to a file |
aoqi@0 | 171 | echo "${new_copyright}" > ${tmpfile} |
aoqi@0 | 172 | # start counting the lines |
aoqi@0 | 173 | counter=1 |
aoqi@0 | 174 | # move to the next line |
aoqi@0 | 175 | continue |
aoqi@0 | 176 | fi |
aoqi@0 | 177 | if [ ${counter} -ne 0 ] ; then |
aoqi@0 | 178 | # this should be a license header line, hence increment counter |
aoqi@0 | 179 | counter=`expr ${counter} + 1` |
aoqi@0 | 180 | # record a string without a prefix to a file |
aoqi@0 | 181 | newline=`echo "${line}" | sed s_"^${prefix}"__` |
aoqi@0 | 182 | |
aoqi@0 | 183 | # we need to take care of the empty lines in the header, i.e. check the prefix without spaces |
aoqi@0 | 184 | trimmed_prefix=`echo "${prefix}" | tr -d " "` |
aoqi@0 | 185 | trimmed_line=`echo "${line}" | tr -d " "` |
aoqi@0 | 186 | if [ "${trimmed_line}" = "${trimmed_prefix}" ] ; then |
aoqi@0 | 187 | echo "" >> ${tmpfile} |
aoqi@0 | 188 | else |
aoqi@0 | 189 | echo "${newline}" >> ${tmpfile} |
aoqi@0 | 190 | fi |
aoqi@0 | 191 | fi |
aoqi@0 | 192 | # stop reading lines when a license header ends and add an empty line to the end |
aoqi@0 | 193 | if [ ${counter} -eq ${lines} ] ; then |
aoqi@0 | 194 | echo "" >> ${tmpfile} |
aoqi@0 | 195 | break |
aoqi@0 | 196 | fi |
aoqi@0 | 197 | done < $1 |
aoqi@0 | 198 | |
aoqi@0 | 199 | # compare the license header with a template file |
aoqi@0 | 200 | if [ -s ${tmpfile} ] ; then |
aoqi@0 | 201 | diff -c ${tmpfile} ${template_dir}/${header} 1>&2 |
aoqi@0 | 202 | if [ "$?" = "0" ] ; then |
aoqi@0 | 203 | echo "SUCCESS: The license header for "`pwd`"/"$1" has been verified." |
aoqi@0 | 204 | echo "###" |
aoqi@0 | 205 | else |
aoqi@0 | 206 | echo "ERROR: License header is not correct in "`pwd`"/"$1 1>&2 |
aoqi@0 | 207 | echo "See diffs above. " 1>&2 |
aoqi@0 | 208 | echo "###" 1>&2 |
aoqi@0 | 209 | echo "" 1>&2 |
aoqi@0 | 210 | error_status=1 |
aoqi@0 | 211 | fi |
aoqi@0 | 212 | else |
aoqi@0 | 213 | # If we don't have a temporary file, there is a problem with a copyright string (or no copyright string) |
aoqi@0 | 214 | echo "ERROR: Copyright string is not correct or missing in "`pwd`"/"$1 1>&2 |
aoqi@0 | 215 | echo "###" 1>&2 |
aoqi@0 | 216 | echo "" 1>&2 |
aoqi@0 | 217 | error_status=1 |
aoqi@0 | 218 | fi |
aoqi@0 | 219 | rm -f ${tmpfile} |
aoqi@0 | 220 | shift |
aoqi@0 | 221 | done |
aoqi@0 | 222 | if [ ${error_status} -ne 0 ] ; then |
aoqi@0 | 223 | exit 1 |
aoqi@0 | 224 | fi |