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