forked from metoppv/improver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyright_check
More file actions
executable file
·121 lines (105 loc) · 3.37 KB
/
copyright_check
File metadata and controls
executable file
·121 lines (105 loc) · 3.37 KB
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
#!/bin/bash
copyright_header_pattern="\
# \(C\) Crown Copyright, Met Office. All rights reserved\.\n\
#\n\
# This file is part of 'IMPROVER' and is released under the BSD 3-Clause license\.\n\
# See LICENSE in the root of the repository for full licensing details\.\
"
copyright_header=$(echo "$copyright_header_pattern" | sed 's/\\(/\(/g; s/\\)/\)/g; s/\\\././g')
show_help() {
echo "info: Check the copyright header in the specified files."
echo " If the non-empty file does not contain the copyright header, it "
echo " will be added. If a file looks like it does contain a copyright "
echo " header, it will verified that it matches exactly."
echo " Hash bangs are preserved."
echo ''
echo "usage: $0 FILEPATH..."
echo ''
echo 'positional arguments:'
echo ' FILEPATH... The file(s) to check'
echo ""
echo "options:"
echo " -h|--help Show this help message and exit."
echo " -v|--verbose Print verbose output."
echo " -f|--fix Fix files with missing copyright header by adding it."
echo ""
}
VERBOSE=false
FIX=false
ARGS=""
DEBUG=false
while :; do
case $1 in
--help|-h)
show_help
exit 0
;;
--verbose|-v)
VERBOSE=true
;;
--fix|-f)
FIX=true
;;
-*)
echo "Unknown option: $1"
show_help
exit 1
;;
*)
ARGS+="$1"$'\n'
;;
esac
shift
[ -z "$1" ] && break # Exit loop when there are no more arguments
done
contains_copyright() {
grep -qi '# .*copyright' $1
}
defines_hash_bang() {
grep -q '^#!' $1
}
correct_copyright() {
grep -zPq "$copyright_header_pattern" "$1"
}
# Function to delete temporary files
cleanup() {
for filepath in "$@"; do
tmp_file="${filepath}.tmp"
if [ -f "$tmp_file" ]; then
[[ ${DEBUG} == true ]] && echo "problem occurred while adding Copyright header, deleting temporary file: $tmp_file" >&2
rm -f "$tmp_file" 2&> /dev/null
fi
done
}
# Trap the EXIT signal to call the cleanup function
trap 'cleanup $ARGS' EXIT
found_error=0
for filepath in $ARGS; do
[[ ${VERBOSE} == true ]] && echo checking ${filepath}
if correct_copyright "${filepath}"; then
[[ ${VERBOSE} == true ]] && echo "File '${filepath}': Copyright verified"
continue
fi
if contains_copyright "${filepath}"; then
echo "Incorrect Copyright header in '${filepath}'" >&2
[[ ${VERBOSE} == true ]] && (diff <(echo -e $copyright_header) <(grep -i -A3 '# .*copyright' ${filepath}) >&2; echo >&2)
found_error=1
elif [ -s "${filepath}" ]; then # skip empty files
echo "File '${filepath}' is missing a copyright header." >&2
if [[ ${FIX} == true ]]; then
echo "Adding missing Copyright to '${filepath}'"
tmp_file="${filepath}.tmp"
if defines_hash_bang "${filepath}"; then
head -n 1 ${filepath} > ${tmp_file}
echo -e ${copyright_header} >> ${tmp_file}
tail -n +2 ${filepath} >> ${tmp_file}
else
echo -e ${copyright_header} > ${tmp_file}
cat ${filepath} >> ${tmp_file}
fi
mv ${tmp_file} ${filepath}
found_error=1
fi
fi
done
exit ${found_error}