You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

update-vendor-licenses.sh 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 LICENSES directory.
  16. # Generates a table of Go dependencies and their licenses.
  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 /LICENSES
  27. #
  28. # -----------------------------------------------------------------------------
  29. # CHANGELOG
  30. # KubeEdge Authors:
  31. # - File derived from kubernetes v1.19.0-beta.2
  32. # - Changed KUBE_ROOT value to use absolute path
  33. # - skip license for k8s.io/klog
  34. set -o errexit
  35. set -o nounset
  36. set -o pipefail
  37. KUBE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
  38. source "${KUBE_ROOT}/hack/lib/util.sh"
  39. export LANG=C
  40. export LC_ALL=C
  41. ###############################################################################
  42. # Process package content
  43. #
  44. # @param package The incoming package name
  45. # @param type The type of content (LICENSE, COPYRIGHT or COPYING)
  46. #
  47. process_content () {
  48. local package=$1
  49. local type=$2
  50. local package_root
  51. local ensure_pattern
  52. local dir_root
  53. local find_maxdepth
  54. local find_names
  55. local -a local_files=()
  56. # Necessary to expand {}
  57. case ${type} in
  58. LICENSE) find_names=(-iname 'licen[sc]e*')
  59. find_maxdepth=1
  60. # Sadly inconsistent in the wild, but mostly license files
  61. # containing copyrights, but no readme/notice files containing
  62. # licenses (except to "see license file")
  63. ensure_pattern="license|copyright"
  64. ;;
  65. # We search READMEs for copyrights and this includes notice files as well
  66. # Look in as many places as we find files matching
  67. COPYRIGHT) find_names=(-iname 'notice*' -o -iname 'readme*')
  68. find_maxdepth=3
  69. ensure_pattern="copyright"
  70. ;;
  71. COPYING) find_names=(-iname 'copying*')
  72. find_maxdepth=1
  73. ensure_pattern="license|copyright"
  74. ;;
  75. esac
  76. # Start search at package root
  77. case ${package} in
  78. github.com/*|golang.org/*|bitbucket.org/*|gonum.org/*)
  79. package_root=$(echo "${package}" |awk -F/ '{ print $1"/"$2"/"$3 }')
  80. ;;
  81. go4.org/*)
  82. package_root=$(echo "${package}" |awk -F/ '{ print $1 }')
  83. ;;
  84. gopkg.in/*)
  85. # Root of gopkg.in package always ends with '.v(number)' and my contain
  86. # more than two path elements. For example:
  87. # - gopkg.in/yaml.v2
  88. # - gopkg.in/inf.v0
  89. # - gopkg.in/square/go-jose.v2
  90. package_root=$(echo "${package}" |grep -oh '.*\.v[0-9]')
  91. ;;
  92. */*)
  93. package_root=$(echo "${package}" |awk -F/ '{ print $1"/"$2 }')
  94. ;;
  95. *)
  96. package_root="${package}"
  97. ;;
  98. esac
  99. # Find files - only root and package level
  100. local_files=()
  101. IFS=" " read -r -a local_files <<< "$(
  102. for dir_root in ${package} ${package_root}; do
  103. [[ -d ${DEPS_DIR}/${dir_root} ]] || continue
  104. # One (set) of these is fine
  105. find "${DEPS_DIR}/${dir_root}" \
  106. -xdev -follow -maxdepth ${find_maxdepth} \
  107. -type f "${find_names[@]}"
  108. done | sort -u)"
  109. local index
  110. local f
  111. index="${package}-${type}"
  112. if [[ -z "${CONTENT[${index}]-}" ]]; then
  113. for f in "${local_files[@]-}"; do
  114. if [[ -z "$f" ]]; then
  115. # Set the default value and then check it to prevent
  116. # accessing potentially empty array
  117. continue
  118. fi
  119. # Find some copyright info in any file and break
  120. if grep -E -i -wq "${ensure_pattern}" "${f}"; then
  121. CONTENT[${index}]="${f}"
  122. break
  123. fi
  124. done
  125. fi
  126. }
  127. #############################################################################
  128. # MAIN
  129. #############################################################################
  130. export GO111MODULE=on
  131. export GOFLAGS=-mod=mod
  132. # Check bash version
  133. if (( BASH_VERSINFO[0] < 4 )); then
  134. echo
  135. echo "ERROR: Bash v4+ required."
  136. # Extra help for OSX
  137. if [[ "$(uname -s)" == "Darwin" ]]; then
  138. echo
  139. echo "Ensure you are up to date on the following packages:"
  140. echo "$ brew install md5sha1sum bash jq"
  141. fi
  142. echo
  143. exit 9
  144. fi
  145. # This variable can be injected, as in the verify script.
  146. LICENSE_ROOT="${LICENSE_ROOT:-${KUBE_ROOT}}"
  147. cd "${LICENSE_ROOT}"
  148. kube::util::ensure-temp-dir
  149. # Save the genreated LICENSE file for each package temporarily
  150. TMP_LICENSE_FILE="${KUBE_TEMP}/LICENSES.$$"
  151. # The directory to save all the LICENSE files
  152. LICENSES_DIR="${LICENSES_DIR:-${LICENSE_ROOT}/LICENSES}"
  153. mkdir -p "${LICENSES_DIR}"
  154. # The tmp directory to save all the LICENSE files, will move to LICENSES_DIR
  155. TMP_LICENSES_DIR="${KUBE_TEMP}/LICENSES.DIR.$$"
  156. mkdir -p "${TMP_LICENSES_DIR}"
  157. DEPS_DIR="vendor"
  158. declare -Ag CONTENT
  159. # Put the K8S LICENSE on top
  160. if [ -f "${LICENSE_ROOT}/LICENSE" ]; then
  161. (
  162. echo "================================================================================"
  163. echo "= Sedna licensed under: ="
  164. echo
  165. cat "${LICENSE_ROOT}/LICENSE"
  166. echo
  167. echo "= LICENSE $(kube::util::md5 "${LICENSE_ROOT}/LICENSE")"
  168. echo "================================================================================"
  169. ) > "${TMP_LICENSE_FILE}"
  170. mv "${TMP_LICENSE_FILE}" "${TMP_LICENSES_DIR}/LICENSE"
  171. fi
  172. # Loop through every vendored package
  173. for PACKAGE in $(go list -m -json all | jq -r .Path | sort -f); do
  174. if [[ -e "staging/src/${PACKAGE}" ]]; then
  175. echo "${PACKAGE} is a staging package, skipping" >&2
  176. continue
  177. fi
  178. if [[ ! -e "${DEPS_DIR}/${PACKAGE}" ]]; then
  179. echo "${PACKAGE} doesn't exist in ${DEPS_DIR}, skipping" >&2
  180. continue
  181. fi
  182. if [[ "${PACKAGE}" = "sigs.k8s.io/structured-merge-diff" ]]; then
  183. # this package doesn't exist, but has v3 subdirectory as a different package
  184. # so it can't be filtered by the previous rule
  185. # temporarily treat this way until find out a better rule
  186. echo "${PACKAGE}, temporarily skipping" >&2
  187. continue
  188. fi
  189. if [[ "${PACKAGE}" = "github.com/cespare/xxhash" ]]; then
  190. # there are 2 versions v1 and v2 under 2 folders indirectly used
  191. # so it can't be filtered by the previous rule
  192. # temporarily treat this way until find out a better rule
  193. echo "${PACKAGE}, temporarily skipping" >&2
  194. continue
  195. fi
  196. if [[ "${PACKAGE}" = "k8s.io/klog" ]]; then
  197. # this package doesn't use, but has v2 subdirectory as a different package
  198. # so it can't be filtered by the previous rule
  199. # temporarily treat this way until find out a better rule
  200. echo "${PACKAGE}, temporarily skipping" >&2
  201. continue
  202. fi
  203. if [[ "${PACKAGE}" = "github.com/emicklei/go-restful" ]]; then
  204. # this package doesn't use, but has v3 subdirectory as a different package
  205. # so it can't be filtered by the previous rule
  206. # temporarily treat this way until find out a better rule
  207. echo "${PACKAGE}, temporarily skipping" >&2
  208. continue
  209. fi
  210. echo "${PACKAGE}"
  211. process_content "${PACKAGE}" LICENSE
  212. process_content "${PACKAGE}" COPYRIGHT
  213. process_content "${PACKAGE}" COPYING
  214. # copy content and throw error message
  215. {
  216. echo "= ${DEPS_DIR}/${PACKAGE} licensed under: ="
  217. echo
  218. file=""
  219. if [[ -n "${CONTENT[${PACKAGE}-LICENSE]-}" ]]; then
  220. file="${CONTENT[${PACKAGE}-LICENSE]-}"
  221. elif [[ -n "${CONTENT[${PACKAGE}-COPYRIGHT]-}" ]]; then
  222. file="${CONTENT[${PACKAGE}-COPYRIGHT]-}"
  223. elif [[ -n "${CONTENT[${PACKAGE}-COPYING]-}" ]]; then
  224. file="${CONTENT[${PACKAGE}-COPYING]-}"
  225. fi
  226. if [[ -z "${file}" ]]; then
  227. cat >&2 << __EOF__
  228. No license could be found for ${PACKAGE} - aborting.
  229. Options:
  230. 1. Check if the upstream repository has a newer version with LICENSE, COPYRIGHT and/or
  231. COPYING files.
  232. 2. Contact the author of the package to ensure there is a LICENSE, COPYRIGHT and/or
  233. COPYING file present.
  234. 3. Do not use this package in KubeEdge.
  235. __EOF__
  236. exit 9
  237. fi
  238. cat "${file}"
  239. echo
  240. echo "= ${file} $(kube::util::md5 "${file}")"
  241. } >> "${TMP_LICENSE_FILE}"
  242. dest_dir="${TMP_LICENSES_DIR}/vendor/${PACKAGE}"
  243. mkdir -p "${dest_dir}"
  244. mv "${TMP_LICENSE_FILE}" "${dest_dir}/LICENSE"
  245. done
  246. # Leave things like OWNERS alone.
  247. rm -f "${LICENSES_DIR}/LICENSE"
  248. rm -rf "${LICENSES_DIR}/vendor"
  249. mv "${TMP_LICENSES_DIR}"/* "${LICENSES_DIR}"