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 24 matching lines...) Expand all Loading... |
35 class Gsutil(object): | 35 class Gsutil(object): |
36 """Call gsutil with some predefined settings. This is a convenience object, | 36 """Call gsutil with some predefined settings. This is a convenience object, |
37 and is also immutable.""" | 37 and is also immutable.""" |
38 def __init__(self, path, boto_path, timeout=None): | 38 def __init__(self, path, boto_path, timeout=None): |
39 if not os.path.exists(path): | 39 if not os.path.exists(path): |
40 raise FileNotFoundError('GSUtil not found in %s' % path) | 40 raise FileNotFoundError('GSUtil not found in %s' % path) |
41 self.path = path | 41 self.path = path |
42 self.timeout = timeout | 42 self.timeout = timeout |
43 self.boto_path = boto_path | 43 self.boto_path = boto_path |
44 | 44 |
45 def call(self, *args): | 45 def get_sub_env(self): |
46 env = os.environ.copy() | 46 env = os.environ.copy() |
47 if self.boto_path: | 47 if self.boto_path: |
48 env['AWS_CREDENTIAL_FILE'] = self.boto_path | 48 env['AWS_CREDENTIAL_FILE'] = self.boto_path |
| 49 else: |
| 50 custompath = env.get('AWS_CREDENTIAL_FILE', '~/.boto') + '.depot_tools' |
| 51 custompath = os.path.expanduser(custompath) |
| 52 if os.path.exists(custompath): |
| 53 env['AWS_CREDENTIAL_FILE'] = custompath |
| 54 |
| 55 return env |
| 56 |
| 57 def call(self, *args): |
49 return subprocess2.call((sys.executable, self.path) + args, | 58 return subprocess2.call((sys.executable, self.path) + args, |
50 env=env, | 59 env=self.get_sub_env(), |
51 timeout=self.timeout) | 60 timeout=self.timeout) |
52 | 61 |
53 def check_call(self, *args): | 62 def check_call(self, *args): |
54 env = os.environ.copy() | |
55 if self.boto_path: | |
56 env['AWS_CREDENTIAL_FILE'] = self.boto_path | |
57 ((out, err), code) = subprocess2.communicate( | 63 ((out, err), code) = subprocess2.communicate( |
58 (sys.executable, self.path) + args, | 64 (sys.executable, self.path) + args, |
59 stdout=subprocess2.PIPE, | 65 stdout=subprocess2.PIPE, |
60 stderr=subprocess2.PIPE, | 66 stderr=subprocess2.PIPE, |
61 env=env, | 67 env=self.get_sub_env(), |
62 timeout=self.timeout) | 68 timeout=self.timeout) |
63 | 69 |
64 # Parse output. | 70 # Parse output. |
65 status_code_match = re.search('status=([0-9]+)', err) | 71 status_code_match = re.search('status=([0-9]+)', err) |
66 if status_code_match: | 72 if status_code_match: |
67 return (int(status_code_match.group(1)), out, err) | 73 return (int(status_code_match.group(1)), out, err) |
68 if ('You are attempting to access protected data with ' | 74 if ('You are attempting to access protected data with ' |
69 'no configured credentials.' in err): | 75 'no configured credentials.' in err): |
70 return (403, out, err) | 76 return (403, out, err) |
71 if 'No such object' in err: | 77 if 'No such object' in err: |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 return code | 335 return code |
330 | 336 |
331 return download_from_google_storage( | 337 return download_from_google_storage( |
332 input_filename, base_url, gsutil, options.num_threads, options.directory, | 338 input_filename, base_url, gsutil, options.num_threads, options.directory, |
333 options.recursive, options.force, options.output, options.ignore_errors, | 339 options.recursive, options.force, options.output, options.ignore_errors, |
334 options.sha1_file) | 340 options.sha1_file) |
335 | 341 |
336 | 342 |
337 if __name__ == '__main__': | 343 if __name__ == '__main__': |
338 sys.exit(main(sys.argv)) | 344 sys.exit(main(sys.argv)) |
OLD | NEW |