aoqi@0: #! /bin/sh -f aoqi@0: # aoqi@0: # Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: # aoqi@0: # This code is free software; you can redistribute it and/or modify it aoqi@0: # under the terms of the GNU General Public License version 2 only, as aoqi@0: # published by the Free Software Foundation. aoqi@0: # aoqi@0: # This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: # version 2 for more details (a copy is included in the LICENSE file that aoqi@0: # accompanied this code). aoqi@0: # aoqi@0: # You should have received a copy of the GNU General Public License version aoqi@0: # 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: # aoqi@0: # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: # or visit www.oracle.com if you need additional information or have any aoqi@0: # questions. aoqi@0: # aoqi@0: aoqi@0: # aoqi@0: # This script checks a copyright notice. aoqi@0: # aoqi@0: # The script should be located in the main jdk repository under make/scripts. aoqi@0: # It works with the templates in the make/templates directory of the jdk source. aoqi@0: # aoqi@0: # Usage: "lic_check.sh [-gpl] or [-gplcp] or [-bsd] file(s)" aoqi@0: aoqi@0: script_directory=`dirname $0` aoqi@0: script_name=`basename $0` aoqi@0: first_option=$1 aoqi@0: aoqi@0: # parse the first argument aoqi@0: aoqi@0: case "$1" in aoqi@0: "-gpl") aoqi@0: header="gpl-header" aoqi@0: ;; aoqi@0: "-gplcp") aoqi@0: header="gpl-cp-header" aoqi@0: ;; aoqi@0: "-bsd") aoqi@0: header="bsd-header" aoqi@0: ;; aoqi@0: *) aoqi@0: echo "Usage: $0 [-gpl] or [-gplcp] or [-bsd] file(s)" 1>&2 aoqi@0: exit 1 aoqi@0: ;; aoqi@0: esac aoqi@0: shift aoqi@0: aoqi@0: #initialize error status aoqi@0: error_status=0 aoqi@0: aoqi@0: # determine and set the absolute path for the script directory aoqi@0: D=`dirname "${script_directory}"` aoqi@0: B=`basename "${script_directory}"` aoqi@0: script_dir="`cd \"${D}\" 2>/dev/null && pwd || echo \"${D}\"`/${B}" aoqi@0: aoqi@0: # set up a variable for the templates directory aoqi@0: template_dir=${script_dir}/../templates aoqi@0: aoqi@0: # Check existence of the template directory. aoqi@0: if [ ! -d ${template_dir} ] ; then aoqi@0: echo "ERROR: The templates directory "${template_dir}" doesn't exist." 1>&2 aoqi@0: exit 1 aoqi@0: fi aoqi@0: aoqi@0: # set the temporary file location aoqi@0: tmpfile=/tmp/source_file.$$ aoqi@0: rm -f ${tmpfile} aoqi@0: aoqi@0: # check number of lines in the template file aoqi@0: lines=`cat ${template_dir}/${header} | wc -l` aoqi@0: aoqi@0: # the template file has one empty line at the end, we need to ignore it aoqi@0: lines=`expr ${lines} - 1` aoqi@0: aoqi@0: # A loop through the all script parameters: aoqi@0: # aoqi@0: # 1. Given a set of source files and a license template header, read a file name of each source file. aoqi@0: # 2. Check if a given file exists. When a directory is encountered, dive in and process all sources in those directories. aoqi@0: # 3. Read each line of the given file and check it for a copyright string. aoqi@0: # 4. If a copyright string found, check the correctness of the years format in the string and replace years with %YEARS%. aoqi@0: # 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: # 6. Store the result (the license header from a given file) into a temporary file. aoqi@0: # 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: # 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: # aoqi@0: while [ "$#" -gt "0" ] ; do aoqi@0: touch ${tmpfile} aoqi@0: aoqi@0: # In case of the directory as a parameter check recursively every file inside. aoqi@0: if [ -d $1 ] ; then aoqi@0: curdir=`pwd` aoqi@0: cd $1 aoqi@0: echo "*** Entering directory: "`pwd` aoqi@0: echo "***" aoqi@0: files=`ls .` aoqi@0: sh ${script_dir}/${script_name} ${first_option} ${files} aoqi@0: status=$? aoqi@0: if [ ${error_status} -ne 1 ] ; then aoqi@0: error_status=${status} aoqi@0: fi aoqi@0: cd ${curdir} aoqi@0: shift aoqi@0: continue aoqi@0: else aoqi@0: echo "### Checking copyright notice in the file: "$1 aoqi@0: echo "###" aoqi@0: fi aoqi@0: aoqi@0: # Check the existence of the source file. aoqi@0: if [ ! -f $1 ] ; then aoqi@0: echo "ERROR: The source file "$1" doesn't exist." 1>&2 aoqi@0: error_status=1 aoqi@0: shift aoqi@0: continue aoqi@0: fi aoqi@0: aoqi@0: # read the source file and determine where the header starts, then get license header without prefix aoqi@0: counter=0 aoqi@0: while read line ; do aoqi@0: # remove windows "line feed" character from the line (if any) aoqi@0: line=`echo "${line}" | tr -d '\r'` aoqi@0: # check if the given line contains copyright aoqi@0: check_copyright=`echo "${line}" | grep "Copyright (c) "` aoqi@0: if [ "${check_copyright}" != "" ] ; then aoqi@0: # determine the comment prefix aoqi@0: prefix=`echo "${line}" | cut -d "C" -f 1` aoqi@0: # remove prefix (we use "_" as a sed delimiter, since the prefix could be like //) aoqi@0: copyright_without_prefix=`echo "${line}" | sed s_"^${prefix}"__g` aoqi@0: # copyright years aoqi@0: year1=`echo "${copyright_without_prefix}" | cut -d " " -f 3` aoqi@0: year2=`echo "${copyright_without_prefix}" | cut -d " " -f 4` aoqi@0: # Processing the first year in the copyright string aoqi@0: length=`expr "${year1}" : '.*'` aoqi@0: if [ ${length} -ne 5 ] ; then aoqi@0: break aoqi@0: fi aoqi@0: check_year1=`echo ${year1} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"` aoqi@0: if [ "${check_year1}" = "" ] ; then aoqi@0: break aoqi@0: fi aoqi@0: # Processing the second year in the copyright string aoqi@0: if [ "${year2}" != "Oracle" ] ; then aoqi@0: length=`expr "${year2}" : '.*'` aoqi@0: if [ ${length} -ne 5 ] ; then aoqi@0: break aoqi@0: else aoqi@0: check_year2=`echo ${year2} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"` aoqi@0: if [ "${check_year2}" = "" ] ; then aoqi@0: break aoqi@0: fi aoqi@0: fi aoqi@0: fi aoqi@0: aoqi@0: # copyright string without copyright years aoqi@0: no_years=`echo "${copyright_without_prefix}" | sed 's/[0-9,]*//g'` aoqi@0: # copyright string before years aoqi@0: before_years=`echo "${no_years}" | cut -d "O" -f 1` aoqi@0: # copyright string after years aoqi@0: after_years=`echo "${no_years}" | cut -d ")" -f 2` aoqi@0: # form a new copyright string with %YEARS% aoqi@0: new_copyright=`echo ${before_years}"%YEARS%"${after_years}` aoqi@0: # save the new copyright string to a file aoqi@0: echo "${new_copyright}" > ${tmpfile} aoqi@0: # start counting the lines aoqi@0: counter=1 aoqi@0: # move to the next line aoqi@0: continue aoqi@0: fi aoqi@0: if [ ${counter} -ne 0 ] ; then aoqi@0: # this should be a license header line, hence increment counter aoqi@0: counter=`expr ${counter} + 1` aoqi@0: # record a string without a prefix to a file aoqi@0: newline=`echo "${line}" | sed s_"^${prefix}"__` aoqi@0: aoqi@0: # we need to take care of the empty lines in the header, i.e. check the prefix without spaces aoqi@0: trimmed_prefix=`echo "${prefix}" | tr -d " "` aoqi@0: trimmed_line=`echo "${line}" | tr -d " "` aoqi@0: if [ "${trimmed_line}" = "${trimmed_prefix}" ] ; then aoqi@0: echo "" >> ${tmpfile} aoqi@0: else aoqi@0: echo "${newline}" >> ${tmpfile} aoqi@0: fi aoqi@0: fi aoqi@0: # stop reading lines when a license header ends and add an empty line to the end aoqi@0: if [ ${counter} -eq ${lines} ] ; then aoqi@0: echo "" >> ${tmpfile} aoqi@0: break aoqi@0: fi aoqi@0: done < $1 aoqi@0: aoqi@0: # compare the license header with a template file aoqi@0: if [ -s ${tmpfile} ] ; then aoqi@0: diff -c ${tmpfile} ${template_dir}/${header} 1>&2 aoqi@0: if [ "$?" = "0" ] ; then aoqi@0: echo "SUCCESS: The license header for "`pwd`"/"$1" has been verified." aoqi@0: echo "###" aoqi@0: else aoqi@0: echo "ERROR: License header is not correct in "`pwd`"/"$1 1>&2 aoqi@0: echo "See diffs above. " 1>&2 aoqi@0: echo "###" 1>&2 aoqi@0: echo "" 1>&2 aoqi@0: error_status=1 aoqi@0: fi aoqi@0: else aoqi@0: # If we don't have a temporary file, there is a problem with a copyright string (or no copyright string) aoqi@0: echo "ERROR: Copyright string is not correct or missing in "`pwd`"/"$1 1>&2 aoqi@0: echo "###" 1>&2 aoqi@0: echo "" 1>&2 aoqi@0: error_status=1 aoqi@0: fi aoqi@0: rm -f ${tmpfile} aoqi@0: shift aoqi@0: done aoqi@0: if [ ${error_status} -ne 0 ] ; then aoqi@0: exit 1 aoqi@0: fi