check_license.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. acquire_rat_jar () {
  19. URL="https://repo1.maven.org/maven2/org/apache/rat/apache-rat/${RAT_VERSION}/apache-rat-${RAT_VERSION}.jar"
  20. JAR="$rat_jar"
  21. # Download rat launch jar if it hasn't been downloaded yet
  22. if [ ! -f "$JAR" ]; then
  23. # Download
  24. printf "Attempting to fetch rat\n"
  25. JAR_DL="${JAR}.part"
  26. if [ $(command -v curl) ]; then
  27. curl -L --silent "${URL}" > "$JAR_DL" && mv "$JAR_DL" "$JAR"
  28. elif [ $(command -v wget) ]; then
  29. wget --quiet ${URL} -O "$JAR_DL" && mv "$JAR_DL" "$JAR"
  30. else
  31. printf "You do not have curl or wget installed, please install rat manually.\n"
  32. exit -1
  33. fi
  34. fi
  35. unzip -tq "$JAR" &> /dev/null
  36. if [ $? -ne 0 ]; then
  37. # We failed to download
  38. rm "$JAR"
  39. printf "Our attempt to download rat locally to ${JAR} failed. Please install rat manually.\n"
  40. exit -1
  41. fi
  42. printf "Done downloading.\n"
  43. }
  44. # Go to the project root directory
  45. FWDIR="$(cd "`dirname "$0"`"/..; pwd)"
  46. cd "$FWDIR"
  47. TMP_DIR=/tmp
  48. if test -x "$JAVA_HOME/bin/java"; then
  49. declare java_cmd="$JAVA_HOME/bin/java"
  50. else
  51. declare java_cmd=java
  52. fi
  53. export RAT_VERSION=0.13
  54. export rat_jar="${TMP_DIR}"/lib/apache-rat-${RAT_VERSION}.jar
  55. mkdir -p ${TMP_DIR}/lib
  56. [[ -f "$rat_jar" ]] || acquire_rat_jar || {
  57. echo "Download failed. Obtain the rat jar manually and place it at $rat_jar"
  58. exit 1
  59. }
  60. echo "Running license checks. This can take a while."
  61. echo "$FWDIR"/.rat-excludes
  62. $java_cmd -jar "$rat_jar" -E "$FWDIR"/.rat-excludes -d "$FWDIR" > rat-results.txt
  63. if [ $? -ne 0 ]; then
  64. echo "RAT exited abnormally"
  65. exit 1
  66. fi
  67. ERRORS="$(cat rat-results.txt | grep -e "??")"
  68. if test ! -z "$ERRORS"; then
  69. echo >&2 "Could not find Apache license headers in the following files:"
  70. echo >&2 "$ERRORS"
  71. exit 1
  72. else
  73. echo -e "RAT checks passed."
  74. fi