-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathlicense-check.sh
executable file
·109 lines (102 loc) · 3.69 KB
/
license-check.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
#!/bin/bash
## Copyright 2019, gRPC Authors All rights reserved.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
# This script checks the copyright headers in source *.swift source files and
# exits if they do not match the expected header. The year, or year range in
# headers is replaced with 'YEARS' for comparison.
# Copyright header text and SHA for *.swift files
read -r -d '' COPYRIGHT_HEADER_SWIFT << 'EOF'
/*
* Copyright YEARS, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
EOF
SWIFT_SHA=$(echo "$COPYRIGHT_HEADER_SWIFT" | shasum | awk '{print $1}')
# Checks the Copyright headers for *.swift files in this repository against the
# expected headers.
#
# Prints the names of all files with invalid or missing headers and exits with
# a non-zero status code.
check_copyright_headers() {
# Exceptions:
# - {echo,annotations,language_service,http}.pb.swift: Google is the author of
# the corresponding .protos, so the generated header has Google as the
# author.
# - LinuxMain.swift, XCTestManifests.swift: Both of these files are generated
# by SwiftPM and do not have headers.
while read -r filename; do
case $filename in
# The .grpc.swift and .pb.swift files have additional generated headers with
# warnings that they have been generated and should not be edited.
# Package.swift is preceeded by a "swift-tools-version" line.
*.grpc.swift)
expected_sha="$SWIFT_GRPC_SHA"
drop_first=9
expected_lines=13
;;
*.pb.swift)
expected_sha="$SWIFT_GRPC_PB"
drop_first=9
expected_lines=13
;;
*/Package.swift)
expected_sha="$SWIFT_SHA"
drop_first=1
expected_lines=15
;;
*/Package@swift-*.*.swift)
expected_sha="$SWIFT_SHA"
drop_first=1
expected_lines=15
;;
*/Package@swift-*.swift)
expected_sha="$SWIFT_SHA"
drop_first=1
expected_lines=15
;;
*)
expected_sha="$SWIFT_SHA"
drop_first=0
expected_lines=15
;;
esac
actual_sha=$(head -n "$((drop_first + expected_lines))" "$filename" \
| tail -n "$expected_lines" \
| sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' \
| shasum \
| awk '{print $1}')
if [ "$actual_sha" != "$expected_sha" ]; then
printf "\033[0;31mMissing or invalid copyright headers in '%s'\033[0m\n" "$filename"
errors=$(( errors + 1 ))
fi
done < <(find . -name '*.swift' \
! -name '*.pb.swift' \
! -name '*.grpc.swift' \
! -path './Sources/GRPCCore/Documentation.docc/*' \
! -path '.*/.build/*')
}
errors=0
check_copyright_headers
exit $errors