yapf_code.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. 'src/python'
  20. )
  21. EXCLUSIONS=(
  22. )
  23. VIRTUALENV=yapf_virtual_environment
  24. virtualenv $VIRTUALENV
  25. PYTHON=$(realpath "${VIRTUALENV}/bin/python")
  26. $PYTHON -m pip install --upgrade pip==9.0.1
  27. $PYTHON -m pip install --upgrade futures
  28. $PYTHON -m pip install yapf==0.16.0
  29. yapf() {
  30. local exclusion exclusion_args=()
  31. for exclusion in "${EXCLUSIONS[@]}"; do
  32. exclusion_args+=( "--exclude" "$1/${exclusion}" )
  33. done
  34. $PYTHON -m yapf -i -r --style=setup.cfg -p "${exclusion_args[@]}" "${1}"
  35. }
  36. if [[ -z "${TEST}" ]]; then
  37. for dir in "${DIRS[@]}"; do
  38. yapf "${dir}"
  39. done
  40. else
  41. ok=yes
  42. for dir in "${DIRS[@]}"; do
  43. tempdir=$(mktemp -d)
  44. cp -RT "${dir}" "${tempdir}"
  45. yapf "${tempdir}"
  46. diff -ru "${dir}" "${tempdir}" || ok=no
  47. rm -rf "${tempdir}"
  48. done
  49. if [[ ${ok} == no ]]; then
  50. false
  51. fi
  52. fi