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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 """Returns the result of sys.platform accounting for cygwin. | 48 """Returns the result of sys.platform accounting for cygwin. |
49 Under cygwin, this will always return "win32" like the native Python.""" | 49 Under cygwin, this will always return "win32" like the native Python.""" |
50 if sys.platform == 'cygwin': | 50 if sys.platform == 'cygwin': |
51 return 'win32' | 51 return 'win32' |
52 return sys.platform | 52 return sys.platform |
53 | 53 |
54 # Common utilities | 54 # Common utilities |
55 class Gsutil(object): | 55 class Gsutil(object): |
56 """Call gsutil with some predefined settings. This is a convenience object, | 56 """Call gsutil with some predefined settings. This is a convenience object, |
57 and is also immutable.""" | 57 and is also immutable.""" |
| 58 |
| 59 MAX_TRIES = 5 |
| 60 RETRY_BASE_DELAY = 5.0 |
| 61 RETRY_DELAY_MULTIPLE = 1.3 |
| 62 |
58 def __init__(self, path, boto_path=None, timeout=None, version='4.15'): | 63 def __init__(self, path, boto_path=None, timeout=None, version='4.15'): |
59 if not os.path.exists(path): | 64 if not os.path.exists(path): |
60 raise FileNotFoundError('GSUtil not found in %s' % path) | 65 raise FileNotFoundError('GSUtil not found in %s' % path) |
61 self.path = path | 66 self.path = path |
62 self.timeout = timeout | 67 self.timeout = timeout |
63 self.boto_path = boto_path | 68 self.boto_path = boto_path |
64 self.version = version | 69 self.version = version |
65 | 70 |
66 def get_sub_env(self): | 71 def get_sub_env(self): |
67 env = os.environ.copy() | 72 env = os.environ.copy() |
(...skipping 25 matching lines...) Expand all Loading... |
93 status_code_match = re.search('status=([0-9]+)', err) | 98 status_code_match = re.search('status=([0-9]+)', err) |
94 if status_code_match: | 99 if status_code_match: |
95 return (int(status_code_match.group(1)), out, err) | 100 return (int(status_code_match.group(1)), out, err) |
96 if ('You are attempting to access protected data with ' | 101 if ('You are attempting to access protected data with ' |
97 'no configured credentials.' in err): | 102 'no configured credentials.' in err): |
98 return (403, out, err) | 103 return (403, out, err) |
99 if 'matched no objects' in err: | 104 if 'matched no objects' in err: |
100 return (404, out, err) | 105 return (404, out, err) |
101 return (code, out, err) | 106 return (code, out, err) |
102 | 107 |
| 108 def check_call_with_retries(self, *args): |
| 109 delay = self.RETRY_BASE_DELAY |
| 110 for i in xrange(self.MAX_TRIES): |
| 111 code, out, err = self.check_call(*args) |
| 112 if not code or i == self.MAX_TRIES - 1: |
| 113 break |
| 114 |
| 115 time.sleep(delay) |
| 116 delay *= self.RETRY_DELAY_MULTIPLE |
| 117 |
| 118 return code, out, err |
| 119 |
103 | 120 |
104 def check_platform(target): | 121 def check_platform(target): |
105 """Checks if any parent directory of target matches (win|mac|linux).""" | 122 """Checks if any parent directory of target matches (win|mac|linux).""" |
106 assert os.path.isabs(target) | 123 assert os.path.isabs(target) |
107 root, target_name = os.path.split(target) | 124 root, target_name = os.path.split(target) |
108 if not target_name: | 125 if not target_name: |
109 return None | 126 return None |
110 if target_name in ('linux', 'mac', 'win'): | 127 if target_name in ('linux', 'mac', 'win'): |
111 return target_name | 128 return target_name |
112 return check_platform(root) | 129 return check_platform(root) |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 | 522 |
506 return download_from_google_storage( | 523 return download_from_google_storage( |
507 input_filename, base_url, gsutil, options.num_threads, options.directory, | 524 input_filename, base_url, gsutil, options.num_threads, options.directory, |
508 options.recursive, options.force, options.output, options.ignore_errors, | 525 options.recursive, options.force, options.output, options.ignore_errors, |
509 options.sha1_file, options.verbose, options.auto_platform, | 526 options.sha1_file, options.verbose, options.auto_platform, |
510 options.extract) | 527 options.extract) |
511 | 528 |
512 | 529 |
513 if __name__ == '__main__': | 530 if __name__ == '__main__': |
514 sys.exit(main(sys.argv)) | 531 sys.exit(main(sys.argv)) |
OLD | NEW |