forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyright-format.sh
executable file
·138 lines (120 loc) · 3.44 KB
/
copyright-format.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
set -e
ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd)
function usage()
{
echo "Usage: copyright-format.sh [<args>]"
echo
echo "Options:"
echo " -h|--help print the help info"
echo " -c|--check check whether there are format issues in C++ files"
echo " -f|--fix fix all the format issue directly"
echo
}
pushd "$ROOT_DIR"/../..
COPYRIGHT_FILE="$ROOT_DIR"/default-copyright.txt
COPYRIGHT=$(cat "$COPYRIGHT_FILE")
LINES_NUM=$(echo "$COPYRIGHT" | wc -l)
DEFAULT_YEAR="2017"
# The key words that must included in copyright description.
KEYWORDS=(
"Copyright"
"Ray Authors"
"License"
)
RUN_TYPE="diff"
FILE_LIST_TMP_FILE="/tmp/.cr_file_list_tmp"
TMP_FILE="/tmp/.cr_tmp"
# The cpp files that will be checked and formatted.
INCLUDES_CPP_DIRS=(
src
cpp
)
# The files that should be skipped.
EXCLUDES_DIRS=(
src/ray/object_manager/plasma/
src/ray/thirdparty
cpp/example
)
ERROR_FILES=()
# Parse options
while [ $# -gt 0 ]; do
key="$1"
case $key in
-h|--help)
usage
exit 0
;;
-c|--check)
RUN_TYPE="diff"
;;
-f|--fix)
RUN_TYPE="fix"
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage
exit 1
;;
esac
shift
done
for directory in "${INCLUDES_CPP_DIRS[@]}"; do
cmd_args="find $directory -type f"
for excluded in "${EXCLUDES_DIRS[@]}"; do
cmd_args="${cmd_args} ! -path "
cmd_args="${cmd_args} '${excluded}"
if [[ "${excluded: -1}" != "/" ]];then
cmd_args="${cmd_args}/"
fi
cmd_args="${cmd_args}*'"
done
cmd_args="${cmd_args} \( -name '*.cc' -or -name '*.h' \)"
eval "${cmd_args}" > "$FILE_LIST_TMP_FILE"
while IFS=$'\n' read -r f
do
head_content=$(sed -n "1,${LINES_NUM}p" "$f")
match="true"
for key_word in "${KEYWORDS[@]}"; do
if [[ $head_content != *$key_word* ]];then
match="false"
break
fi
done
if [[ "$match" == "false" ]]; then
ERROR_FILES+=("$f")
if [[ "$RUN_TYPE" == "fix" ]];then
first_added_year=$(git log --pretty=format:%ad --date=short "$f" | awk -F- '{print $1}' | tail -1)
last_modified_year=$(git log --pretty=format:%ad --date=short "$f" | awk -F- '{print $1}' | head -1)
years=""
if [[ "$first_added_year" == "$last_modified_year" ]];then
years=$first_added_year
else
years="${first_added_year}-${last_modified_year}"
fi
sed '1s/^/\n/' "$f" > $TMP_FILE
mv $TMP_FILE "$f"
cat "$COPYRIGHT_FILE" "$f" > $TMP_FILE
mv $TMP_FILE "$f"
sed "s/${DEFAULT_YEAR}/${years}/" "$f" > $TMP_FILE
mv $TMP_FILE "$f"
fi
fi
done < $"$FILE_LIST_TMP_FILE"
rm -f "$FILE_LIST_TMP_FILE"
done
if [[ ${#ERROR_FILES[*]} -gt 0 ]];then
if [[ "$RUN_TYPE" == "fix" ]];then
echo "Copyright has been added to the files below:"
printf '%s\n' "${ERROR_FILES[@]}"
exit 0
else
echo "Missing copyright info at the beginning of below files. Please run 'sh ci/travis/copyright-format.sh -f' to fix them:"
printf '%s\n' "${ERROR_FILES[@]}"
exit 1
fi
else
echo 'Copyright check succeeded.'
exit 0
fi