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 16set -ex 17 18# change to root directory 19cd "$(dirname "${0}")/../.." 20 21DIRS=( 22 'examples/python' 23 'src/python' 24 'tools' 25) 26EXCLUSIONS=( 27 '*protoc_lib_deps.py' # this file is auto-generated 28 '*_pb2*.py' # no need to format protoc generated files 29) 30 31VIRTUALENV=yapf_virtual_environment 32 33python -m virtualenv $VIRTUALENV 34PYTHON=${VIRTUALENV}/bin/python 35"$PYTHON" -m pip install --upgrade pip==10.0.1 36"$PYTHON" -m pip install --upgrade futures 37"$PYTHON" -m pip install yapf==0.20.0 38 39yapf() { 40 local exclusion exclusion_args=() 41 for exclusion in "${EXCLUSIONS[@]}"; do 42 exclusion_args+=( "--exclude" "$1/${exclusion}" ) 43 done 44 $PYTHON -m yapf -i -r --style=setup.cfg -p "${exclusion_args[@]}" "${1}" 45} 46 47if [[ -z "${TEST}" ]]; then 48 for dir in "${DIRS[@]}"; do 49 yapf "${dir}" 50 done 51else 52 ok=yes 53 for dir in "${DIRS[@]}"; do 54 tempdir=$(mktemp -d) 55 cp -RT "${dir}" "${tempdir}" 56 yapf "${tempdir}" 57 diff -x '*.pyc' -ru "${dir}" "${tempdir}" || ok=no 58 rm -rf "${tempdir}" 59 done 60 if [[ ${ok} == no ]]; then 61 false 62 fi 63fi 64