 Chromium Code Reviews
 Chromium Code Reviews Issue 870093003:
  Hook sys.stdio directly to the gsutil subprocess for the gsutil call  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
    
  
    Issue 870093003:
  Hook sys.stdio directly to the gsutil subprocess for the gsutil call  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools| OLD | NEW | 
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python | 
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 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 """Run a pinned gsutil.""" | 6 """Run a pinned gsutil.""" | 
| 7 | 7 | 
| 8 | 8 | 
| 9 import argparse | 9 import argparse | 
| 10 import base64 | 10 import base64 | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 class SubprocessError(Exception): | 30 class SubprocessError(Exception): | 
| 31 def __init__(self, message=None, code=0): | 31 def __init__(self, message=None, code=0): | 
| 32 super(SubprocessError, self).__init__(message) | 32 super(SubprocessError, self).__init__(message) | 
| 33 self.code = code | 33 self.code = code | 
| 34 | 34 | 
| 35 | 35 | 
| 36 class InvalidGsutilError(Exception): | 36 class InvalidGsutilError(Exception): | 
| 37 pass | 37 pass | 
| 38 | 38 | 
| 39 | 39 | 
| 40 def call(args, verbose=True, **kwargs): | 40 def call(args, **kwargs): | 
| 
scottmg
2015/01/23 21:09:41
I think this is just subprocess.call() now?
 
hinoka
2015/01/23 21:56:25
You're right.  Done.
I'm keeping the alias, becau
 | |
| 41 kwargs['stdout'] = subprocess.PIPE | 41 kwargs['stdout'] = sys.stdout | 
| 42 kwargs['stderr'] = subprocess.STDOUT | 42 kwargs['stderr'] = sys.stderr | 
| 43 kwargs['stdin'] = sys.stdin | |
| 43 proc = subprocess.Popen(args, **kwargs) | 44 proc = subprocess.Popen(args, **kwargs) | 
| 44 out = [] | 45 return proc.wait() | 
| 45 for line in proc.stdout: | |
| 46 out.append(line) | |
| 47 if verbose: | |
| 48 sys.stdout.write(line) | |
| 49 code = proc.wait() | |
| 50 if code: | |
| 51 raise SubprocessError('%s failed with %s' % (args, code), code) | |
| 52 return ''.join(out) | |
| 53 | 46 | 
| 54 | 47 | 
| 55 def download_gsutil(version, target_dir): | 48 def download_gsutil(version, target_dir): | 
| 56 """Downloads gsutil into the target_dir.""" | 49 """Downloads gsutil into the target_dir.""" | 
| 57 filename = 'gsutil_%s.zip' % version | 50 filename = 'gsutil_%s.zip' % version | 
| 58 target_filename = os.path.join(target_dir, filename) | 51 target_filename = os.path.join(target_dir, filename) | 
| 59 | 52 | 
| 60 # Check if the target exists already. | 53 # Check if the target exists already. | 
| 61 if os.path.exists(target_filename): | 54 if os.path.exists(target_filename): | 
| 62 md5_calc = hashlib.md5() | 55 md5_calc = hashlib.md5() | 
| 63 with open(target_filename, 'rb') as f: | 56 with open(target_filename, 'rb') as f: | 
| 64 while True: | 57 while True: | 
| 65 buf = f.read(4096) | 58 buf = f.read(4096) | 
| 66 if not buf: | 59 if not buf: | 
| 67 break | 60 break | 
| 68 md5_calc.update(buf) | 61 md5_calc.update(buf) | 
| 69 local_md5 = md5_calc.hexdigest() | 62 local_md5 = md5_calc.hexdigest() | 
| 70 | 63 | 
| 71 metadata_url = '%s%s' % (API_URL, filename) | 64 metadata_url = '%s%s' % (API_URL, filename) | 
| 72 metadata = json.load(urllib2.urlopen(metadata_url)) | 65 metadata = json.load(urllib2.urlopen(metadata_url)) | 
| 73 remote_md5 = base64.b64decode(metadata['md5Hash']) | 66 remote_md5 = base64.b64decode(metadata['md5Hash']) | 
| 74 | 67 | 
| 75 if local_md5 == remote_md5: | 68 if local_md5 == remote_md5: | 
| 76 return target_filename | 69 return target_filename | 
| 77 os.remove(target_filename) | 70 os.remove(target_filename) | 
| 78 | 71 | 
| 79 # Do the download. | 72 # Do the download. | 
| 80 url = '%s%s' % (GSUTIL_URL, filename) | 73 url = '%s%s' % (GSUTIL_URL, filename) | 
| 81 u = urllib2.urlopen(url) | 74 u = urllib2.urlopen(url) | 
| 75 print >> sys.stderr, 'Downloading gsutil from %s...' % url | |
| 82 with open(target_filename, 'wb') as f: | 76 with open(target_filename, 'wb') as f: | 
| 83 while True: | 77 while True: | 
| 84 buf = u.read(4096) | 78 buf = u.read(4096) | 
| 85 if not buf: | 79 if not buf: | 
| 86 break | 80 break | 
| 87 f.write(buf) | 81 f.write(buf) | 
| 88 return target_filename | 82 return target_filename | 
| 89 | 83 | 
| 90 | 84 | 
| 91 def check_gsutil(gsutil_bin): | 85 def check_gsutil(gsutil_bin): | 
| 92 """Run gsutil version and make sure it runs.""" | 86 """Run gsutil version and make sure it runs.""" | 
| 93 try: | 87 return call([sys.executable, gsutil_bin, 'version']) == 0 | 
| 94 call([sys.executable, gsutil_bin, 'version'], verbose=False) | |
| 95 return True | |
| 96 except SubprocessError: | |
| 97 return False | |
| 98 | 88 | 
| 99 | 89 | 
| 100 def ensure_gsutil(version, target): | 90 def ensure_gsutil(version, target): | 
| 101 bin_dir = os.path.join(target, 'gsutil_%s' % version) | 91 bin_dir = os.path.join(target, 'gsutil_%s' % version) | 
| 102 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') | 92 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') | 
| 103 if os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): | 93 if os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): | 
| 104 # Everything is awesome! we're all done here. | 94 # Everything is awesome! we're all done here. | 
| 105 return gsutil_bin | 95 return gsutil_bin | 
| 106 | 96 | 
| 107 if os.path.isdir(bin_dir): | 97 if os.path.isdir(bin_dir): | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 args.args = extras + args.args | 138 args.args = extras + args.args | 
| 149 return args.force_version, args.fallback, args.target, args.args | 139 return args.force_version, args.fallback, args.target, args.args | 
| 150 | 140 | 
| 151 | 141 | 
| 152 def main(): | 142 def main(): | 
| 153 force_version, fallback, target, args = parse_args() | 143 force_version, fallback, target, args = parse_args() | 
| 154 return run_gsutil(force_version, fallback, target, args) | 144 return run_gsutil(force_version, fallback, target, args) | 
| 155 | 145 | 
| 156 if __name__ == '__main__': | 146 if __name__ == '__main__': | 
| 157 sys.exit(main()) | 147 sys.exit(main()) | 
| OLD | NEW |