| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Download files from Google Storage based on SHA1 sums.""" | 6 """Download files from Google Storage based on SHA1 sums.""" |
| 7 | 7 |
| 8 | 8 |
| 9 import hashlib | 9 import hashlib |
| 10 import optparse | 10 import optparse |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if status_code_match: | 103 if status_code_match: |
| 104 return (int(status_code_match.group(1)), out, err) | 104 return (int(status_code_match.group(1)), out, err) |
| 105 if ('You are attempting to access protected data with ' | 105 if ('You are attempting to access protected data with ' |
| 106 'no configured credentials.' in err): | 106 'no configured credentials.' in err): |
| 107 return (403, out, err) | 107 return (403, out, err) |
| 108 if 'No such object' in err: | 108 if 'No such object' in err: |
| 109 return (404, out, err) | 109 return (404, out, err) |
| 110 return (code, out, err) | 110 return (code, out, err) |
| 111 | 111 |
| 112 | 112 |
| 113 def check_bucket_permissions(base_url, gsutil): | 113 def check_bucket_permissions(bucket, gsutil): |
| 114 if not bucket: |
| 115 print >> sys.stderr, 'Missing bucket %s.' |
| 116 return (None, 1) |
| 117 base_url = 'gs://%s' % bucket |
| 118 |
| 114 code, _, ls_err = gsutil.check_call('ls', base_url) | 119 code, _, ls_err = gsutil.check_call('ls', base_url) |
| 115 if code != 0: | 120 if code != 0: |
| 116 print >> sys.stderr, ls_err | 121 print >> sys.stderr, ls_err |
| 117 if code == 403: | 122 if code == 403: |
| 118 print >> sys.stderr, 'Got error 403 while authenticating to %s.' % base_url | 123 print >> sys.stderr, 'Got error 403 while authenticating to %s.' % base_url |
| 119 print >> sys.stderr, 'Try running "download_from_google_storage --config".' | 124 print >> sys.stderr, 'Try running "download_from_google_storage --config".' |
| 120 elif code == 404: | 125 elif code == 404: |
| 121 print >> sys.stderr, '%s not found.' % base_url | 126 print >> sys.stderr, '%s not found.' % base_url |
| 122 return (base_url, code) | 127 return (base_url, code) |
| 123 | 128 |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 options.output = input_filename[:-5] | 450 options.output = input_filename[:-5] |
| 446 else: | 451 else: |
| 447 parser.error('Unreachable state.') | 452 parser.error('Unreachable state.') |
| 448 | 453 |
| 449 # Check if output file already exists. | 454 # Check if output file already exists. |
| 450 if not options.directory and not options.force and not options.no_resume: | 455 if not options.directory and not options.force and not options.no_resume: |
| 451 if os.path.exists(options.output): | 456 if os.path.exists(options.output): |
| 452 parser.error('Output file %s exists and --no_resume is specified.' | 457 parser.error('Output file %s exists and --no_resume is specified.' |
| 453 % options.output) | 458 % options.output) |
| 454 | 459 |
| 455 base_url = 'gs://%s' % options.bucket | |
| 456 | |
| 457 # Check we have a valid bucket with valid permissions. | 460 # Check we have a valid bucket with valid permissions. |
| 458 if not options.no_auth: | 461 base_url, code = check_bucket_permissions(options.bucket, gsutil) |
| 459 code = check_bucket_permissions(base_url, gsutil, options.no_auth) | 462 if code: |
| 460 if code: | 463 return code |
| 461 return code | |
| 462 | 464 |
| 463 return download_from_google_storage( | 465 return download_from_google_storage( |
| 464 input_filename, base_url, gsutil, options.num_threads, options.directory, | 466 input_filename, base_url, gsutil, options.num_threads, options.directory, |
| 465 options.recursive, options.force, options.output, options.ignore_errors, | 467 options.recursive, options.force, options.output, options.ignore_errors, |
| 466 options.sha1_file, options.verbose, options.auto_platform) | 468 options.sha1_file, options.verbose, options.auto_platform) |
| 467 | 469 |
| 468 | 470 |
| 469 if __name__ == '__main__': | 471 if __name__ == '__main__': |
| 470 sys.exit(main(sys.argv)) | 472 sys.exit(main(sys.argv)) |
| OLD | NEW |