Debian Codegraph @

tools/distrib/buildozer.sh

raw
1 #! /bin/bash
2 # Copyright 2019 The 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
16 set -e
17
18 BUILDOZER_VERSION="4.2.2"
19 TEMP_BUILDOZER_PATH="/tmp/buildozer-for-grpc"
20
21 MAX_DOWNLOAD_RETRY=5
22 DOWNLOAD_WAITING_INTERVAL_SECS=10
23
24 function error_handling() {
25 error=$1
26 if [[ -n "$error" ]]; then
27 echo "${error}"
28 exit 1
29 fi
30 }
31
32 function download_buildozer() {
33 platform="$(uname -s)"
34 case "${platform}" in
35 Linux*) download_link="https://github.com/bazelbuild/buildtools/releases/download/${BUILDOZER_VERSION}/buildozer-linux-amd64";;
36 Darwin*) download_link="https://github.com/bazelbuild/buildtools/releases/download/${BUILDOZER_VERSION}/buildozer-darwin-amd64";;
37 *) error_handling "Unsupported platform: ${platform}";;
38 esac
39
40 download_success=0
41 for i in $(seq 1 $MAX_DOWNLOAD_RETRY); do
42 if [ -x "$(command -v curl)" ]; then
43 http_code=`curl -L -o ${TEMP_BUILDOZER_PATH} -w "%{http_code}" ${download_link}`
44 if [ $http_code -eq "200" ]; then
45 download_success=1
46 fi
47 elif [ -x "$(command -v wget)" ]; then
48 wget -S -O ${TEMP_BUILDOZER_PATH} ${download_link} 2>&1 | grep "200 OK" && download_success=1
49 else
50 error_handling "Download failed: curl and wget not available"
51 fi
52
53 if [ $download_success -eq 1 ]; then
54 break
55 elif [ $i -lt $MAX_DOWNLOAD_RETRY ]; then
56 echo "Failed to download buildozer: retrying in $DOWNLOAD_WAITING_INTERVAL_SECS secs"
57 sleep $DOWNLOAD_WAITING_INTERVAL_SECS
58 fi
59 done
60
61 if [ $download_success -ne 1 ]; then
62 error_handling "Failed to download buildozer after $MAX_DOWNLOAD_RETRY tries"
63 fi
64
65 chmod +x ${TEMP_BUILDOZER_PATH}
66 }
67
68
69 # Get the correct version of buildozer
70 if [ -x "$(command -v buildozer)" ]; then
71 existing_buildozer_version="$(buildozer -version 2>&1 | head -n1 | cut -d" " -f3)"
72 if [[ "${existing_buildozer_version}" != "${BUILDOZER_VERSION}" ]]; then
73 download_buildozer
74 buildozer_bin="${TEMP_BUILDOZER_PATH}"
75 else
76 buildozer_bin="buildozer"
77 fi
78 else
79 if [ -x ${TEMP_BUILDOZER_PATH} ]; then
80 existing_buildozer_version="$(${TEMP_BUILDOZER_PATH} -version 2>&1 | head -n1 | cut -d" " -f3)"
81 if [[ "${existing_buildozer_version}" != "${BUILDOZER_VERSION}" ]]; then
82 download_buildozer
83 fi
84 else
85 download_buildozer
86 fi
87 buildozer_bin="${TEMP_BUILDOZER_PATH}"
88 fi
89
90 # cd to repo root
91 dir=$(dirname "${0}")
92 cd "${dir}/../.."
93
94 set -ex
95
96 # shellcheck disable=SC2086,SC2068
97 ${buildozer_bin} "$@"