| 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 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 | 30 |
| 31 class InvalidFileError(IOError): | 31 class InvalidFileError(IOError): |
| 32 pass | 32 pass |
| 33 | 33 |
| 34 | 34 |
| 35 # Common utilities | 35 # Common utilities |
| 36 class Gsutil(object): | 36 class Gsutil(object): |
| 37 """Call gsutil with some predefined settings. This is a convenience object, | 37 """Call gsutil with some predefined settings. This is a convenience object, |
| 38 and is also immutable.""" | 38 and is also immutable.""" |
| 39 def __init__(self, path, boto_path, timeout=None): | 39 def __init__(self, path, boto_path, timeout=None, bypass_prodaccess=False): |
| 40 if not os.path.exists(path): | 40 if not os.path.exists(path): |
| 41 raise FileNotFoundError('GSUtil not found in %s' % path) | 41 raise FileNotFoundError('GSUtil not found in %s' % path) |
| 42 self.path = path | 42 self.path = path |
| 43 self.timeout = timeout | 43 self.timeout = timeout |
| 44 self.boto_path = boto_path | 44 self.boto_path = boto_path |
| 45 self.bypass_prodaccess = False |
| 45 | 46 |
| 46 def get_sub_env(self): | 47 def get_sub_env(self): |
| 47 env = os.environ.copy() | 48 env = os.environ.copy() |
| 48 if self.boto_path == os.devnull: | 49 if self.boto_path == os.devnull: |
| 49 env['AWS_CREDENTIAL_FILE'] = '' | 50 env['AWS_CREDENTIAL_FILE'] = '' |
| 50 env['BOTO_CONFIG'] = '' | 51 env['BOTO_CONFIG'] = '' |
| 51 elif self.boto_path: | 52 elif self.boto_path: |
| 52 env['AWS_CREDENTIAL_FILE'] = self.boto_path | 53 env['AWS_CREDENTIAL_FILE'] = self.boto_path |
| 53 env['BOTO_CONFIG'] = self.boto_path | 54 env['BOTO_CONFIG'] = self.boto_path |
| 54 else: | 55 else: |
| 55 custompath = env.get('AWS_CREDENTIAL_FILE', '~/.boto') + '.depot_tools' | 56 custompath = env.get('AWS_CREDENTIAL_FILE', '~/.boto') + '.depot_tools' |
| 56 custompath = os.path.expanduser(custompath) | 57 custompath = os.path.expanduser(custompath) |
| 57 if os.path.exists(custompath): | 58 if os.path.exists(custompath): |
| 58 env['AWS_CREDENTIAL_FILE'] = custompath | 59 env['AWS_CREDENTIAL_FILE'] = custompath |
| 59 | 60 |
| 60 return env | 61 return env |
| 61 | 62 |
| 62 def call(self, *args): | 63 def call(self, *args): |
| 64 if self.bypass_prodaccess: |
| 65 args.insert(0, '--bypass_prodaccess') |
| 63 return subprocess2.call((sys.executable, self.path) + args, | 66 return subprocess2.call((sys.executable, self.path) + args, |
| 64 env=self.get_sub_env(), | 67 env=self.get_sub_env(), |
| 65 timeout=self.timeout) | 68 timeout=self.timeout) |
| 66 | 69 |
| 67 def check_call(self, *args): | 70 def check_call(self, *args): |
| 71 if self.bypass_prodaccess: |
| 72 args.insert(0, '--bypass_prodaccess') |
| 68 ((out, err), code) = subprocess2.communicate( | 73 ((out, err), code) = subprocess2.communicate( |
| 69 (sys.executable, self.path) + args, | 74 (sys.executable, self.path) + args, |
| 70 stdout=subprocess2.PIPE, | 75 stdout=subprocess2.PIPE, |
| 71 stderr=subprocess2.PIPE, | 76 stderr=subprocess2.PIPE, |
| 72 env=self.get_sub_env(), | 77 env=self.get_sub_env(), |
| 73 timeout=self.timeout) | 78 timeout=self.timeout) |
| 74 | 79 |
| 75 # Parse output. | 80 # Parse output. |
| 76 status_code_match = re.search('status=([0-9]+)', err) | 81 status_code_match = re.search('status=([0-9]+)', err) |
| 77 if status_code_match: | 82 if status_code_match: |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 print('The current platform doesn\'t match "%s", skipping.' % | 322 print('The current platform doesn\'t match "%s", skipping.' % |
| 318 options.platform) | 323 options.platform) |
| 319 return 0 | 324 return 0 |
| 320 | 325 |
| 321 # Set the boto file to /dev/null if we don't need auth. | 326 # Set the boto file to /dev/null if we don't need auth. |
| 322 if options.no_auth: | 327 if options.no_auth: |
| 323 options.boto = os.devnull | 328 options.boto = os.devnull |
| 324 | 329 |
| 325 # Make sure we can find a working instance of gsutil. | 330 # Make sure we can find a working instance of gsutil. |
| 326 if os.path.exists(GSUTIL_DEFAULT_PATH): | 331 if os.path.exists(GSUTIL_DEFAULT_PATH): |
| 327 gsutil = Gsutil(GSUTIL_DEFAULT_PATH, boto_path=options.boto) | 332 gsutil = Gsutil(GSUTIL_DEFAULT_PATH, |
| 333 boto_path=options.boto, |
| 334 bypass_prodaccess=options.no_auth) |
| 328 else: | 335 else: |
| 329 gsutil = None | 336 gsutil = None |
| 330 for path in os.environ["PATH"].split(os.pathsep): | 337 for path in os.environ["PATH"].split(os.pathsep): |
| 331 if os.path.exists(path) and 'gsutil' in os.listdir(path): | 338 if os.path.exists(path) and 'gsutil' in os.listdir(path): |
| 332 gsutil = Gsutil(os.path.join(path, 'gsutil'), boto_path=options.boto) | 339 gsutil = Gsutil(os.path.join(path, 'gsutil'), |
| 340 boto_path=options.boto, |
| 341 bypass_prodaccess=False) |
| 333 if not gsutil: | 342 if not gsutil: |
| 334 parser.error('gsutil not found in %s, bad depot_tools checkout?' % | 343 parser.error('gsutil not found in %s, bad depot_tools checkout?' % |
| 335 GSUTIL_DEFAULT_PATH) | 344 GSUTIL_DEFAULT_PATH) |
| 336 | 345 |
| 337 # Passing in -g/--config will run our copy of GSUtil, then quit. | 346 # Passing in -g/--config will run our copy of GSUtil, then quit. |
| 338 if options.config: | 347 if options.config: |
| 339 return gsutil.call('config') | 348 return gsutil.call('config') |
| 340 | 349 |
| 341 if not args: | 350 if not args: |
| 342 parser.error('Missing target.') | 351 parser.error('Missing target.') |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return code | 390 return code |
| 382 | 391 |
| 383 return download_from_google_storage( | 392 return download_from_google_storage( |
| 384 input_filename, base_url, gsutil, options.num_threads, options.directory, | 393 input_filename, base_url, gsutil, options.num_threads, options.directory, |
| 385 options.recursive, options.force, options.output, options.ignore_errors, | 394 options.recursive, options.force, options.output, options.ignore_errors, |
| 386 options.sha1_file) | 395 options.sha1_file) |
| 387 | 396 |
| 388 | 397 |
| 389 if __name__ == '__main__': | 398 if __name__ == '__main__': |
| 390 sys.exit(main(sys.argv)) | 399 sys.exit(main(sys.argv)) |
| OLD | NEW |