Chromium Code Reviews| 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 | |
|
Vadim Sh.
2013/12/03 01:33:41
Did you mean self.bypass_prodaccess = bypass_proda
Ryan Tseng
2013/12/03 22:57:11
Oops, yes
| |
| 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') | |
|
Vadim Sh.
2013/12/03 01:33:41
|args| is a tuple here. You can't insert stuff int
Ryan Tseng
2013/12/03 22:57:11
fixed
| |
| 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 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 print('The current platform doesn\'t match "%s", skipping.' % | 327 print('The current platform doesn\'t match "%s", skipping.' % |
| 323 options.platform) | 328 options.platform) |
| 324 return 0 | 329 return 0 |
| 325 | 330 |
| 326 # Set the boto file to /dev/null if we don't need auth. | 331 # Set the boto file to /dev/null if we don't need auth. |
| 327 if options.no_auth: | 332 if options.no_auth: |
| 328 options.boto = os.devnull | 333 options.boto = os.devnull |
| 329 | 334 |
| 330 # Make sure we can find a working instance of gsutil. | 335 # Make sure we can find a working instance of gsutil. |
| 331 if os.path.exists(GSUTIL_DEFAULT_PATH): | 336 if os.path.exists(GSUTIL_DEFAULT_PATH): |
| 332 gsutil = Gsutil(GSUTIL_DEFAULT_PATH, boto_path=options.boto) | 337 gsutil = Gsutil(GSUTIL_DEFAULT_PATH, |
| 338 boto_path=options.boto, | |
| 339 bypass_prodaccess=options.no_auth) | |
| 333 else: | 340 else: |
| 334 gsutil = None | 341 gsutil = None |
| 335 for path in os.environ["PATH"].split(os.pathsep): | 342 for path in os.environ["PATH"].split(os.pathsep): |
| 336 if os.path.exists(path) and 'gsutil' in os.listdir(path): | 343 if os.path.exists(path) and 'gsutil' in os.listdir(path): |
| 337 gsutil = Gsutil(os.path.join(path, 'gsutil'), boto_path=options.boto) | 344 gsutil = Gsutil(os.path.join(path, 'gsutil'), |
| 345 boto_path=options.boto, | |
| 346 bypass_prodaccess=False) | |
|
Vadim Sh.
2013/12/03 01:33:41
Why not use options.no_auth here as well? Is it be
Ryan Tseng
2013/12/03 22:57:11
Actually this business with finding an existing ve
| |
| 338 if not gsutil: | 347 if not gsutil: |
| 339 parser.error('gsutil not found in %s, bad depot_tools checkout?' % | 348 parser.error('gsutil not found in %s, bad depot_tools checkout?' % |
| 340 GSUTIL_DEFAULT_PATH) | 349 GSUTIL_DEFAULT_PATH) |
| 341 | 350 |
| 342 # Passing in -g/--config will run our copy of GSUtil, then quit. | 351 # Passing in -g/--config will run our copy of GSUtil, then quit. |
| 343 if options.config: | 352 if options.config: |
| 344 return gsutil.call('config') | 353 return gsutil.call('config') |
| 345 | 354 |
| 346 if not args: | 355 if not args: |
| 347 parser.error('Missing target.') | 356 parser.error('Missing target.') |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 return code | 395 return code |
| 387 | 396 |
| 388 return download_from_google_storage( | 397 return download_from_google_storage( |
| 389 input_filename, base_url, gsutil, options.num_threads, options.directory, | 398 input_filename, base_url, gsutil, options.num_threads, options.directory, |
| 390 options.recursive, options.force, options.output, options.ignore_errors, | 399 options.recursive, options.force, options.output, options.ignore_errors, |
| 391 options.sha1_file, options.verbose) | 400 options.sha1_file, options.verbose) |
| 392 | 401 |
| 393 | 402 |
| 394 if __name__ == '__main__': | 403 if __name__ == '__main__': |
| 395 sys.exit(main(sys.argv)) | 404 sys.exit(main(sys.argv)) |
| OLD | NEW |