license-check.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.pb.swift: Google is the author of the corresponding .proto, so the
  82. # generated header has Google as the author.
  83. # - LinuxMain.swift, XCTestManifests.swift: Both of these files are generated
  84. # by SwiftPM and do not have headers.
  85. find . -name '*.swift' \
  86. ! -name 'echo.pb.swift' \
  87. ! -name 'LinuxMain.swift' \
  88. ! -name 'XCTestManifests.swift' \
  89. ! -path './.build/*' \
  90. | while read -r filename; do
  91. case $filename in
  92. # The .grpc.swift and .pb.swift files have additional generated headers with
  93. # warnings that they have been generated and should not be edited.
  94. # Package.swift is preceeded by a "swift-tools-version" line.
  95. *.grpc.swift)
  96. expected_sha="$SWIFT_GRPC_SHA"
  97. drop_first=8
  98. expected_lines=13
  99. ;;
  100. *.pb.swift)
  101. expected_sha="$SWIFT_GRPC_PB"
  102. drop_first=8
  103. expected_lines=13
  104. ;;
  105. */Package.swift)
  106. expected_sha="$SWIFT_SHA"
  107. drop_first=1
  108. expected_lines=15
  109. ;;
  110. *)
  111. expected_sha="$SWIFT_SHA"
  112. drop_first=0
  113. expected_lines=15
  114. ;;
  115. esac
  116. actual_sha=$(head -n "$((drop_first + expected_lines))" "$filename" \
  117. | tail -n "$expected_lines" \
  118. | sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/201[56789]/YEARS/' \
  119. | shasum \
  120. | awk '{print $1}')
  121. if [ "$actual_sha" != "$expected_sha" ]; then
  122. printf "\033[0;31mMissing or invalid copyright headers in '$filename'\033[0m\n"
  123. exit 1
  124. fi
  125. done
  126. }
  127. check_copyright_headers