Please review a new script for JDK8: JDK source code legal headers conformance verification (original) (raw)

Misha Bykov misha.bykov at oracle.com
Tue Mar 6 00:20:54 UTC 2012


I need a reviewer for a new attached script for JDK8 infrastructure.

SUMMARY

Most of JDK source files are required to contain a legal approved license header. The content and the format of the header is pre-defined by legal and any alteration is not permitted.

At the moment JDK8 contains three templates for source license headers: gpl, gpl-cp and bsd. The templates are located in $ROOT/make/templates directory:

$ ls $ROOT/make/templates bsd-header gpl-cp-header gpl-header $

It's recommended for a developer, when he introduced or modified source code files, to check whether a license header is precisely correct for every file he worked on.

The new script allows a JDK developer to check if the specified license header for a given file (or a set of files) matches the right header template.

NAME

lic_check.sh - JDK source code legal headers conformance verification

SYNOPSIS

lic_check.sh [-gpl] or [-gplcp] or [-bsd] file(s)

DESCRIPTION

The script for OpenJDK distribution to verify legal notices in a particular source file or a set of files.

The script must be located in the directory:

$ROOT/make/scripts

It uses templates from:

$ROOT/make/templates

The successful output example:

No differences encountered SUCCESS: The license header for filename.java has been verified.

The unsuccessful output example if Oracle copyright string is missing or copyright years are not correct :

ERROR: Copyright string is not correct or missing in filename.java.

If the copyright string is correct, but the license header is not correct, the script should produce the "diff" output between a template in $ROOT/make/templates and the license header from the given file.

HOW TO TEST

  1. Place the script into $ROOT/make/scripts in JDK8 source repository (otherwise it will not find $ROOT/make/templates and produce an error).
  2. cd to the directory with the files to check license headers
  3. $ROOT/make/scripts/lic_check.sh [-gpl] or [-gplcp] or [-bsd] filename(s)

Any questions or comments about bugs in the script, improvement suggestions, script style, etc. would be appreciated.

Thanks, Misha -------------- next part -------------- #! /bin/sh -f #

Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.

DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

This code is free software; you can redistribute it and/or modify it

under the terms of the GNU General Public License version 2 only, as

published by the Free Software Foundation.

This code is distributed in the hope that it will be useful, but WITHOUT

ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or

FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License

version 2 for more details (a copy is included in the LICENSE file that

accompanied this code).

You should have received a copy of the GNU General Public License version

2 along with this work; if not, write to the Free Software Foundation,

Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA

or visit www.oracle.com if you need additional information or have any

questions.

This script checks a copyright notice.

The script should be located in the main jdk repository under make/scripts.

It works with the templates in the make/templates directory of the jdk source.

Usage: "lic_check.sh [-gpl] or [-gplcp] or [-bsd] file(s)"

script_directory=dirname $0

parse the first argument

case "$1" in "-gpl") header="gpl-header" ;; "-gplcp") header="gpl-cp-header" ;; "-bsd") header="bsd-header" ;; *) echo "Usage: $0 [-gpl] or [-gplcp] or [-bsd] file(s)" 1>&2 exit 1 ;; esac shift

determine and set the absolute path for the script directory

D=dirname "${script_directory}" B=basename "${script_directory}" script_dir="cd \"${D}\" 2>/dev/null && pwd || echo \"${D}\"/${B}"

set up a variable for the templates directory

template_dir=${script_dir}/../templates

Check existence of the template directory.

if [ ! -d ${template_dir} ] ; then echo "ERROR: The templates directory "${template_dir}" doesn't exist." 1>&2 exit 1 fi

set the temporary file location

tmpfile=/tmp/source_file.$$ rm -f ${tmpfile}

check number of lines in the template file

lines=cat <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>t</mi><mi>e</mi><mi>m</mi><mi>p</mi><mi>l</mi><mi>a</mi><mi>t</mi><msub><mi>e</mi><mi>d</mi></msub><mi>i</mi><mi>r</mi></mrow><mi mathvariant="normal">/</mi></mrow><annotation encoding="application/x-tex">{template_dir}/</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span></span><span class="mord">/</span></span></span></span>{header} | wc -l

the template file has one empty line at the end, we need to ignore it

lines=expr ${lines} - 1

A loop throuhgh the all script parameters

while [ "$#" -gt "0" ] ; do

