Path: blob/aarch64-shenandoah-jdk8u272-b10/make/scripts/lic_check.sh
32284 views
#! /bin/sh -f1#2# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4#5# This code is free software; you can redistribute it and/or modify it6# under the terms of the GNU General Public License version 2 only, as7# published by the Free Software Foundation.8#9# This code is distributed in the hope that it will be useful, but WITHOUT10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12# version 2 for more details (a copy is included in the LICENSE file that13# accompanied this code).14#15# You should have received a copy of the GNU General Public License version16# 2 along with this work; if not, write to the Free Software Foundation,17# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18#19# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20# or visit www.oracle.com if you need additional information or have any21# questions.22#2324#25# This script checks a copyright notice.26#27# The script should be located in the main jdk repository under make/scripts.28# It works with the templates in the make/templates directory of the jdk source.29#30# Usage: "lic_check.sh [-gpl] or [-gplcp] or [-bsd] file(s)"3132script_directory=`dirname $0`33script_name=`basename $0`34first_option=$13536# parse the first argument3738case "$1" in39"-gpl")40header="gpl-header"41;;42"-gplcp")43header="gpl-cp-header"44;;45"-bsd")46header="bsd-header"47;;48*)49echo "Usage: $0 [-gpl] or [-gplcp] or [-bsd] file(s)" 1>&250exit 151;;52esac53shift5455#initialize error status56error_status=05758# determine and set the absolute path for the script directory59D=`dirname "${script_directory}"`60B=`basename "${script_directory}"`61script_dir="`cd \"${D}\" 2>/dev/null && pwd || echo \"${D}\"`/${B}"6263# set up a variable for the templates directory64template_dir=${script_dir}/../templates6566# Check existence of the template directory.67if [ ! -d ${template_dir} ] ; then68echo "ERROR: The templates directory "${template_dir}" doesn't exist." 1>&269exit 170fi7172# set the temporary file location73tmpfile=/tmp/source_file.$$74rm -f ${tmpfile}7576# check number of lines in the template file77lines=`cat ${template_dir}/${header} | wc -l`7879# the template file has one empty line at the end, we need to ignore it80lines=`expr ${lines} - 1`8182# A loop through the all script parameters:83#84# 1. Given a set of source files and a license template header, read a file name of each source file.85# 2. Check if a given file exists. When a directory is encountered, dive in and process all sources in those directories.86# 3. Read each line of the given file and check it for a copyright string.87# 4. If a copyright string found, check the correctness of the years format in the string and replace years with %YEARS%.88# 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.89# 6. Store the result (the license header from a given file) into a temporary file.90# 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.91# 8. Produce a error in case a temporary file is empty, it means we didn't find a copyright string, or it's not correct92#93while [ "$#" -gt "0" ] ; do94touch ${tmpfile}9596# In case of the directory as a parameter check recursively every file inside.97if [ -d $1 ] ; then98curdir=`pwd`99cd $1100echo "*** Entering directory: "`pwd`101echo "***"102files=`ls .`103sh ${script_dir}/${script_name} ${first_option} ${files}104status=$?105if [ ${error_status} -ne 1 ] ; then106error_status=${status}107fi108cd ${curdir}109shift110continue111else112echo "### Checking copyright notice in the file: "$1113echo "###"114fi115116# Check the existence of the source file.117if [ ! -f $1 ] ; then118echo "ERROR: The source file "$1" doesn't exist." 1>&2119error_status=1120shift121continue122fi123124# read the source file and determine where the header starts, then get license header without prefix125counter=0126while read line ; do127# remove windows "line feed" character from the line (if any)128line=`echo "${line}" | tr -d '\r'`129# check if the given line contains copyright130check_copyright=`echo "${line}" | grep "Copyright (c) "`131if [ "${check_copyright}" != "" ] ; then132# determine the comment prefix133prefix=`echo "${line}" | cut -d "C" -f 1`134# remove prefix (we use "_" as a sed delimiter, since the prefix could be like //)135copyright_without_prefix=`echo "${line}" | sed s_"^${prefix}"__g`136# copyright years137year1=`echo "${copyright_without_prefix}" | cut -d " " -f 3`138year2=`echo "${copyright_without_prefix}" | cut -d " " -f 4`139# Processing the first year in the copyright string140length=`expr "${year1}" : '.*'`141if [ ${length} -ne 5 ] ; then142break143fi144check_year1=`echo ${year1} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"`145if [ "${check_year1}" = "" ] ; then146break147fi148# Processing the second year in the copyright string149if [ "${year2}" != "Oracle" ] ; then150length=`expr "${year2}" : '.*'`151if [ ${length} -ne 5 ] ; then152break153else154check_year2=`echo ${year2} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"`155if [ "${check_year2}" = "" ] ; then156break157fi158fi159fi160161# copyright string without copyright years162no_years=`echo "${copyright_without_prefix}" | sed 's/[0-9,]*//g'`163# copyright string before years164before_years=`echo "${no_years}" | cut -d "O" -f 1`165# copyright string after years166after_years=`echo "${no_years}" | cut -d ")" -f 2`167# form a new copyright string with %YEARS%168new_copyright=`echo ${before_years}"%YEARS%"${after_years}`169# save the new copyright string to a file170echo "${new_copyright}" > ${tmpfile}171# start counting the lines172counter=1173# move to the next line174continue175fi176if [ ${counter} -ne 0 ] ; then177# this should be a license header line, hence increment counter178counter=`expr ${counter} + 1`179# record a string without a prefix to a file180newline=`echo "${line}" | sed s_"^${prefix}"__`181182# we need to take care of the empty lines in the header, i.e. check the prefix without spaces183trimmed_prefix=`echo "${prefix}" | tr -d " "`184trimmed_line=`echo "${line}" | tr -d " "`185if [ "${trimmed_line}" = "${trimmed_prefix}" ] ; then186echo "" >> ${tmpfile}187else188echo "${newline}" >> ${tmpfile}189fi190fi191# stop reading lines when a license header ends and add an empty line to the end192if [ ${counter} -eq ${lines} ] ; then193echo "" >> ${tmpfile}194break195fi196done < $1197198# compare the license header with a template file199if [ -s ${tmpfile} ] ; then200diff -c ${tmpfile} ${template_dir}/${header} 1>&2201if [ "$?" = "0" ] ; then202echo "SUCCESS: The license header for "`pwd`"/"$1" has been verified."203echo "###"204else205echo "ERROR: License header is not correct in "`pwd`"/"$1 1>&2206echo "See diffs above. " 1>&2207echo "###" 1>&2208echo "" 1>&2209error_status=1210fi211else212# If we don't have a temporary file, there is a problem with a copyright string (or no copyright string)213echo "ERROR: Copyright string is not correct or missing in "`pwd`"/"$1 1>&2214echo "###" 1>&2215echo "" 1>&2216error_status=1217fi218rm -f ${tmpfile}219shift220done221if [ ${error_status} -ne 0 ] ; then222exit 1223fi224225226