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

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: 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, verbose=True, native=False, **kwargs):
41 kwargs['stdout'] = subprocess.PIPE 41 if native:
42 kwargs['stderr'] = subprocess.STDOUT 42 kwargs['stdout'] = sys.stdout
43 kwargs['stderr'] = sys.stderr
44 else:
45 kwargs['stdout'] = subprocess.PIPE
46 kwargs['stderr'] = subprocess.STDOUT
47 kwargs['stdin'] = sys.stdin
43 proc = subprocess.Popen(args, **kwargs) 48 proc = subprocess.Popen(args, **kwargs)
44 out = [] 49 out = []
45 for line in proc.stdout: 50 if not native:
46 out.append(line) 51 for line in proc.stdout:
47 if verbose: 52 out.append(line)
48 sys.stdout.write(line) 53 if verbose:
54 sys.stdout.write(line)
49 code = proc.wait() 55 code = proc.wait()
50 if code: 56 if code:
51 raise SubprocessError('%s failed with %s' % (args, code), code) 57 raise SubprocessError('%s failed with %s' % (args, code), code)
52 return ''.join(out) 58 return ''.join(out)
53 59
54 60
55 def download_gsutil(version, target_dir): 61 def download_gsutil(version, target_dir):
56 """Downloads gsutil into the target_dir.""" 62 """Downloads gsutil into the target_dir."""
57 filename = 'gsutil_%s.zip' % version 63 filename = 'gsutil_%s.zip' % version
58 target_filename = os.path.join(target_dir, filename) 64 target_filename = os.path.join(target_dir, filename)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 return gsutil_bin 127 return gsutil_bin
122 128
123 129
124 def run_gsutil(force_version, fallback, target, args): 130 def run_gsutil(force_version, fallback, target, args):
125 if force_version: 131 if force_version:
126 gsutil_bin = ensure_gsutil(force_version, target) 132 gsutil_bin = ensure_gsutil(force_version, target)
127 else: 133 else:
128 gsutil_bin = fallback 134 gsutil_bin = fallback
129 cmd = [sys.executable, gsutil_bin] + args 135 cmd = [sys.executable, gsutil_bin] + args
130 try: 136 try:
131 call(cmd) 137 call(cmd, native=True)
scottmg 2015/01/23 20:51:54 The only other place it's used is check_gsutil, so
hinoka 2015/01/23 21:05:35 Done.
132 except SubprocessError as e: 138 except SubprocessError as e:
133 return e.code 139 return e.code
134 return 0 140 return 0
135 141
136 142
137 def parse_args(): 143 def parse_args():
138 parser = argparse.ArgumentParser() 144 parser = argparse.ArgumentParser()
139 parser.add_argument('--force-version') 145 parser.add_argument('--force-version')
140 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) 146 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL)
141 parser.add_argument('--target', default=DEFAULT_BIN_DIR) 147 parser.add_argument('--target', default=DEFAULT_BIN_DIR)
142 parser.add_argument('args', nargs=argparse.REMAINDER) 148 parser.add_argument('args', nargs=argparse.REMAINDER)
143 149
144 args, extras = parser.parse_known_args() 150 args, extras = parser.parse_known_args()
145 if args.args and args.args[0] == '--': 151 if args.args and args.args[0] == '--':
146 args.args.pop(0) 152 args.args.pop(0)
147 if extras: 153 if extras:
148 args.args = extras + args.args 154 args.args = extras + args.args
149 return args.force_version, args.fallback, args.target, args.args 155 return args.force_version, args.fallback, args.target, args.args
150 156
151 157
152 def main(): 158 def main():
153 force_version, fallback, target, args = parse_args() 159 force_version, fallback, target, args = parse_args()
154 return run_gsutil(force_version, fallback, target, args) 160 return run_gsutil(force_version, fallback, target, args)
155 161
156 if __name__ == '__main__': 162 if __name__ == '__main__':
157 sys.exit(main()) 163 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