echo "### Checking copyright notice in the file: "$1 echo "###" touch ${tmpfile}

Check existence of the source file.

if [ -d $1 ] ; then
    echo ERROR: $1" is a directory." 1>&2
    shift
    continue
fi
if [ ! -f $1 ] ; then
        echo "ERROR: The source file "$1" doesn't exist." 1>&2
    shift
        continue
fi

read the source file and determine where the header starts, then get license header without prefix

counter=0
while read line ; do

remove windows "line feed" character from the line (if any)

    line=`echo "${line}" | tr -d '\r'`

check if the given line contains copyright

    check_copyright=`echo "${line}" | grep "Copyright (c) "`
    if [ "${check_copyright}" != "" ] ; then

determine the comment prefix

        prefix=`echo "${line}" | cut -d "C" -f 1`

remove prefix (we use "_" as a sed delimiter, since the prefix could be like //)

        copyright_without_prefix=`echo "${line}" | sed s_"^${prefix}"__`

copyright years

        year1=`echo "${copyright_without_prefix}" | cut -d " " -f 3`
        year2=`echo "${copyright_without_prefix}" | cut -d " " -f 4`

Processing the first year in the copyright string

        length=`expr "${year1}" : '.*'`
        if [ ${length} -ne 5 ] ; then
                break
        fi
        check_year1=`echo ${year1} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"`
        if [ "${check_year1}" = "" ] ; then
                break
        fi

Processing the second year in the copyright string

        if [ "${year2}" != "Oracle" ] ; then
                length=`expr "${year2}" : '.*'`
                if [ ${length} -ne 5 ] ; then
                        break
                else
                        check_year2=`echo ${year2} | egrep "19[0-9][0-9],|2[0-9][0-9][0-9],"`
                        if [ "${check_year2}" = "" ] ; then
                                break
                        fi
                fi
        fi

copyright string without copyright years

        no_years=`echo "${copyright_without_prefix}" | sed 's/[0-9,]*//g'`

copyright string before years

        before_years=`echo "${no_years}" | cut -d "O" -f 1`

copyright string after years

        after_years=`echo "${no_years}" | cut -d ")" -f 2`

form a new copyright string with %YEARS%

        new_copyright=`echo <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>b</mi><mi>e</mi><mi>f</mi><mi>o</mi><mi>r</mi><msub><mi>e</mi><mi>y</mi></msub><mi>e</mi><mi>a</mi><mi>r</mi><mi>s</mi></mrow><mi mathvariant="normal">&quot;</mi></mrow><annotation encoding="application/x-tex">{before_years}&quot;%YEARS%&quot;</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9805em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">b</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">y</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">e</span><span class="mord mathnormal">a</span><span class="mord mathnormal">rs</span></span><span class="mord">&quot;</span></span></span></span>{after_years}`

save the new copyright string to a file

        echo "${new_copyright}" > ${tmpfile}

start counting the lines

                       counter=1

move to the next line

        continue
    fi
    if [ ${counter} -ne 0 ] ; then

this should be a license header line, hence increment counter

        counter=`expr ${counter} + 1`

record a string without a prefix to a file

        newline=`echo "${line}" | sed s_"^${prefix}"__`

we need to take care of the empty lines in the header, i.e. check the prefix without spaces

        trimmed_prefix=`echo "${prefix}" | tr -d " "`
        if [ "${line}" = "${trimmed_prefix}" ] ; then
            echo "" >> ${tmpfile}
        else
            echo "${newline}" >> ${tmpfile}
        fi
    fi

stop reading lines when a license header ends and add an empty line to the end

    if [ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>c</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi></mrow><mo>−</mo><mi>e</mi><mi>q</mi></mrow><annotation encoding="application/x-tex">{counter} -eq </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6984em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">co</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.03588em;">q</span></span></span></span>{lines} ] ; then
        echo "" >> ${tmpfile}
        break
    fi
done < $1

compare the license header with a template file

if [ -s ${tmpfile} ] ; then
    diff -c <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>m</mi><mi>p</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">{tmpfile} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span></span></span></span></span>{template_dir}/${header}
    if [ "$?" = "0" ] ; then
        echo "SUCCESS: The license header for "$1" has been verified."
        echo "###"
    fi
else
    echo "ERROR: Copyright string is not correct or missing in "$1"." 1>&2
    echo "###"
fi
rm -f ${tmpfile}
shift

done



More information about the build-dev mailing list