yapf_code.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. # Copyright 2015 gRPC 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. set -ex
  16. # change to root directory
  17. cd "$(dirname "${0}")/../.."
  18. DIRS=(
  19. 'examples/python'
  20. 'src/python'
  21. 'tools'
  22. )
  23. VIRTUALENV=yapf_virtual_environment
  24. python3 -m virtualenv $VIRTUALENV -p $(which python3)
  25. PYTHON=${VIRTUALENV}/bin/python
  26. "$PYTHON" -m pip install --upgrade pip==19.3.1
  27. "$PYTHON" -m pip install --upgrade futures
  28. "$PYTHON" -m pip install yapf==0.28.0
  29. yapf() {
  30. $PYTHON -m yapf -i -r --style=setup.cfg "${1}"
  31. }
  32. if [[ -z "${TEST}" ]]; then
  33. for dir in "${DIRS[@]}"; do
  34. yapf "${dir}"
  35. done
  36. else
  37. ok=yes
  38. for dir in "${DIRS[@]}"; do
  39. tempdir=$(mktemp -d)
  40. cp -RT "${dir}" "${tempdir}"
  41. yapf "${tempdir}"
  42. diff -x '*.pyc' -ru "${dir}" "${tempdir}" || ok=no
  43. rm -rf "${tempdir}"
  44. done
  45. if [[ ${ok} == no ]]; then
  46. false
  47. fi
  48. fi