-
Notifications
You must be signed in to change notification settings - Fork 5
/
apply_copyright
executable file
·86 lines (69 loc) · 1.57 KB
/
apply_copyright
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
#!/bin/bash
LEN=`wc -l COPYRIGHT | cut -d " " -f 1`
FIRST_LINE="`head -n 1 COPYRIGHT`"
function apply_to_file() {
F="$1"
cat COPYRIGHT "$F" > "$F.copyright"
mv "$F.copyright" "$F"
echo "$F - added"
}
function update_file() {
# update file if only the first line is different
F="$1"
head -n 1 "$F" | grep "$FIRST_LINE" &>/dev/null
if [ $? -ne 0 ]; then
DIFF_LINES_NUM=`head -n $LEN "$F" | diff COPYRIGHT - | egrep "^<" | wc -l`
if [ "$DIFF_LINES_NUM" == "1" ]; then
head -n 1 COPYRIGHT > "$F.copyright"
tail -n +2 "$F" >> "$F.copyright"
mv "$F.copyright" "$F"
echo "$F - updated"
return
fi
fi
echo "$F - !!!!!!!!!!!!!!!!!!!!!! CAN'T UPDATE !!!!!!!!!!!!!!!!!!!!!!"
}
function do_file() {
F="$1"
head -n 1 "$F" | grep "Copyright" | grep "Pier Carlo Chiodi" &>/dev/null
if [ $? -eq 0 ]; then
# copyright statement already there
# is the current one?
head -n 1 "$F" | grep "$FIRST_LINE" &>/dev/null
if [ $? -eq 0 ]; then
echo "$F - ok"
else
# copyright statement outdated
update_file "$F"
fi
else
# copyright statement missing
head -n 1 "$F" | egrep "^#" &>/dev/null
if [ $? -eq 0 ]; then
echo "$F - !!!!!!!!!!!!!!!!!!!!!! SPECIAL ATTENTION NEEDED !!!!!!!!!!!!!!!!!!!!!!"
else
apply_to_file "$F"
fi
fi
}
function do_dir() {
DIR="$1"
PATTERN="$2"
for f in $DIR/$PATTERN
do
if [ -f "$f" ]; then
do_file "$f"
fi
done
}
function do_subdirs() {
DIR="$1"
PATTERN="$2"
for d in `find "$DIR" -type d`
do
do_dir "$d" "$PATTERN"
done
}
do_subdirs "pierky" "*.py"
do_subdirs "scripts" "*"
do_subdirs "tests" "*.py"