1# Copyright 2020 The gRPC Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Download and unzip the target file to the destination.""" 15 16from __future__ import print_function 17 18import os 19import sys 20import tempfile 21import zipfile 22 23import requests 24 25 26def main(): 27 if len(sys.argv) != 3: 28 print("Usage: python download_and_unzip.py [zipfile-url] [destination]") 29 sys.exit(1) 30 download_url = sys.argv[1] 31 destination = sys.argv[2] 32 33 with tempfile.TemporaryFile() as tmp_file: 34 r = requests.get(download_url) 35 if r.status_code != requests.codes.ok: 36 print( 37 'Download %s failed with [%d] "%s"' 38 % (download_url, r.status_code, r.text()) 39 ) 40 sys.exit(1) 41 else: 42 tmp_file.write(r.content) 43 print("Successfully downloaded from %s", download_url) 44 with zipfile.ZipFile(tmp_file, "r") as target_zip_file: 45 target_zip_file.extractall(destination) 46 print("Successfully unzip to %s" % destination) 47 48 49if __name__ == "__main__": 50 main() 51