| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 #FIXME | 6 """Script for downloading a file from Google Cloud Storage.""" |
| 7 |
| 7 | 8 |
| 8 import argparse | 9 import argparse |
| 9 import hashlib | 10 import hashlib |
| 10 import httplib | 11 import httplib |
| 11 import os | 12 import os |
| 12 import sys | 13 import sys |
| 13 | 14 |
| 14 | 15 |
| 15 def _FatalError(message): | 16 def _FatalError(message): |
| 16 print >> sys.stderr, "%s: fatal error: %s" % (os.path.basename(sys.argv[0]), | 17 print >> sys.stderr, "%s: fatal error: %s" % (os.path.basename(sys.argv[0]), |
| 17 message) | 18 message) |
| 18 | 19 |
| 19 | 20 |
| 20 def _DownloadFile(source): | 21 def _DownloadFile(source): |
| 21 """FIXME""" | 22 """Downloads |source| from Google Cloud Storage. |source| should be a path, |
| 23 including the bucket name (e.g., if the full URL is gs://mojo/a/b/c.xyz, |
| 24 |source| should be "mojo/a/b/c.xyz").""" |
| 22 | 25 |
| 23 try: | 26 try: |
| 24 conn = httplib.HTTPSConnection("storage.googleapis.com") | 27 conn = httplib.HTTPSConnection("storage.googleapis.com") |
| 25 conn.request("GET", "/" + source) | 28 conn.request("GET", "/" + source) |
| 26 resp = conn.getresponse() | 29 resp = conn.getresponse() |
| 27 if resp.status != httplib.OK: | 30 if resp.status != httplib.OK: |
| 28 _FatalError("HTTP status: %s" % resp.reason) | 31 _FatalError("HTTP status: %s" % resp.reason) |
| 29 data = resp.read() | 32 data = resp.read() |
| 30 conn.close() | 33 conn.close() |
| 31 return data | 34 return data |
| 32 except httplib.HTTPException as e: | 35 except httplib.HTTPException as e: |
| 33 _FatalError("HTTP exception: %s" % str(e)) | 36 _FatalError("HTTP exception: %s" % str(e)) |
| 34 | 37 |
| 35 | 38 |
| 36 def main(): | 39 def main(): |
| 37 parser = argparse.ArgumentParser( | 40 parser = argparse.ArgumentParser( |
| 38 description="Downloads a file from Google Cloud Storage.") | 41 description="Downloads a file from Google Cloud Storage.") |
| 39 parser.add_argument("--sha1-hash", dest="sha1_hash", | 42 parser.add_argument("--sha1-hash", dest="sha1_hash", |
| 40 help="SHA-1 hash for the downloaded file") | 43 help="SHA-1 hash for the downloaded file") |
| 41 parser.add_argument("--executable", action="store_true", | 44 parser.add_argument("--executable", action="store_true", |
| 42 help="make the downloaded file executable") | 45 help="make the downloaded file executable") |
| 43 parser.add_argument("source", help="source path, including bucket name") | 46 parser.add_argument("source", help="source path, including the bucket name") |
| 44 parser.add_argument("destination", help="destination path") | 47 parser.add_argument("destination", help="destination path") |
| 45 args = parser.parse_args() | 48 args = parser.parse_args() |
| 46 | 49 |
| 47 bits = _DownloadFile(args.source) | 50 bits = _DownloadFile(args.source) |
| 48 if args.sha1_hash: | 51 if args.sha1_hash: |
| 49 got_sha1_hash = hashlib.sha1(bits).hexdigest().lower() | 52 got_sha1_hash = hashlib.sha1(bits).hexdigest().lower() |
| 50 expected_sha1_hash = args.sha1_hash.lower() | 53 expected_sha1_hash = args.sha1_hash.lower() |
| 51 if got_sha1_hash != expected_sha1_hash: | 54 if got_sha1_hash != expected_sha1_hash: |
| 52 _FatalError("SHA-1 hash did not match: got %s, expected %s" % | 55 _FatalError("SHA-1 hash did not match: got %s, expected %s" % |
| 53 (got_sha1_hash, expected_sha1_hash)) | 56 (got_sha1_hash, expected_sha1_hash)) |
| 54 | 57 |
| 55 with open(args.destination, "wb") as f: | 58 with open(args.destination, "wb") as f: |
| 56 f.write(bits) | 59 f.write(bits) |
| 57 | 60 |
| 58 if args.executable: | 61 if args.executable: |
| 59 curr_mode = os.stat(args.destination).st_mode | 62 curr_mode = os.stat(args.destination).st_mode |
| 60 # Set the x bits (0111) where the r bits (0444) are set. | 63 # Set the x bits (0111) where the r bits (0444) are set. |
| 61 new_mode = curr_mode | ((curr_mode & 0444) >> 2) | 64 new_mode = curr_mode | ((curr_mode & 0444) >> 2) |
| 62 os.chmod(args.destination, new_mode) | 65 os.chmod(args.destination, new_mode) |
| 63 | 66 |
| 64 return 0 | 67 return 0 |
| 65 | 68 |
| 66 | 69 |
| 67 if __name__ == "__main__": | 70 if __name__ == "__main__": |
| 68 sys.exit(main()) | 71 sys.exit(main()) |
| OLD | NEW |