license-check.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. replace_years() {
  38. sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/201[56789]/YEARS/'
  39. }
  40. # Checks the Copyright headers for *.swift files in this repository against the
  41. # expected headers.
  42. #
  43. # Prints the names of all files with invalid or missing headers and exits with
  44. # a non-zero status code.
  45. check_copyright_headers() {
  46. # Exceptions:
  47. # - {echo,annotations,language_service,http}.pb.swift: Google is the author of
  48. # the corresponding .protos, so the generated header has Google as the
  49. # author.
  50. # - LinuxMain.swift, XCTestManifests.swift: Both of these files are generated
  51. # by SwiftPM and do not have headers.
  52. while read -r filename; do
  53. case $filename in
  54. # The .grpc.swift and .pb.swift files have additional generated headers with
  55. # warnings that they have been generated and should not be edited.
  56. # Package.swift is preceeded by a "swift-tools-version" line.
  57. *.grpc.swift)
  58. expected_sha="$SWIFT_GRPC_SHA"
  59. drop_first=9
  60. expected_lines=13
  61. ;;
  62. *.pb.swift)
  63. expected_sha="$SWIFT_GRPC_PB"
  64. drop_first=9
  65. expected_lines=13
  66. ;;
  67. */Package.swift)
  68. expected_sha="$SWIFT_SHA"
  69. drop_first=1
  70. expected_lines=15
  71. ;;
  72. */Package@swift-*.*.swift)
  73. expected_sha="$SWIFT_SHA"
  74. drop_first=1
  75. expected_lines=15
  76. ;;
  77. *)
  78. expected_sha="$SWIFT_SHA"
  79. drop_first=0
  80. expected_lines=15
  81. ;;
  82. esac
  83. actual_sha=$(head -n "$((drop_first + expected_lines))" "$filename" \
  84. | tail -n "$expected_lines" \
  85. | sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' \
  86. | shasum \
  87. | awk '{print $1}')
  88. if [ "$actual_sha" != "$expected_sha" ]; then
  89. printf "\033[0;31mMissing or invalid copyright headers in '%s'\033[0m\n" "$filename"
  90. errors=$(( errors + 1 ))
  91. fi
  92. done < <(find . -name '*.swift' \
  93. ! -name '*.pb.swift' \
  94. ! -name '*.grpc.swift' \
  95. ! -name 'LinuxMain.swift' \
  96. ! -name 'XCTestManifests.swift' \
  97. ! -path './FuzzTesting/.build/*' \
  98. ! -path './Performance/QPSBenchmark/.build/*' \
  99. ! -path './Performance/Benchmarks/.build/*' \
  100. ! -path './scripts/.swift-format-source/*' \
  101. ! -path './.build/*')
  102. }
  103. errors=0
  104. check_copyright_headers
  105. exit $errors