build_wheel.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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. #=================================================
  16. # Utils
  17. #=================================================
  18. # directory config
  19. DIST_DIR="dist"
  20. BUILD_DIR="build"
  21. EGG_DIR="paddledet.egg-info"
  22. CFG_DIR="configs"
  23. TEST_DIR=".tests"
  24. # command line log config
  25. RED='\033[0;31m'
  26. BLUE='\033[0;34m'
  27. GREEN='\033[1;32m'
  28. BOLD='\033[1m'
  29. NONE='\033[0m'
  30. function python_version_check() {
  31. PY_MAIN_VERSION=`python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1}'`
  32. PY_SUB_VERSION=`python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $2}'`
  33. echo -e "find python version ${PY_MAIN_VERSION}.${PY_SUB_VERSION}"
  34. if [ $PY_MAIN_VERSION -ne "3" -o $PY_SUB_VERSION -lt "5" ]; then
  35. echo -e "${RED}FAIL:${NONE} please use Python >= 3.5 !"
  36. exit 1
  37. fi
  38. }
  39. function init() {
  40. echo -e "${BLUE}[init]${NONE} removing building directory..."
  41. rm -rf $DIST_DIR $BUILD_DIR $EGG_DIR $TEST_DIR
  42. if [ `pip list | grep paddledet | wc -l` -gt 0 ]; then
  43. echo -e "${BLUE}[init]${NONE} uninstalling paddledet..."
  44. pip uninstall -y paddledet
  45. fi
  46. echo -e "${BLUE}[init]${NONE} ${GREEN}init success\n"
  47. }
  48. function build_and_install() {
  49. echo -e "${BLUE}[build]${NONE} building paddledet wheel..."
  50. python setup.py sdist bdist_wheel
  51. if [ $? -ne 0 ]; then
  52. echo -e "${RED}[FAIL]${NONE} build paddledet wheel failed !"
  53. exit 1
  54. fi
  55. echo -e "${BLUE}[build]${NONE} ${GREEN}build paddldet wheel success\n"
  56. echo -e "${BLUE}[install]${NONE} installing paddledet..."
  57. cd $DIST_DIR
  58. find . -name "paddledet*.whl" | xargs pip install
  59. if [ $? -ne 0 ]; then
  60. cd ..
  61. echo -e "${RED}[FAIL]${NONE} install paddledet wheel failed !"
  62. exit 1
  63. fi
  64. echo -e "${BLUE}[install]${NONE} ${GREEN}paddledet install success\n"
  65. cd ..
  66. }
  67. function unittest() {
  68. if [ -d $TEST_DIR ]; then
  69. rm -rf $TEST_DIR
  70. fi;
  71. echo -e "${BLUE}[unittest]${NONE} run unittests..."
  72. # NOTE: perform unittests under TEST_DIR to
  73. # make sure installed paddledet is used
  74. mkdir $TEST_DIR
  75. cp -r $CFG_DIR $TEST_DIR
  76. cd $TEST_DIR
  77. if [ $? != 0 ]; then
  78. exit 1
  79. fi
  80. find "../ppdet" -wholename '*tests/test_*' -type f -print0 | \
  81. xargs -0 -I{} -n1 -t bash -c 'python -u -s {}'
  82. # clean TEST_DIR
  83. cd ..
  84. rm -rf $TEST_DIR
  85. echo -e "${BLUE}[unittest]${NONE} ${GREEN}unittests success\n${NONE}"
  86. }
  87. function cleanup() {
  88. if [ -d $TEST_DIR ]; then
  89. rm -rf $TEST_DIR
  90. fi
  91. rm -rf $BUILD_DIR $EGG_DIR
  92. pip uninstall -y paddledet
  93. }
  94. function abort() {
  95. echo -e "${RED}[FAIL]${NONE} build wheel and unittest failed !
  96. please check your code" 1>&2
  97. cur_dir=`basename "$pwd"`
  98. if [ cur_dir==$TEST_DIR -o cur_dir==$DIST_DIR ]; then
  99. cd ..
  100. fi
  101. rm -rf $BUILD_DIR $EGG_DIR $DIST_DIR $TEST_DIR
  102. pip uninstall -y paddledet
  103. }
  104. python_version_check
  105. trap 'abort' 0
  106. set -e
  107. init
  108. build_and_install
  109. unittest
  110. cleanup
  111. # get Paddle version
  112. PADDLE_VERSION=`python -c "import paddle; print(paddle.version.full_version)"`
  113. PADDLE_COMMIT=`python -c "import paddle; print(paddle.version.commit)"`
  114. PADDLE_COMMIT=`git rev-parse --short $PADDLE_COMMIT`
  115. # get PaddleDetection branch
  116. PPDET_BRANCH=`git rev-parse --abbrev-ref HEAD`
  117. PPDET_COMMIT=`git rev-parse --short HEAD`
  118. # get Python version
  119. PYTHON_VERSION=`python -c "import platform; print(platform.python_version())"`
  120. echo -e "\n${GREEN}paddledet wheel compiled and checked success !${NONE}
  121. ${BLUE}Python version:${NONE} $PYTHON_VERSION
  122. ${BLUE}Paddle version:${NONE} $PADDLE_VERSION ($PADDLE_COMMIT)
  123. ${BLUE}PaddleDetection branch:${NONE} $PPDET_BRANCH ($PPDET_COMMIT)\n"
  124. echo -e "${GREEN}wheel saved under${NONE} ${RED}${BOLD}./dist"
  125. trap : 0