license-check.sh 3.6 KB

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