download_and_unzip.py 1.5 KB

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