make/scripts/lic_check.sh

changeset 0
75a576e87639
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/make/scripts/lic_check.sh	Wed Apr 27 01:39:08 2016 +0800
     1.3 @@ -0,0 +1,224 @@
     1.4 +#! /bin/sh -f
     1.5 +#
     1.6 +# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.7 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8 +#
     1.9 +# This code is free software; you can redistribute it and/or modify it
    1.10 +# under the terms of the GNU General Public License version 2 only, as
    1.11 +# published by the Free Software Foundation.
    1.12 +#
    1.13 +# This code is distributed in the hope that it will be useful, but WITHOUT
    1.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.15 +# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.16 +# version 2 for more details (a copy is included in the LICENSE file that
    1.17 +# accompanied this code).
    1.18 +#
    1.19 +# You should have received a copy of the GNU General Public License version
    1.20 +# 2 along with this work; if not, write to the Free Software Foundation,
    1.21 +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 +#
    1.23 +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.24 +# or visit www.oracle.com if you need additional information or have any
    1.25 +# questions.
    1.26 +#
    1.27 +
    1.28 +#
    1.29 +# This script checks a copyright notice.
    1.30 +#
    1.31 +# The script should be located in the main jdk repository under make/scripts.
    1.32 +# It works with the templates in the make/templates directory of the jdk source.
    1.33 +#
    1.34 +# Usage: "lic_check.sh [-gpl] or [-gplcp] or [-bsd] file(s)"
    1.35 +
    1.36 +script_directory=`dirname $0`
    1.37 +script_name=`basename $0`
    1.38 +first_option=$1
    1.39 +
    1.40 +# parse the first argument
    1.41 +
    1.42 +case "$1" in
    1.43 +	"-gpl")
    1.44 +		header="gpl-header"
    1.45 +		;;
    1.46 +	"-gplcp")
    1.47 +		header="gpl-cp-header"
    1.48 +		;;
    1.49 +	"-bsd")
    1.50 +		header="bsd-header"
    1.51 +                ;;
    1.52 +	*)
    1.53 +		echo "Usage: $0 [-gpl] or [-gplcp] or [-bsd] file(s)" 1>&2
    1.54 +		exit 1
    1.55 +		;;
    1.56 +esac
    1.57 +shift
    1.58 +
    1.59 +#initialize error status
    1.60 +error_status=0
    1.61 +
    1.62 +# determine and set the absolute path for the script directory
    1.63 +D=`dirname "${script_directory}"`
    1.64 +B=`basename "${script_directory}"`
    1.65 +script_dir="`cd \"${D}\" 2>/dev/null && pwd || echo \"${D}\"`/${B}"
    1.66 +
    1.67 +# set up a variable for the templates directory
    1.68 +template_dir=${script_dir}/../templates
    1.69 +
    1.70 +# Check existence of the template directory.
    1.71 +if [ ! -d ${template_dir} ] ; then
    1.72 +        echo "ERROR: The templates directory "${template_dir}" doesn't exist." 1>&2
    1.73 +        exit 1
    1.74 +fi
    1.75 +
    1.76 +# set the temporary file location
    1.77 +tmpfile=/tmp/source_file.$$
    1.78 +rm -f ${tmpfile}
    1.79 +
    1.80 +# check number of lines in the template file
    1.81 +lines=`cat ${template_dir}/${header} | wc -l`
    1.82 +
    1.83 +# the template file has one empty line at the end, we need to ignore it
    1.84 +lines=`expr ${lines} - 1`
    1.85 +
    1.86 +# A loop through the all script parameters:
    1.87 +#
    1.88 +# 1. Given a set of source files and a license template header, read a file name of each source file.
    1.89 +# 2. Check if a given file exists. When a directory is encountered, dive in and process all sources in those directories.
    1.90 +# 3. Read each line of the given file and check it for a copyright string.
    1.91 +# 4. If a copyright string found, check the correctness of the years format in the string and replace years with %YEARS%.
    1.92 +# 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.
    1.93 +# 6. Store the result (the license header from a given file) into a temporary file.
    1.94 +# 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.
    1.95 +# 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
    1.96 +#
    1.97 +while [ "$#" -gt "0" ] ; do
    1.98 +	touch ${tmpfile}
    1.99 +
   1.100 +	# In case of the directory as a parameter check recursively every file inside.
   1.101 +	if [ -d $1 ] ; then
   1.102 +		curdir=`pwd`
   1.103 +		cd $1
   1.104 +		echo "*** Entering directory: "`pwd`
   1.105 +		echo "***"
   1.106 +		files=`ls .`
   1.107 +		sh ${script_dir}/${script_name} ${first_option} ${files}
   1.108 +		status=$?
   1.109 +		if [ ${error_status} -ne 1 ] ; then
   1.110 +			error_status=${status}
   1.111 +		fi
   1.112 +		cd ${curdir}
   1.113 +		shift
   1.114 +		continue
   1.115 +	else
   1.116 +		echo "### Checking copyright notice in the file: "$1
   1.117 +		echo "###"
   1.118 +	fi
   1.119 +
   1.120 +	# Check the existence of the source file.
   1.121 +	if [ ! -f $1 ] ; then
   1.122 +        	echo "ERROR: The source file "$1" doesn't exist." 1>&2
   1.123 +		error_status=1
   1.124 +		shift
   1.125 +        	continue
   1.126 +	fi
   1.127 +
   1.128 +	# read the source file and determine where the header starts, then get license header without prefix
   1.129 +	counter=0
   1.130 +	while read line ; do
   1.131 +		# remove windows "line feed" character from the line (if any)
   1.132 +		line=`echo "${line}" | tr -d '\r'`
   1.133 +		# check if the given line contains copyright
   1.134 +		check_copyright=`echo "${line}" | grep "Copyright (c) "`
   1.135 +		if [ "${check_copyright}" != "" ] ; then
   1.136 +			# determine the comment prefix
   1.137 +			prefix=`echo "${line}" | cut -d "C" -f 1`
   1.138 +			# remove prefix (we use "_" as a sed delimiter, since the prefix could be like //)
   1.139 +			copyright_without_prefix=`echo "${line}" | sed s_"^${prefix}"__g`
   1.140 +			# copyright years
   1.141 +			year1=`echo "${copyright_without_prefix}" | cut -d " " -f 3`
   1.142 +			year2=`echo "${copyright_without_prefix}" | cut -d " " -f 4`
   1.143 +			# Processing the first year in the copyright string
   1.144 +			length=`expr "${year1}" : '.*'`
   1.145 +			if [ ${length} -ne 5 ] ; then
   1.146 +        			break
   1.147 +			fi
   1.148 +			check_year1=`echo ${year1} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"`
   1.149 +			if [ "${check_year1}" = "" ] ; then
   1.150 +        			break
   1.151 +			fi
   1.152 +			# Processing the second year in the copyright string
   1.153 +			if [ "${year2}" != "Oracle" ] ; then
   1.154 +        			length=`expr "${year2}" : '.*'`
   1.155 +        			if [ ${length} -ne 5 ] ; then
   1.156 +                			break
   1.157 +        			else
   1.158 +                			check_year2=`echo ${year2} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"`
   1.159 +                			if [ "${check_year2}" = "" ] ; then
   1.160 +                        			break
   1.161 +                			fi
   1.162 +        			fi
   1.163 +			fi
   1.164 +
   1.165 +			# copyright string without copyright years
   1.166 +			no_years=`echo "${copyright_without_prefix}" | sed 's/[0-9,]*//g'`
   1.167 +			# copyright string before years
   1.168 +			before_years=`echo "${no_years}" | cut -d "O" -f 1`
   1.169 +			# copyright string after years
   1.170 +			after_years=`echo "${no_years}" | cut -d ")" -f 2`
   1.171 +			# form a new copyright string with %YEARS%
   1.172 +			new_copyright=`echo ${before_years}"%YEARS%"${after_years}`
   1.173 +			# save the new copyright string to a file
   1.174 +			echo "${new_copyright}" > ${tmpfile}
   1.175 +			# start counting the lines
   1.176 +                       	counter=1
   1.177 +			# move to the next line
   1.178 +			continue
   1.179 +		fi
   1.180 +		if [ ${counter} -ne 0 ] ; then
   1.181 +			# this should be a license header line, hence increment counter
   1.182 +			counter=`expr ${counter} + 1`
   1.183 +			# record a string without a prefix to a file
   1.184 +			newline=`echo "${line}" | sed s_"^${prefix}"__`
   1.185 +
   1.186 +			# we need to take care of the empty lines in the header, i.e. check the prefix without spaces
   1.187 +			trimmed_prefix=`echo "${prefix}" | tr -d " "`
   1.188 +			trimmed_line=`echo "${line}"  | tr -d " "`
   1.189 +			if [ "${trimmed_line}" = "${trimmed_prefix}" ] ; then
   1.190 +				echo "" >> ${tmpfile}
   1.191 +			else
   1.192 +				echo "${newline}" >> ${tmpfile}
   1.193 +			fi
   1.194 +		fi
   1.195 +		# stop reading lines when a license header ends and add an empty line to the end
   1.196 +		if [ ${counter} -eq ${lines} ] ; then
   1.197 +			echo "" >> ${tmpfile}
   1.198 +			break
   1.199 +		fi
   1.200 +	done < $1
   1.201 +
   1.202 +	# compare the license header with a template file
   1.203 +	if [ -s ${tmpfile} ] ; then
   1.204 +		diff -c ${tmpfile} ${template_dir}/${header} 1>&2
   1.205 +		if [ "$?" = "0" ] ; then
   1.206 +			echo "SUCCESS: The license header for "`pwd`"/"$1" has been verified."
   1.207 +			echo "###"
   1.208 +		else
   1.209 +			echo "ERROR: License header is not correct in "`pwd`"/"$1 1>&2
   1.210 +			echo "See diffs above. " 1>&2
   1.211 +			echo "###" 1>&2
   1.212 +			echo "" 1>&2
   1.213 +			error_status=1
   1.214 +		fi
   1.215 +	else
   1.216 +		# If we don't have a temporary file, there is a problem with a copyright string (or no copyright string)
   1.217 +		echo "ERROR: Copyright string is not correct or missing in "`pwd`"/"$1 1>&2
   1.218 +		echo "###" 1>&2
   1.219 +		echo "" 1>&2
   1.220 +		error_status=1
   1.221 +	fi
   1.222 +	rm -f ${tmpfile}
   1.223 +	shift
   1.224 +done
   1.225 +if [ ${error_status} -ne 0 ] ; then
   1.226 +	exit 1
   1.227 +fi

mercurial