Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: gsutil.py

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
Patch Set: subprocess.call() Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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):
41 kwargs['stdout'] = subprocess.PIPE 41 return subprocess.call(
42 kwargs['stderr'] = subprocess.STDOUT 42 args, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
scottmg 2015/01/23 22:07:23 stdout=None, etc means it uses the parent's, which
hinoka 2015/01/23 23:47:40 Oh yep, you're right. Confirmed locally.
43 proc = subprocess.Popen(args, **kwargs)
44 out = []
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 43
54 44
55 def download_gsutil(version, target_dir): 45 def download_gsutil(version, target_dir):
56 """Downloads gsutil into the target_dir.""" 46 """Downloads gsutil into the target_dir."""
57 filename = 'gsutil_%s.zip' % version 47 filename = 'gsutil_%s.zip' % version
58 target_filename = os.path.join(target_dir, filename) 48 target_filename = os.path.join(target_dir, filename)
59 49
60 # Check if the target exists already. 50 # Check if the target exists already.
61 if os.path.exists(target_filename): 51 if os.path.exists(target_filename):
62 md5_calc = hashlib.md5() 52 md5_calc = hashlib.md5()
63 with open(target_filename, 'rb') as f: 53 with open(target_filename, 'rb') as f:
64 while True: 54 while True:
65 buf = f.read(4096) 55 buf = f.read(4096)
66 if not buf: 56 if not buf:
67 break 57 break
68 md5_calc.update(buf) 58 md5_calc.update(buf)
69 local_md5 = md5_calc.hexdigest() 59 local_md5 = md5_calc.hexdigest()
70 60
71 metadata_url = '%s%s' % (API_URL, filename) 61 metadata_url = '%s%s' % (API_URL, filename)
72 metadata = json.load(urllib2.urlopen(metadata_url)) 62 metadata = json.load(urllib2.urlopen(metadata_url))
73 remote_md5 = base64.b64decode(metadata['md5Hash']) 63 remote_md5 = base64.b64decode(metadata['md5Hash'])
74 64
75 if local_md5 == remote_md5: 65 if local_md5 == remote_md5:
76 return target_filename 66 return target_filename
77 os.remove(target_filename) 67 os.remove(target_filename)
78 68
79 # Do the download. 69 # Do the download.
80 url = '%s%s' % (GSUTIL_URL, filename) 70 url = '%s%s' % (GSUTIL_URL, filename)
81 u = urllib2.urlopen(url) 71 u = urllib2.urlopen(url)
72 print >> sys.stderr, 'Downloading gsutil from %s...' % url
pgervais 2015/01/23 21:59:18 This is changing the output of the vanilla gsutil
hinoka 2015/01/23 23:47:40 Removed this printout
82 with open(target_filename, 'wb') as f: 73 with open(target_filename, 'wb') as f:
83 while True: 74 while True:
84 buf = u.read(4096) 75 buf = u.read(4096)
85 if not buf: 76 if not buf:
86 break 77 break
87 f.write(buf) 78 f.write(buf)
88 return target_filename 79 return target_filename
89 80
90 81
91 def check_gsutil(gsutil_bin): 82 def check_gsutil(gsutil_bin):
92 """Run gsutil version and make sure it runs.""" 83 """Run gsutil version and make sure it runs."""
93 try: 84 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 85
99 86
100 def ensure_gsutil(version, target): 87 def ensure_gsutil(version, target):
101 bin_dir = os.path.join(target, 'gsutil_%s' % version) 88 bin_dir = os.path.join(target, 'gsutil_%s' % version)
102 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') 89 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil')
103 if os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): 90 if os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin):
104 # Everything is awesome! we're all done here. 91 # Everything is awesome! we're all done here.
105 return gsutil_bin 92 return gsutil_bin
106 93
107 if os.path.isdir(bin_dir): 94 if os.path.isdir(bin_dir):
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 args.args = extras + args.args 135 args.args = extras + args.args
149 return args.force_version, args.fallback, args.target, args.args 136 return args.force_version, args.fallback, args.target, args.args
150 137
151 138
152 def main(): 139 def main():
153 force_version, fallback, target, args = parse_args() 140 force_version, fallback, target, args = parse_args()
154 return run_gsutil(force_version, fallback, target, args) 141 return run_gsutil(force_version, fallback, target, args)
155 142
156 if __name__ == '__main__': 143 if __name__ == '__main__':
157 sys.exit(main()) 144 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698