check_copyright.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # Copyright The OpenTelemetry Authors
  3. # SPDX-License-Identifier: Apache-2.0
  4. if [[ ! -e tools/check_copyright.sh ]]; then
  5. echo "This tool must be run from the topmost directory." >&2
  6. exit 1
  7. fi
  8. set -e
  9. #
  10. # Process input file .copyright-ignore,
  11. # - remove comments
  12. # - remove blank lines
  13. # to create file /tmp/all_ignored
  14. #
  15. grep -v "^#" < .copyright-ignore | \
  16. grep -v "^[[:space:]]*$" > /tmp/all_ignored
  17. #
  18. # Find all files from the repository
  19. # to create file /tmp/all_checked
  20. #
  21. find . -type f -print | sort -u > /tmp/all_checked
  22. #
  23. # Filter out /tmp/all_checked,
  24. # remove all ignored patterns from /tmp/all_ignored
  25. # When the pattern is *.md,
  26. # make sure to filter *\.md to avoid hiding *.cmd
  27. # Then, *\.md needs to be escaped to *\\.md,
  28. # to be given to egrep, hence the sed.
  29. #
  30. while IFS= read -r PATTERN; do
  31. SAFE_PATTERN=`echo "${PATTERN}" | sed "s!\.!\\\\\.!g"`
  32. echo "Filtering out ${SAFE_PATTERN}"
  33. egrep -v "${SAFE_PATTERN}" < /tmp/all_checked > /tmp/all_checked-tmp
  34. mv /tmp/all_checked-tmp /tmp/all_checked
  35. done < /tmp/all_ignored
  36. #
  37. # For all files in /tmp/all_checked
  38. # - verify there is copyright
  39. # - verify there is a license
  40. # and append to /tmp/all_missing
  41. #
  42. # Valid copyright strings are:
  43. # - Copyright The OpenTelemetry Authors
  44. #
  45. # Valid license strings are:
  46. # - SPDX-License-Identifier: Apache-2.0
  47. #
  48. rm -rf /tmp/all_missing
  49. touch /tmp/all_missing
  50. for FILE in `cat /tmp/all_checked`
  51. do
  52. echo "Checking ${FILE}"
  53. export COPYRIGHT=`head -10 ${FILE} | grep -c "Copyright The OpenTelemetry Authors"`
  54. export LICENSE=`head -10 ${FILE} | grep -c "SPDX-License-Identifier: Apache-2.0"`
  55. if [ "$COPYRIGHT" == "0" ]; then
  56. echo "Missing copyright in ${FILE}" >> /tmp/all_missing
  57. fi;
  58. if [ "${LICENSE}" == "0" ]; then
  59. echo "Missing license in ${FILE}" >> /tmp/all_missing
  60. fi;
  61. done
  62. #
  63. # Final report
  64. #
  65. FAIL_COUNT=`wc -l < /tmp/all_missing`
  66. if [ ${FAIL_COUNT} != "0" ]; then
  67. #
  68. # CI FAILED
  69. #
  70. cat /tmp/all_missing
  71. echo "Total number of failed checks: ${FAIL_COUNT}"
  72. exit 1
  73. fi;
  74. #
  75. # CI PASSED
  76. #
  77. exit 0