license-check.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. # Copyright 2019, gRPC Authors All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script checks the copyright headers in source *.swift source files and
  16. # exits if they do not match the expected header. The year, or year range in
  17. # headers is replaced with 'YEARS' for comparison.
  18. # Copyright header text and SHA for *.swift files
  19. read -r -d '' COPYRIGHT_HEADER_SWIFT << 'EOF'
  20. /*
  21. * Copyright YEARS, gRPC Authors All rights reserved.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. */
  35. EOF
  36. SWIFT_SHA=$(echo "$COPYRIGHT_HEADER_SWIFT" | shasum | awk '{print $1}')
  37. # Copyright header text and SHA for *.grpc.swift files
  38. read -r -d '' COPYRIGHT_HEADER_SWIFT_GRPC << 'EOF'
  39. // Copyright YEARS, gRPC Authors All rights reserved.
  40. //
  41. // Licensed under the Apache License, Version 2.0 (the "License");
  42. // you may not use this file except in compliance with the License.
  43. // You may obtain a copy of the License at
  44. //
  45. // http://www.apache.org/licenses/LICENSE-2.0
  46. //
  47. // Unless required by applicable law or agreed to in writing, software
  48. // distributed under the License is distributed on an "AS IS" BASIS,
  49. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50. // See the License for the specific language governing permissions and
  51. // limitations under the License.
  52. EOF
  53. SWIFT_GRPC_SHA=$(echo "$COPYRIGHT_HEADER_SWIFT_GRPC" | shasum | awk '{print $1}')
  54. # Copyright header text and SHA for *.pb.swift files
  55. read -r -d '' COPYRIGHT_HEADER_SWIFT_PB << 'EOF'
  56. // Copyright YEARS gRPC authors.
  57. //
  58. // Licensed under the Apache License, Version 2.0 (the "License");
  59. // you may not use this file except in compliance with the License.
  60. // You may obtain a copy of the License at
  61. //
  62. // http://www.apache.org/licenses/LICENSE-2.0
  63. //
  64. // Unless required by applicable law or agreed to in writing, software
  65. // distributed under the License is distributed on an "AS IS" BASIS,
  66. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  67. // See the License for the specific language governing permissions and
  68. // limitations under the License.
  69. EOF
  70. SWIFT_GRPC_PB=$(echo "$COPYRIGHT_HEADER_SWIFT_PB" | shasum | awk '{print $1}')
  71. replace_years() {
  72. sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/201[56789]/YEARS/'
  73. }
  74. # Checks the Copyright headers for *.swift files in this repository against the
  75. # expected headers.
  76. #
  77. # Prints the names of all files with invalid or missing headers and exits with
  78. # a non-zero status code.
  79. check_copyright_headers() {
  80. # Exceptions:
  81. # - {echo,annotations,language_service,http}.pb.swift: Google is the author of
  82. # the corresponding .protos, so the generated header has Google as the
  83. # author.
  84. # - LinuxMain.swift, XCTestManifests.swift: Both of these files are generated
  85. # by SwiftPM and do not have headers.
  86. while read -r filename; do
  87. case $filename in
  88. # The .grpc.swift and .pb.swift files have additional generated headers with
  89. # warnings that they have been generated and should not be edited.
  90. # Package.swift is preceeded by a "swift-tools-version" line.
  91. *.grpc.swift)
  92. expected_sha="$SWIFT_GRPC_SHA"
  93. drop_first=8
  94. expected_lines=13
  95. ;;
  96. *.pb.swift)
  97. expected_sha="$SWIFT_GRPC_PB"
  98. drop_first=9
  99. expected_lines=13
  100. ;;
  101. */Package.swift)
  102. expected_sha="$SWIFT_SHA"
  103. drop_first=1
  104. expected_lines=15
  105. ;;
  106. *)
  107. expected_sha="$SWIFT_SHA"
  108. drop_first=0
  109. expected_lines=15
  110. ;;
  111. esac
  112. actual_sha=$(head -n "$((drop_first + expected_lines))" "$filename" \
  113. | tail -n "$expected_lines" \
  114. | sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' \
  115. | shasum \
  116. | awk '{print $1}')
  117. if [ "$actual_sha" != "$expected_sha" ]; then
  118. printf "\033[0;31mMissing or invalid copyright headers in '%s'\033[0m\n" "$filename"
  119. errors=$(( errors + 1 ))
  120. fi
  121. done < <(find . -name '*.swift' \
  122. ! -name 'echo.pb.swift' \
  123. ! -name 'annotations.pb.swift' \
  124. ! -name 'language_service.pb.swift' \
  125. ! -name 'http.pb.swift' \
  126. ! -name 'LinuxMain.swift' \
  127. ! -name 'XCTestManifests.swift' \
  128. ! -path './.build/*')
  129. }
  130. errors=0
  131. check_copyright_headers
  132. if [[ "$errors" == 0 ]]; then
  133. echo "License headers: OK"
  134. else
  135. echo "License headers: found $errors issue(s)."
  136. fi
  137. exit $errors