blob: 27c5e3129ddb584ee930f101154388b438259fcf [file] [log] [blame]
Masood Malekghassemi06dea572016-05-11 12:01:40 -07001#!/bin/bash
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2015 gRPC authors.
Masood Malekghassemi06dea572016-05-11 12:01:40 -07003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# 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
Masood Malekghassemi06dea572016-05-11 12:01:40 -07007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Masood Malekghassemi06dea572016-05-11 12:01:40 -07009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# 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.
Masood Malekghassemi06dea572016-05-11 12:01:40 -070015
16set -ex
17
18# change to root directory
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000019cd "$(dirname "${0}")/../.."
Masood Malekghassemi06dea572016-05-11 12:01:40 -070020
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000021DIRS=(
ncteisen848a7492017-12-12 10:31:47 -080022 'examples/python'
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000023 'src/python'
ncteisen5f8bf792017-12-11 18:09:31 -080024 'tools'
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000025)
26EXCLUSIONS=(
ncteisenacbf0652017-12-12 10:58:19 -080027 '*protoc_lib_deps.py' # this file is auto-generated
ncteisenc47c5892017-12-12 12:45:56 -080028 '*_pb2*.py' # no need to format protoc generated files
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000029)
Masood Malekghassemi06dea572016-05-11 12:01:40 -070030
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000031VIRTUALENV=yapf_virtual_environment
Masood Malekghassemi06dea572016-05-11 12:01:40 -070032
Mehrdad Afshari700d0502018-04-27 07:01:00 -070033python -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
Masood Malekghassemi06dea572016-05-11 12:01:40 -070038
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000039yapf() {
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}
Masood Malekghassemi06dea572016-05-11 12:01:40 -070046
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000047if [[ -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}"
Ken Paysonbc165842017-12-13 13:44:34 -080057 diff -x '*.pyc' -ru "${dir}" "${tempdir}" || ok=no
Nathaniel Manista9f1e5ab2017-03-02 23:29:52 +000058 rm -rf "${tempdir}"
59 done
60 if [[ ${ok} == no ]]; then
61 false
62 fi
63fi