1#!/usr/bin/env bash
2# Copyright 2021 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 -eo pipefail
17
18display_usage() {
19  cat <<EOF >/dev/stderr
20A helper to run yapf formatter.
21
22USAGE: $0 [--diff]
23   --diff: Do not apply changes, only show the diff
24
25ENVIRONMENT:
26   XDS_K8S_DRIVER_VENV_DIR: the path to python virtual environment directory
27                            Default: $XDS_K8S_DRIVER_DIR/venv
28EXAMPLES:
29$0
30$0 --diff
31EOF
32  exit 1
33}
34
35if [[ "$1" == "-h" || "$1" == "--help" ]]; then
36  display_usage
37fi
38
39SCRIPT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
40readonly SCRIPT_DIR
41readonly XDS_K8S_DRIVER_DIR="${SCRIPT_DIR}/.."
42
43cd "${XDS_K8S_DRIVER_DIR}"
44
45# Relative paths not yet supported by shellcheck.
46# shellcheck source=/dev/null
47source "${XDS_K8S_DRIVER_DIR}/bin/ensure_venv.sh"
48
49if [[ "$1" == "--diff" ]]; then
50  readonly MODE="--diff"
51else
52  readonly MODE="--in-place"
53  readonly VERBOSE="--verbose" # print out file names while processing
54fi
55
56exec python -m yapf "${MODE}" ${VERBOSE:-} \
57  --parallel --recursive --style=../../../setup.cfg \
58  framework bin tests
59