shell脚本 给代码加上apache lisence2(al2)头

来源:互联网 发布:打车软件推广方案 编辑:程序博客网 时间:2024/04/29 22:08

给定一个代码包,将其中未加上apache lisence2(al2)头的文件,全加上 al2头。

1> 文件中已经有al2头的文件,直接退出。

查找文件内容中有行内容包括“licenses/LICENSE-2.0”“Apache Software Foundation”;行开头内容包括“^#Licensedto the Apache Software”的文件。

2>文件中没有al2头的文件。其文件的开头如果有几个空行,则将文件空行删除。使得文件的第一行为"#!/bin/bash"或者 "#!/bin/python2"


3>对符合文件的添加al2头。

   sed -i是直接修改文件,-n 可以取消屏幕的输出信息。


//add_license.sh:对一个文件操作

#!/bin/bash

set -e
set -o xtrace

file=$1

# for file has blank lines at the head, remove those blank lines.
sed -i '/./,/^$/!d' $file

# for file has a license head,exit
#if [[ ` cat $file | grep -iHn "license" | wc -l` -gt 0 ]]; then
#    exit 0
#fi

# for files already had al2 head, exit.
if [[ `cat $file | grep "licenses/LICENSE-2.0" | wc -l` -gt 0 ]]; then
    if [[ `cat $file | grep "Unless required by applicable law" | wc -l` -gt 0 ]]; then
        if [[ `cat $file | grep "Copyright" | wc -l` -gt 0 ]]; then
            exit 0
        fi
    fi

fi

# for files already have MIT Licese, exit.
if [[ ` cat $file | grep "licensed under the MIT" | wc -l` -gt 0 ]]; then
        exit 0
fi

# for files already have BSD License head,exit.
if [[ `cat $file | grep "BSD License" | wc -l` -gt 0 ]]; then
    exit 0
fi

# for files already has Horizon Apache license
if [[ `cat $file | grep "base Horizon JavaScript object" | wc -l` -gt 0 ]];then
    exit 0
fi

# for ".html" file
if [ "${file##*.}"x = "html"x ];then
     sed -i '1 i\\n<!-- Copyright 2014 Intel Corporation, All Rights Reserved.\n\n Licensed under the Apache License, Version 2.0 (the"License");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n  http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an\n "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n KIND, either express or implied. See the License for the\n specific language governing permissions and limitations\n under the License.\n --> \n' $file
    exit 0
fi

# for ".css" ".js" file
if [ "${file##*.}"x = "css"x ]||[ "${file##*.}"x = "js"x ];then

    sed -i '1 i\\n/* Copyright 2014 Intel Corporation, All Rights Reserved.\n\n Licensed under the Apache License, Version 2.0 (the"License");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n  http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an\n "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n KIND, either express or implied. See the License for the\n specific language governing permissions and limitations\n under the License.\n */\n' $file
    exit 0

fi

# for ".py" ".sh" file has the "# vim" "bin", we add al2 head behind the 1st line.

if [[ `sed -n 1p $file| grep -E "(bin|^# vim: tabstop)"| wc -l ` -gt 0 ]]; then

sed -i '1 a\\n# Copyright 2014 Intel Corporation, All Rights Reserved.\n\n# Licensed under the Apache License, Version 2.0 (the"License");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n#  http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing,\n# software distributed under the License is distributed on an\n# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n# KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations\n# under the License.\n' $file

    exit 0
fi

sed -i '1 i\\n# Copyright 2014 Intel Corporation, All Rights Reserved.\n\n# Licensed under the Apache License, Version 2.0 (the"License");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n#  http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing,\n# software distributed under the License is distributed on an\n# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n# KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations\n# under the License.\n' $file

set +o xtrace

//multi_add.sh:对多个文件批量操作

set -e
TOPDIR=$(cd $(dirname "$0")&& pwd)
cp -rf $TOPDIR/add_license.sh /usr/local/bin/

# enter the code directory
code=$1
cd $code

# for all the files except the directory file, do add_license
for n in `find . -name "*.py" -o -name "*.sh" -o -name "*.css" -o -name "*.js" -o -name "*.html"`; do
    if [[ ! -d $n ]]; then
        add_license.sh $n
    fi
done

# deal with __init__.py

for n in `find . -name "__init__.py"`; do
    cnt=`cat $n | wc -l`
    if [[ $cnt -eq 0 ]]; then
        echo "" > $n

sed -i '1 i\\n# Copyright 2014 Intel Corporation, All Rights Reserved.\n\n# Licensed under the Apache License, Version 2.0 (the"License");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n#  http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing,\n# software distributed under the License is distributed on an\n# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n# KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations\n# under the License.\n' $n
    fi
done

//处理空的__init__.py文件: init.sh

set -e
TOPDIR=$(cd $(dirname "$0")&& pwd)
cp -rf $TOPDIR/add_license.sh /usr/local/bin/

# enter the code directory
code=$1
cd $code

# deal with __init__.py

for n in `find . -name "__init__.py"`; do
    cnt=`cat $n | wc -l`
    if [[ $cnt -eq 0 ]]; then
        echo "" > $n
        add_license.sh $n
    fi
done




0 0