Debian Codegraph @

tools/run_tests/python_utils/download_and_unzip.py

raw
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 tempfile
21 import zipfile
22
23 import requests
24
25
26 def 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("Download %s failed with [%d] \"%s\"" %
37 (download_url, r.status_code, r.text()))
38 sys.exit(1)
39 else:
40 tmp_file.write(r.content)
41 print("Successfully downloaded from %s", download_url)
42 with zipfile.ZipFile(tmp_file, 'r') as target_zip_file:
43 target_zip_file.extractall(destination)
44 print("Successfully unzip to %s" % destination)
45
46
47 if __name__ == "__main__":
48 main()