forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_license.sh
executable file
·98 lines (87 loc) · 2.5 KB
/
check_license.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
PROJECT_ROOT=$(git rev-parse --show-toplevel)
LICENSE_LOCATION="$PROJECT_ROOT"/scripts/LICENSE_HEADER
NUMLINES=$(< "$LICENSE_LOCATION" wc -l | tr -d ' ')
LICENSE=$(sed "s/{DATE_Y}/$(date +"%Y")/" "$LICENSE_LOCATION")
VERSIONED_GO_FILES=$(git ls-tree --full-tree --name-only -r HEAD | grep "\.go$")
EXCLUDE=(
"Code generated by"
"David Lazar"
"Go Authors"
"Google Inc"
"Prometheus Authors"
"Jeffrey Wilcke"
"dummy.go"
)
FILTER=$(IFS="|" ; echo "${EXCLUDE[*]}")
INPLACE=false
VERBOSE=false
MOD_COUNT=0
RETURN_VALUE=0
usage() {
echo "check_license"
echo
echo "Usage: $0 [args]"
echo
echo "Args:"
echo "-i Edit in-place."
echo "-v Verbose, same as doing \`head -n ${NUMLINES:-15}\` on each file."
echo
}
RED_FG=$(tput setaf 1 2>/dev/null)
END_FG_COLOR=$(tput sgr0 2>/dev/null)
while [ "$1" != "" ]; do
case "$1" in
-i)
INPLACE=true
;;
-v) VERBOSE=true
;;
-h)
usage
exit 0
;;
*)
echo "${RED_FG}[ERROR]${END_FG_COLOR} Unknown option $1"
usage
exit 1
;;
esac
shift
done
for FILE in $VERSIONED_GO_FILES; do
# https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat
if [[ $LICENSE != $(<"$PROJECT_ROOT/$FILE" head -n "$NUMLINES") ]]; then
if <"$PROJECT_ROOT/$FILE" head -n "$NUMLINES" | tr "\n" " " | grep -qvE "$FILTER"; then
RETURN_VALUE=1
if ! $VERBOSE; then
if $INPLACE; then
cat <(echo -e "$LICENSE\n") "$PROJECT_ROOT/$FILE" > "$PROJECT_ROOT/$FILE".1 &&
mv "$PROJECT_ROOT/$FILE"{.1,}
((MOD_COUNT++))
fi
echo "$FILE"
else
echo -e "\n${RED_FG}$FILE${END_FG_COLOR}"
<"$PROJECT_ROOT/$FILE" head -n "$NUMLINES"
echo
fi
fi
fi
done
# check the README.md file.
READMECOPYRIGHT="Copyright (C) 2019-$(date +"%Y"), Algorand Inc."
if [ "$(<README.md grep -c "${READMECOPYRIGHT}" | tr -d ' ')" = "0" ]; then
RETURN_VALUE=1
echo "README.md file need to have its license date range updated."
fi
if [ $RETURN_VALUE -ne 0 ]; then
echo -e "\n${RED_FG}FAILED LICENSE CHECK.${END_FG_COLOR}"
if [ $INPLACE == "false" ]; then
echo -e "Use 'check_license.sh -i' to fix."
else
echo "Modified $MOD_COUNT file(s)."
fi
echo ""
fi
exit $RETURN_VALUE