Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / tools / run_tests / python_utils / download_and_unzip.py
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
16 from __future__ import print_function
17
18 import os
19 import sys
20 import zipfile
21 import requests
22 import tempfile
23
24
25 def main():
26     if len(sys.argv) != 3:
27         print("Usage: python download_and_unzip.py [zipfile-url] [destination]")
28         sys.exit(1)
29     download_url = sys.argv[1]
30     destination = sys.argv[2]
31
32     with tempfile.TemporaryFile() as tmp_file:
33         r = requests.get(download_url)
34         if r.status_code != requests.codes.ok:
35             print("Download %s failed with [%d] \"%s\"" %
36                   (download_url, r.status_code, r.text()))
37             sys.exit(1)
38         else:
39             tmp_file.write(r.content)
40             print("Successfully downloaded from %s", download_url)
41         with zipfile.ZipFile(tmp_file, 'r') as target_zip_file:
42             target_zip_file.extractall(destination)
43         print("Successfully unzip to %s" % destination)
44
45
46 if __name__ == "__main__":
47     main()