Last active
February 18, 2017 16:59
-
-
Save yujunz/8db9f4eb53107b99084822022f582038 to your computer and use it in GitHub Desktop.
add license header for opnfv project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://github.com/Justin-chi/Lab/blob/master/add_license.sh | |
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" | |
;; | |
*) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment