build_wheel.sh 4.2 KB

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