-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyright_check
executable file
·88 lines (74 loc) · 2.37 KB
/
copyright_check
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
#!/bin/bash
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 ""
}
if [[ " $@ " =~ " --help " || " $@ " =~ " -h " ]]; then
show_help
exit 0
fi
copyright_header="\
# (C) Crown Copyright, Met Office. All rights reserved.\n\
#\n\
# This file is part of 'dagrunner' and is released under the BSD 3-Clause license.\n\
# See LICENSE in the root of the repository for full licensing details.\
"
contains_copyright() {
grep -qi '# .*copyright' $1
}
defines_hash_bang() {
grep -q '^#!' $1
}
correct_copyright() {
python -c 'import sys; sys.exit(0 if "'"$1"'" in open("'"$2"'", "r").read() else 1)'
}
# Function to delete temporary files
cleanup() {
for filepath in "$@"; do
tmp_file="${filepath}.tmp"
if [ -f "$tmp_file" ]; then
echo "Deleting temporary file: $tmp_file"
rm -f "$tmp_file" 2&> /dev/null
fi
done
}
# Trap the EXIT signal to call the cleanup function
trap 'cleanup "$@"' EXIT
found_error=0
for filepath in "$@"; do
if contains_copyright "${filepath}"; then
#echo "File '${filepath}' already contains Crown Copyright"
if correct_copyright "${copyright_header}" "${filepath}"; then
continue
else
echo "Incorrect Copyright header in '${filepath}'"
found_error=1
fi
elif [ -s "${filepath}" ]; then # skip empty files
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
done
exit ${found_error}