Include third party script for license checking and amending 19/29119/2
authorYujun Zhang <zhang.yujunz@zte.com.cn>
Tue, 21 Feb 2017 08:08:42 +0000 (16:08 +0800)
committerYujun Zhang <zhang.yujunz@zte.com.cn>
Tue, 21 Feb 2017 08:10:19 +0000 (16:10 +0800)
The following commit message are generated automatically by git-subrepo
-----------------------------------------------------------------------------
git subrepo clone git@github.com:openzero-zte/License.git third-party/License

subrepo:
  subdir:   "third-party/License"
  merged:   "61489da"
upstream:
  origin:   "git@github.com:openzero-zte/License.git"
  branch:   "master"
  commit:   "61489da"
git-subrepo:
  version:  "0.3.0"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "988f8c8"
-----------------------------------------------------------------------------
Change-Id: I8eab86a8ce3f26995af3e3535f31f361b4826a8b
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
third-party/License/.gitrepo [new file with mode: 0644]
third-party/License/README.md [new file with mode: 0644]
third-party/License/add_license.sh [new file with mode: 0755]

diff --git a/third-party/License/.gitrepo b/third-party/License/.gitrepo
new file mode 100644 (file)
index 0000000..29cd5a7
--- /dev/null
@@ -0,0 +1,11 @@
+; DO NOT EDIT (unless you know what you are doing)
+;
+; This subdirectory is a git "subrepo", and this file is maintained by the
+; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
+;
+[subrepo]
+       remote = git@github.com:openzero-zte/License.git
+       branch = master
+       commit = 61489dae4453b66887d0d90a2244610a30f7e53c
+       parent = 3e443dff14a2be02b914e66f27b549d0ed4cc600
+       cmdver = 0.3.0
diff --git a/third-party/License/README.md b/third-party/License/README.md
new file mode 100644 (file)
index 0000000..0232de7
--- /dev/null
@@ -0,0 +1,10 @@
+# License
+
+A script for checking and adding license header according to [OPNFV contribution guideline](https://wiki.opnfv.org/display/DEV/Contribution+Guidelines)
+
+## Quick Start
+
+```
+$ cd <project-folder>
+$ curl https://raw.githubusercontent.com/Justin-chi/License/master/add_license.sh |bash
+```
diff --git a/third-party/License/add_license.sh b/third-party/License/add_license.sh
new file mode 100755 (executable)
index 0000000..9b383c6
--- /dev/null
@@ -0,0 +1,180 @@
+#!/bin/bash
+# Copyright justin.chigang@gmail.com
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+TMP_FILE=tmp.file
+LICENSE_TOKEN="http://www.apache.org/licenses/LICENSE-2.0"
+
+function get_first_author()
+{
+    author=$(git log --reverse --pretty=format:"%ae" $1 | head -1)
+    substring=$(git log --reverse --pretty=format:"%ae" $1 | head -1 | awk -F@ '{print $2}')
+    case $substring in
+     huawei*)
+       echo "HUAWEI TECHNOLOGIES CO.,LTD"
+          ;;
+     orange*)
+       echo "Orange"
+          ;;
+     zte*)
+       echo "ZTE Corporation"
+          ;;
+     *)
+       echo "$author"
+          ;;
+     esac
+}
+
+function get_latest_year()
+{
+    git log --pretty=format:"%ad" $1 | head -1 | awk '{print $5}'
+}
+
+function gen_c_license()
+{
+cat << EOF >$1
+/******************************************************************************* 
+ * Copyright (c) $2 $3 and others. 
+ *
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Apache License, Version 2.0 
+ * which accompanies this distribution, and is available at 
+ * http://www.apache.org/licenses/LICENSE-2.0 
+ *******************************************************************************/ 
+
+EOF
+}
+
+function gen_xml_license()
+{
+cat << EOF >$1
+<!--
+ Copyright (c) $2 $3 and others.
+
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Apache License, Version 2.0
+ which accompanies this distribution, and is available at
+ http://www.apache.org/licenses/LICENSE-2.0
+-->
+
+EOF
+}
+
+function gen_bash_license()
+{
+cat << EOF >$1
+##############################################################################
+# Copyright (c) $2 $3 and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+EOF
+}
+
+function add_c_license()
+{
+    C_LICENSE="c_license_header.tmp"
+    author=`get_first_author "$1"`
+    year=`get_latest_year "$1"`
+    gen_c_license $C_LICENSE $year "$author"
+    cat $C_LICENSE $1 > $TMP_FILE
+    rm -f $C_LICENSE
+    mv $TMP_FILE $1
+}
+
+function add_xml_license()
+{
+    XML_LICENSE="xml_license_header.tmp"
+    author=`get_first_author "$1"`
+    year=`get_latest_year "$1"`
+    gen_xml_license $XML_LICENSE $year "$author"
+    cat $XML_LICENSE $1 > $TMP_FILE
+    rm -f $XML_LICENSE
+    mv $TMP_FILE $1
+}
+
+function add_bash_license()
+{
+    BASH_LICENSE="bash_license_header.tmp"
+    author=`get_first_author "$1"`
+    year=`get_latest_year "$1"`
+    gen_bash_license $BASH_LICENSE $year "$author"
+    cat $1 | head -1 | grep "#!" > /dev/null
+    if [ $? -eq 0 ]; then
+        #insert 2
+        sed -i "1 r $BASH_LICENSE" $1
+    else
+        #sed -i "1 R $BASH_LICENSE" $1
+        cat $BASH_LICENSE $1 > $TMP_FILE
+        mv $TMP_FILE $1
+    fi
+    rm -f $BASH_LICENSE
+}
+
+if [[ -z "$1" ]] || [[ ! -d "$1" ]]; then  
+    echo "The directory is empty or not exist!"  
+    echo "It will use the current directory."  
+    nowdir=$(pwd)  
+else  
+    nowdir=$(cd $1; pwd)  
+fi  
+echo "$nowdir"  
+
+n=0
+
+function Searchfile()  
+{  
+    cd $1  
+    
+    dirlist=$(ls)  
+    for dirname in $dirlist
+    do  
+        if [[ -d "$dirname" ]];then
+            n=$((n+4))
+            cd $dirname
+            for i in $( seq 0 $n );do echo -n ' ';done;echo "$dirname ..."  
+            Searchfile $(pwd)  
+            cd ..  
+            n=$((n-4))
+        fi;
+
+        filename=$dirname
+        if [[ -f "$filename" ]]; then
+            for i in $( seq 0 $n );do echo -n ' ';done;echo " |--$filename"
+            grep -rn $LICENSE_TOKEN $filename >/dev/null
+            if [ $? -eq 1 ]; then
+                if [ "${filename##*.}" = "c" -o "${filename##*.}" = "cpp" -o "${filename##*.}" = "java" ]; then
+                    add_c_license $filename 
+                    for i in $( seq 0 $n );do echo -n ' ';done;echo " |--add license for $filename... "
+                elif [ "${filename##*.}" = "py" -o "${filename##*.}" = "yml" -o "${filename##*.}" = "yaml" -o "${filename##*.}" = "sh" ]; then
+                    add_bash_license $filename 
+                    for i in $( seq 0 $n );do echo -n ' ';done;echo " |--add license for $filename... "
+                elif [ "${filename##*.}" = "xml" ]; then
+                    add_xml_license $filename
+                    for i in $( seq 0 $n );do echo -n ' ';done;echo " |--add license for $filename... "
+                fi;
+            fi;
+        fi;
+    done;  
+}  
+  
+Searchfile $nowdir 
+
+# Revert changes of skipped files, e.g. __init__.py
+
+git checkout \*\*/__init__.py