package-check.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2019 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. set -e
  17. if [[ $# -le 1 ]]; then
  18. cat <<EOF
  19. Usage:
  20. package-check.sh <jar-file> <package-list>
  21. Checks that the class files in the <jar file> are in the <package-list> or
  22. sub-packages.
  23. EOF
  24. exit 1
  25. fi
  26. jar_file=$1
  27. shift
  28. if [[ ! -f ${jar_file} ]]; then
  29. echo "jar file \"${jar_file}\" does not exist."
  30. exit 1
  31. fi
  32. prefixes=()
  33. while [[ $# -ge 1 ]]; do
  34. package="$1"
  35. if [[ "${package}" = */* ]]; then
  36. echo "Invalid package \"${package}\". Use dot notation for packages."
  37. exit 1
  38. fi
  39. # Transform to a slash-separated path and add a trailing slash to enforce
  40. # package name boundary.
  41. prefixes+=("${package//\.//}/")
  42. shift
  43. done
  44. # Get the file names from the jar file.
  45. zip_contents=`zipinfo -1 $jar_file`
  46. # Check all class file names against the expected prefixes.
  47. old_ifs=${IFS}
  48. IFS=$'\n'
  49. failed=false
  50. for zip_entry in ${zip_contents}; do
  51. # Check the suffix.
  52. if [[ "${zip_entry}" = *.class ]]; then
  53. # Match against prefixes.
  54. found=false
  55. for prefix in ${prefixes[@]}; do
  56. if [[ "${zip_entry}" = "${prefix}"* ]]; then
  57. found=true
  58. break
  59. fi
  60. done
  61. if [[ "${found}" == "false" ]]; then
  62. echo "Class file ${zip_entry} is outside specified packages."
  63. failed=true
  64. fi
  65. fi
  66. done
  67. if [[ "${failed}" == "true" ]]; then
  68. exit 1
  69. fi
  70. IFS=${old_ifs}