Skip to content

Commit fda9de2

Browse files
committed
wip
1 parent bb1798e commit fda9de2

1 file changed

Lines changed: 14 additions & 220 deletions

File tree

scripts/checkCopyrightPresent.sh

Lines changed: 14 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -1,239 +1,33 @@
11
#!/bin/bash
2-
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3-
repo_root="$(cd "${script_dir}/.." && pwd)"
4-
rewrite_headers=false
5-
6-
for arg in "$@"; do
7-
if [ "$arg" = "--rewrite-header" ]; then
8-
rewrite_headers=true
9-
fi
10-
done
11-
122
filesWithOutCopyright=()
133
filesWithOutSPDX=()
14-
filesWithInvalidHeader=()
15-
filesWithAdunaYearAfter2020=()
16-
expectedHeader=(
17-
"/*******************************************************************************"
18-
"__YEAR__"
19-
" *"
20-
" * All rights reserved. This program and the accompanying materials"
21-
" * are made available under the terms of the Eclipse Distribution License v1.0"
22-
" * which accompanies this distribution, and is available at"
23-
" * http://www.eclipse.org/org/documents/edl-v10.php."
24-
" *"
25-
" * SPDX-License-Identifier: BSD-3-Clause"
26-
" *******************************************************************************/"
27-
)
28-
29-
count_asterisks() {
30-
local line="$1"
31-
local only
32-
only="${line//[^*]/}"
33-
printf '%s' "${#only}"
34-
}
35-
36-
line_matches_expected() {
37-
local expected="$1"
38-
local actual="$2"
39-
local expected_count
40-
local len
41-
local i
42-
local candidate
43-
local prev
44-
local curr
45-
46-
if [ "$expected" = "$actual" ]; then
47-
return 0
48-
fi
49-
50-
expected_count="$(count_asterisks "$expected")"
51-
if [ "$expected_count" -le 10 ]; then
52-
return 1
53-
fi
54-
55-
len=${#expected}
56-
for ((i=0; i<len; i++)); do
57-
if [ "${expected:i:1}" = "*" ]; then
58-
candidate="${expected:0:i}${expected:i+1}"
59-
if [ "$candidate" = "$actual" ]; then
60-
return 0
61-
fi
62-
fi
63-
done
64-
65-
for ((i=0; i<=len; i++)); do
66-
prev=""
67-
curr=""
68-
if [ "$i" -gt 0 ]; then
69-
prev="${expected:i-1:1}"
70-
fi
71-
curr="${expected:i:1}"
72-
if [ "$prev" = "*" ] || [ "$curr" = "*" ]; then
73-
candidate="${expected:0:i}*${expected:i}"
74-
if [ "$candidate" = "$actual" ]; then
75-
return 0
76-
fi
77-
fi
78-
done
79-
return 1
80-
}
81-
82-
rewrite_header() {
83-
local file="$1"
84-
local year_line="$2"
85-
local tmp_file
86-
local mode
87-
mode=$(stat -f %Lp "$file" 2>/dev/null || stat -c %a "$file")
88-
tmp_file="$(mktemp)"
89-
{
90-
echo "${expectedHeader[0]}"
91-
echo "$year_line"
92-
echo "${expectedHeader[2]}"
93-
echo "${expectedHeader[3]}"
94-
echo "${expectedHeader[4]}"
95-
echo "${expectedHeader[5]}"
96-
echo "${expectedHeader[6]}"
97-
echo "${expectedHeader[7]}"
98-
echo "${expectedHeader[8]}"
99-
echo "${expectedHeader[9]}"
100-
tail -n +11 "$file"
101-
} > "$tmp_file"
102-
if [ -n "$mode" ]; then
103-
chmod "$mode" "$tmp_file"
104-
fi
105-
mv "$tmp_file" "$file"
106-
}
107-
108-
read_header_lines() {
109-
local file="$1"
110-
header_lines=()
111-
while IFS= read -r line; do
112-
line="${line%$'\r'}"
113-
header_lines+=("$line")
114-
done < <(head -n 10 "$file")
115-
}
116-
117-
118-
while IFS= read -r -d '' i; do
119-
if [[ "$i" == "${repo_root}/core/queryparser/sparql/JavaCC/"* ]]; then
120-
continue
121-
fi
4+
for i in $(find ../ -name pom.xml); do
1225
# only look in non test files but make sure src/main exists.
1236
# and not in package-info
1247
dir="${i/pom.xml/}src/"
1258
if [ -d "$dir" ]; then
126-
while IFS= read -r -d '' c; do
127-
echo "$c"
128-
read_header_lines "$c"
129-
130-
generated=false
131-
if grep -q 'Generated By:' "$c"; then
132-
generated=true
133-
fi
134-
135-
if [ "$generated" = false ]; then
136-
has_copyright=false
137-
if [ ${#header_lines[@]} -ge 2 ] && [[ "${header_lines[1]}" == *Copyright* ]]; then
138-
has_copyright=true
139-
elif grep -q "Copyright" "$c"; then
140-
has_copyright=true
141-
fi
142-
143-
if [ ${#header_lines[@]} -lt 10 ]; then
144-
if [ "$has_copyright" = true ]; then
145-
filesWithInvalidHeader+=("$c")
146-
else
147-
filesWithOutCopyright+=("$c")
148-
fi
149-
else
150-
match_count=0
151-
has_valid_year_line=false
152-
aduna_year=""
153-
154-
if [[ "${header_lines[1]}" =~ ^[[:space:]]*\*\ Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors\.$ ]]; then
155-
has_valid_year_line=true
156-
elif [[ "${header_lines[1]}" =~ ^[[:space:]]*\*\ Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors,\ Aduna,\ and\ others\.$ ]]; then
157-
has_valid_year_line=true
158-
aduna_year="${BASH_REMATCH[1]}"
159-
fi
160-
161-
for idx in 0 2 3 4 5 6 7 8 9; do
162-
if line_matches_expected "${expectedHeader[$idx]}" "${header_lines[$idx]}"; then
163-
match_count=$((match_count + 1))
164-
fi
165-
done
166-
167-
if [ "$rewrite_headers" = true ] && [ "$match_count" -ge 4 ] && [ "$match_count" -lt 9 ]; then
168-
rewrite_header "$c" "${header_lines[1]}"
169-
read_header_lines "$c"
170-
match_count=0
171-
has_valid_year_line=false
172-
aduna_year=""
173-
if [[ "${header_lines[1]}" =~ ^[[:space:]]*\*\ Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors\.$ ]]; then
174-
has_valid_year_line=true
175-
elif [[ "${header_lines[1]}" =~ ^[[:space:]]*\*\ Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors,\ Aduna,\ and\ others\.$ ]]; then
176-
has_valid_year_line=true
177-
aduna_year="${BASH_REMATCH[1]}"
178-
fi
179-
for idx in 0 2 3 4 5 6 7 8 9; do
180-
if line_matches_expected "${expectedHeader[$idx]}" "${header_lines[$idx]}"; then
181-
match_count=$((match_count + 1))
182-
fi
183-
done
184-
fi
185-
186-
if [ -n "$aduna_year" ] && [ "$aduna_year" -gt 2020 ]; then
187-
filesWithAdunaYearAfter2020+=("$c")
188-
fi
189-
190-
if [ "$has_valid_year_line" = false ]; then
191-
if [ "$has_copyright" = true ]; then
192-
filesWithInvalidHeader+=("$c")
193-
else
194-
filesWithOutCopyright+=("$c")
195-
fi
196-
elif [ "$match_count" -lt 9 ]; then
197-
filesWithInvalidHeader+=("$c")
198-
fi
199-
fi
9+
for c in $(find ${dir} -name *.java -not -name package-info.java -exec grep -L Copyright {} \;); do
10+
# skip a file that is generated by.
11+
f=$(grep -L 'Generated By:' $c)
12+
if [ ! -z $f ]; then
13+
filesWithOutCopyright+=($f)
20014
fi
201-
202-
has_spdx=false
203-
if [ ${#header_lines[@]} -ge 9 ] && [[ "${header_lines[8]}" == *"SPDX-License-Identifier: BSD-3-Clause"* ]]; then
204-
has_spdx=true
205-
elif grep -q 'SPDX-License-Identifier: BSD-3-Clause' "$c"; then
206-
has_spdx=true
207-
fi
208-
209-
if [ "$has_spdx" = false ]; then
210-
filesWithOutSPDX+=("$c")
211-
fi
212-
done < <(find "${dir}" -name "*.java" -not -name "package-info.java" -print0)
15+
done
16+
for c in $(find ${dir} -name *.java -not -name package-info.java -exec grep -L 'SPDX-License-Identifier: BSD-3-Clause' {} \;); do
17+
filesWithOutSPDX+=($c)
18+
done
21319
fi
214-
done < <(find "${repo_root}" -type d -name target -prune -o -type f -name "pom.xml" -print0)
20+
done
21521

216-
for f in "${filesWithOutCopyright[@]}"; do
22+
for f in ${filesWithOutCopyright[@]}; do
21723
echo "Missing copyright: $f"
21824
done
219-
for f in "${filesWithInvalidHeader[@]}"; do
220-
echo "Invalid copyright header: $f"
221-
done
222-
for f in "${filesWithAdunaYearAfter2020[@]}"; do
223-
echo "Aduna header year must be 2020 or earlier: $f"
224-
done
225-
for f in "${filesWithOutSPDX[@]}"; do
25+
for f in ${filesWithOutSPDX[@]}; do
22626
echo "Missing SPDX line in: $f"
22727
done
228-
if [ ${#filesWithOutCopyright[@]} -ne 0 ] || [ ${#filesWithInvalidHeader[@]} -ne 0 ] || [ ${#filesWithAdunaYearAfter2020[@]} -ne 0 ]; then
229-
if [ "$rewrite_headers" = false ]; then
230-
echo "Tip: rerun with --rewrite-header to auto-fix headers when possible."
231-
fi
28+
if [ ${#filesWithOutCopyright[@]} -ne 0 ]; then
23229
exit 1
23330
fi
23431
if [ ${#filesWithOutSPDX[@]} -ne 0 ]; then
235-
if [ "$rewrite_headers" = false ]; then
236-
echo "Tip: rerun with --rewrite-header to auto-fix headers when possible."
237-
fi
23832
exit 2
23933
fi

0 commit comments

Comments
 (0)