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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 if status_code_match: | 98 if status_code_match: |
99 return (int(status_code_match.group(1)), out, err) | 99 return (int(status_code_match.group(1)), out, err) |
100 if ('You are attempting to access protected data with ' | 100 if ('You are attempting to access protected data with ' |
101 'no configured credentials.' in err): | 101 'no configured credentials.' in err): |
102 return (403, out, err) | 102 return (403, out, err) |
103 if 'No such object' in err: | 103 if 'No such object' in err: |
104 return (404, out, err) | 104 return (404, out, err) |
105 return (code, out, err) | 105 return (code, out, err) |
106 | 106 |
107 | 107 |
108 def check_bucket_permissions(base_url, gsutil): | |
109 code, _, ls_err = gsutil.check_call('ls', base_url) | |
110 if code != 0: | |
111 print >> sys.stderr, ls_err | |
112 if code == 403: | |
113 print >> sys.stderr, 'Got error 403 while authenticating to %s.' % base_url | |
114 print >> sys.stderr, 'Try running "download_from_google_storage --config".' | |
115 elif code == 404: | |
116 print >> sys.stderr, '%s not found.' % base_url | |
117 return code | |
118 | |
119 | |
120 def check_platform(target): | 108 def check_platform(target): |
121 """Checks if any parent directory of target matches (win|mac|linux).""" | 109 """Checks if any parent directory of target matches (win|mac|linux).""" |
122 assert os.path.isabs(target) | 110 assert os.path.isabs(target) |
123 root, target_name = os.path.split(target) | 111 root, target_name = os.path.split(target) |
124 if not target_name: | 112 if not target_name: |
125 return None | 113 return None |
126 if target_name in ('linux', 'mac', 'win'): | 114 if target_name in ('linux', 'mac', 'win'): |
127 return target_name | 115 return target_name |
128 return check_platform(root) | 116 return check_platform(root) |
129 | 117 |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 parser.error('Unreachable state.') | 435 parser.error('Unreachable state.') |
448 | 436 |
449 # Check if output file already exists. | 437 # Check if output file already exists. |
450 if not options.directory and not options.force and not options.no_resume: | 438 if not options.directory and not options.force and not options.no_resume: |
451 if os.path.exists(options.output): | 439 if os.path.exists(options.output): |
452 parser.error('Output file %s exists and --no_resume is specified.' | 440 parser.error('Output file %s exists and --no_resume is specified.' |
453 % options.output) | 441 % options.output) |
454 | 442 |
455 base_url = 'gs://%s' % options.bucket | 443 base_url = 'gs://%s' % options.bucket |
456 | 444 |
457 # Check we have a valid bucket with valid permissions. | |
458 if not options.no_auth: | |
459 code = check_bucket_permissions(base_url, gsutil) | |
460 if code: | |
461 return code | |
462 | |
463 return download_from_google_storage( | 445 return download_from_google_storage( |
464 input_filename, base_url, gsutil, options.num_threads, options.directory, | 446 input_filename, base_url, gsutil, options.num_threads, options.directory, |
465 options.recursive, options.force, options.output, options.ignore_errors, | 447 options.recursive, options.force, options.output, options.ignore_errors, |
466 options.sha1_file, options.verbose, options.auto_platform) | 448 options.sha1_file, options.verbose, options.auto_platform) |
467 | 449 |
468 | 450 |
469 if __name__ == '__main__': | 451 if __name__ == '__main__': |
470 sys.exit(main(sys.argv)) | 452 sys.exit(main(sys.argv)) |
OLD | NEW |