update-vendor-licenses.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env bash
  2. # Copyright 2015 The Kubernetes Authors.
  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. # Update the Godeps/LICENSES document.
  16. # Generates a table of Godep dependencies and their license.
  17. #
  18. # Usage:
  19. # $0 [--create-missing] [/path/to/licenses]
  20. #
  21. # --create-missing will write the files that only exist upstream, locally.
  22. # This option is mostly used for testing as we cannot check-in any of the
  23. # additionally created files into the vendor auto-generated tree.
  24. #
  25. # Run every time a license file is added/modified within /vendor to
  26. # update /Godeps/LICENSES
  27. set -o errexit
  28. set -o nounset
  29. set -o pipefail
  30. export LANG=C
  31. export LC_ALL=C
  32. ###############################################################################
  33. # Process package content
  34. #
  35. # @param package The incoming package name
  36. # @param type The type of content (LICENSE, COPYRIGHT or COPYING)
  37. #
  38. process_content () {
  39. local package=$1
  40. local type=$2
  41. local package_root
  42. local ensure_pattern
  43. local dir_root
  44. local find_maxdepth
  45. local find_names
  46. local -a local_files=()
  47. # Necessary to expand {}
  48. case ${type} in
  49. LICENSE) find_names=(-iname 'licen[sc]e*')
  50. find_maxdepth=1
  51. # Sadly inconsistent in the wild, but mostly license files
  52. # containing copyrights, but no readme/notice files containing
  53. # licenses (except to "see license file")
  54. ensure_pattern="license|copyright"
  55. ;;
  56. # We search READMEs for copyrights and this includes notice files as well
  57. # Look in as many places as we find files matching
  58. COPYRIGHT) find_names=(-iname 'notice*' -o -iname 'readme*')
  59. find_maxdepth=3
  60. ensure_pattern="copyright"
  61. ;;
  62. COPYING) find_names=(-iname 'copying*')
  63. find_maxdepth=1
  64. ensure_pattern="license|copyright"
  65. ;;
  66. esac
  67. # Start search at package root
  68. case ${package} in
  69. github.com/*|golang.org/*|bitbucket.org/*|gonum.org/*)
  70. package_root=$(echo "${package}" |awk -F/ '{ print $1"/"$2"/"$3 }')
  71. ;;
  72. go4.org/*)
  73. package_root=$(echo "${package}" |awk -F/ '{ print $1 }')
  74. ;;
  75. gopkg.in/*)
  76. # Root of gopkg.in package always ends with '.v(number)' and my contain
  77. # more than two path elements. For example:
  78. # - gopkg.in/yaml.v2
  79. # - gopkg.in/inf.v0
  80. # - gopkg.in/square/go-jose.v2
  81. package_root=$(echo "${package}" |grep -oh '.*\.v[0-9]')
  82. ;;
  83. *)
  84. package_root=$(echo "${package}" |awk -F/ '{ print $1"/"$2 }')
  85. ;;
  86. esac
  87. # Find files - only root and package level
  88. local_files=()
  89. IFS=" " read -r -a local_files <<< "$(
  90. for dir_root in ${package} ${package_root}; do
  91. [[ -d ${DEPS_DIR}/${dir_root} ]] || continue
  92. # One (set) of these is fine
  93. find "${DEPS_DIR}/${dir_root}" \
  94. -xdev -follow -maxdepth ${find_maxdepth} \
  95. -type f "${find_names[@]}"
  96. done | sort -u)"
  97. local index
  98. local f
  99. index="${package}-${type}"
  100. if [[ -z "${CONTENT[${index}]-}" ]]; then
  101. for f in "${local_files[@]-}"; do
  102. if [[ -z "$f" ]]; then
  103. # Set the default value and then check it to prevent
  104. # accessing potentially empty array
  105. continue
  106. fi
  107. # Find some copyright info in any file and break
  108. if grep -E -i -wq "${ensure_pattern}" "${f}"; then
  109. CONTENT[${index}]="${f}"
  110. break
  111. fi
  112. done
  113. fi
  114. }
  115. #############################################################################
  116. # MAIN
  117. #############################################################################
  118. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  119. source "${KUBE_ROOT}/hack/lib/init.sh"
  120. export GO111MODULE=on
  121. # Check bash version
  122. if (( BASH_VERSINFO[0] < 4 )); then
  123. echo
  124. echo "ERROR: Bash v4+ required."
  125. # Extra help for OSX
  126. if [[ "$(uname -s)" == "Darwin" ]]; then
  127. echo
  128. echo "Ensure you are up to date on the following packages:"
  129. echo "$ brew install md5sha1sum bash jq"
  130. fi
  131. echo
  132. exit 9
  133. fi
  134. # This variable can be injected, as in the verify script.
  135. LICENSE_ROOT="${LICENSE_ROOT:-${KUBE_ROOT}}"
  136. cd "${LICENSE_ROOT}"
  137. VENDOR_LICENSE_FILE="Godeps/LICENSES"
  138. TMP_LICENSE_FILE="/tmp/Godeps.LICENSES.$$"
  139. DEPS_DIR="vendor"
  140. declare -Ag CONTENT
  141. # Put the K8S LICENSE on top
  142. (
  143. echo "================================================================================"
  144. echo "= Kubernetes licensed under: ="
  145. echo
  146. cat "${LICENSE_ROOT}/LICENSE"
  147. echo
  148. echo "= LICENSE $(md5sum < "${LICENSE_ROOT}/LICENSE" | awk '{print $1}')"
  149. echo "================================================================================"
  150. ) > ${TMP_LICENSE_FILE}
  151. # Loop through every vendored package
  152. for PACKAGE in $(go list -m -json all | jq -r .Path | sort -f); do
  153. if [[ -e "staging/src/${PACKAGE}" ]]; then
  154. echo "$PACKAGE is a staging package, skipping" > /dev/stderr
  155. continue
  156. fi
  157. if [[ ! -e "${DEPS_DIR}/${PACKAGE}" ]]; then
  158. echo "$PACKAGE doesn't exist in vendor, skipping" > /dev/stderr
  159. continue
  160. fi
  161. process_content "${PACKAGE}" LICENSE
  162. process_content "${PACKAGE}" COPYRIGHT
  163. process_content "${PACKAGE}" COPYING
  164. # display content
  165. echo
  166. echo "================================================================================"
  167. echo "= ${DEPS_DIR}/${PACKAGE} licensed under: ="
  168. echo
  169. file=""
  170. if [[ -n "${CONTENT[${PACKAGE}-LICENSE]-}" ]]; then
  171. file="${CONTENT[${PACKAGE}-LICENSE]-}"
  172. elif [[ -n "${CONTENT[${PACKAGE}-COPYRIGHT]-}" ]]; then
  173. file="${CONTENT[${PACKAGE}-COPYRIGHT]-}"
  174. elif [[ -n "${CONTENT[${PACKAGE}-COPYING]-}" ]]; then
  175. file="${CONTENT[${PACKAGE}-COPYING]-}"
  176. fi
  177. if [[ -z "${file}" ]]; then
  178. cat > /dev/stderr << __EOF__
  179. No license could be found for ${PACKAGE} - aborting.
  180. Options:
  181. 1. Check if the upstream repository has a newer version with LICENSE, COPYRIGHT and/or
  182. COPYING files.
  183. 2. Contact the author of the package to ensure there is a LICENSE, COPYRIGHT and/or
  184. COPYING file present.
  185. 3. Do not use this package in Kubernetes.
  186. __EOF__
  187. exit 9
  188. fi
  189. cat "${file}"
  190. echo
  191. echo "= ${file} $(md5sum < "${file}" | awk '{print $1}')"
  192. echo "================================================================================"
  193. echo
  194. done >> ${TMP_LICENSE_FILE}
  195. cat ${TMP_LICENSE_FILE} > ${VENDOR_LICENSE_FILE}