| 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 = bypass_prodaccess |
| 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): |
| 63 return subprocess2.call((sys.executable, self.path) + args, | 64 cmd = [sys.executable, self.path] |
| 64 env=self.get_sub_env(), | 65 if self.bypass_prodaccess: |
| 65 timeout=self.timeout) | 66 cmd += ['--bypass_prodaccess'] |
| 67 cmd += list(args) |
| 68 return subprocess2.call(cmd, env=self.get_sub_env(), timeout=self.timeout) |
| 66 | 69 |
| 67 def check_call(self, *args): | 70 def check_call(self, *args): |
| 71 cmd = [sys.executable, self.path] |
| 72 if self.bypass_prodaccess: |
| 73 cmd += ['--bypass_prodaccess'] |
| 74 cmd += list(args) |
| 68 ((out, err), code) = subprocess2.communicate( | 75 ((out, err), code) = subprocess2.communicate( |
| 69 (sys.executable, self.path) + args, | 76 cmd, |
| 70 stdout=subprocess2.PIPE, | 77 stdout=subprocess2.PIPE, |
| 71 stderr=subprocess2.PIPE, | 78 stderr=subprocess2.PIPE, |
| 72 env=self.get_sub_env(), | 79 env=self.get_sub_env(), |
| 73 timeout=self.timeout) | 80 timeout=self.timeout) |
| 74 | 81 |
| 75 # Parse output. | 82 # Parse output. |
| 76 status_code_match = re.search('status=([0-9]+)', err) | 83 status_code_match = re.search('status=([0-9]+)', err) |
| 77 if status_code_match: | 84 if status_code_match: |
| 78 return (int(status_code_match.group(1)), out, err) | 85 return (int(status_code_match.group(1)), out, err) |
| 79 if ('You are attempting to access protected data with ' | 86 if ('You are attempting to access protected data with ' |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 if not re.match(options.platform, sys.platform): | 327 if not re.match(options.platform, sys.platform): |
| 321 if options.verbose: | 328 if options.verbose: |
| 322 print('The current platform doesn\'t match "%s", skipping.' % | 329 print('The current platform doesn\'t match "%s", skipping.' % |
| 323 options.platform) | 330 options.platform) |
| 324 return 0 | 331 return 0 |
| 325 | 332 |
| 326 # Set the boto file to /dev/null if we don't need auth. | 333 # Set the boto file to /dev/null if we don't need auth. |
| 327 if options.no_auth: | 334 if options.no_auth: |
| 328 options.boto = os.devnull | 335 options.boto = os.devnull |
| 329 | 336 |
| 330 # Make sure we can find a working instance of gsutil. | 337 # Make sure gsutil exists where we expect it to. |
| 331 if os.path.exists(GSUTIL_DEFAULT_PATH): | 338 if os.path.exists(GSUTIL_DEFAULT_PATH): |
| 332 gsutil = Gsutil(GSUTIL_DEFAULT_PATH, boto_path=options.boto) | 339 gsutil = Gsutil(GSUTIL_DEFAULT_PATH, |
| 340 boto_path=options.boto, |
| 341 bypass_prodaccess=options.no_auth) |
| 333 else: | 342 else: |
| 334 gsutil = None | 343 parser.error('gsutil not found in %s, bad depot_tools checkout?' % |
| 335 for path in os.environ["PATH"].split(os.pathsep): | 344 GSUTIL_DEFAULT_PATH) |
| 336 if os.path.exists(path) and 'gsutil' in os.listdir(path): | |
| 337 gsutil = Gsutil(os.path.join(path, 'gsutil'), boto_path=options.boto) | |
| 338 if not gsutil: | |
| 339 parser.error('gsutil not found in %s, bad depot_tools checkout?' % | |
| 340 GSUTIL_DEFAULT_PATH) | |
| 341 | 345 |
| 342 # 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. |
| 343 if options.config: | 347 if options.config: |
| 344 return gsutil.call('config') | 348 return gsutil.call('config') |
| 345 | 349 |
| 346 if not args: | 350 if not args: |
| 347 parser.error('Missing target.') | 351 parser.error('Missing target.') |
| 348 if len(args) > 1: | 352 if len(args) > 1: |
| 349 parser.error('Too many targets.') | 353 parser.error('Too many targets.') |
| 350 if not options.bucket: | 354 if not options.bucket: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 return code | 390 return code |
| 387 | 391 |
| 388 return download_from_google_storage( | 392 return download_from_google_storage( |
| 389 input_filename, base_url, gsutil, options.num_threads, options.directory, | 393 input_filename, base_url, gsutil, options.num_threads, options.directory, |
| 390 options.recursive, options.force, options.output, options.ignore_errors, | 394 options.recursive, options.force, options.output, options.ignore_errors, |
| 391 options.sha1_file, options.verbose) | 395 options.sha1_file, options.verbose) |
| 392 | 396 |
| 393 | 397 |
| 394 if __name__ == '__main__': | 398 if __name__ == '__main__': |
| 395 sys.exit(main(sys.argv)) | 399 sys.exit(main(sys.argv)) |
| OLD | NEW |