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

Side by Side Diff: subprocess2.py

Issue 6877005: Make subprocess2.call() returned values to be the same as subprocess.call(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 9 years, 8 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 | tests/subprocess2_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding=utf8 1 # coding=utf8
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 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 """Collection of subprocess wrapper functions. 5 """Collection of subprocess wrapper functions.
6 6
7 In theory you shouldn't need anything else in subprocess, or this module failed. 7 In theory you shouldn't need anything else in subprocess, or this module failed.
8 """ 8 """
9 9
10 from __future__ import with_statement 10 from __future__ import with_statement
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 kwargs.get('cwd'), 185 kwargs.get('cwd'),
186 None, 186 None,
187 'Visit ' 187 'Visit '
188 'http://code.google.com/p/chromium/wiki/CygwinDllRemappingFailure to ' 188 'http://code.google.com/p/chromium/wiki/CygwinDllRemappingFailure to '
189 'learn how to fix this error; you need to rebase your cygwin dlls') 189 'learn how to fix this error; you need to rebase your cygwin dlls')
190 # Popen() can throw OSError when cwd or args[0] doesn't exist. Let it go 190 # Popen() can throw OSError when cwd or args[0] doesn't exist. Let it go
191 # through 191 # through
192 raise 192 raise
193 193
194 194
195 def call(args, timeout=None, **kwargs): 195 def communicate(args, timeout=None, **kwargs):
196 """Wraps subprocess.Popen().communicate(). 196 """Wraps subprocess.Popen().communicate().
197 197
198 Returns ((stdout, stderr), returncode). 198 Returns ((stdout, stderr), returncode).
199 199
200 - The process will be killed after |timeout| seconds and returncode set to 200 - The process will be killed after |timeout| seconds and returncode set to
201 TIMED_OUT. 201 TIMED_OUT.
202 - Automatically passes stdin content as input so do not specify stdin=PIPE. 202 - Automatically passes stdin content as input so do not specify stdin=PIPE.
203 """ 203 """
204 stdin = kwargs.pop('stdin', None) 204 stdin = kwargs.pop('stdin', None)
205 if stdin is not None: 205 if stdin is not None:
(...skipping 27 matching lines...) Expand all
233 proc.wait() 233 proc.wait()
234 # It's -9 on linux and 1 on Windows. Standardize to TIMED_OUT. 234 # It's -9 on linux and 1 on Windows. Standardize to TIMED_OUT.
235 proc.returncode = TIMED_OUT 235 proc.returncode = TIMED_OUT
236 time.sleep(0.001) 236 time.sleep(0.001)
237 # Now that the process died, reset the cursor and read the file. 237 # Now that the process died, reset the cursor and read the file.
238 buff.seek(0) 238 buff.seek(0)
239 out = [buff.read(), None] 239 out = [buff.read(), None]
240 return out, proc.returncode 240 return out, proc.returncode
241 241
242 242
243 def call(args, **kwargs):
244 """Emulates subprocess.call().
245
246 Automatically convert stdout=PIPE or stderr=PIPE to VOID.
247 """
248 if kwargs.get('stdout') == PIPE:
249 kwargs['stdout'] = VOID
250 if kwargs.get('stderr') == PIPE:
251 kwargs['stderr'] = VOID
252 return communicate(args, **kwargs)[1]
253
254
243 def check_call(args, **kwargs): 255 def check_call(args, **kwargs):
244 """Improved version of subprocess.check_call(). 256 """Improved version of subprocess.check_call().
245 257
246 Returns (stdout, stderr), unlike subprocess.check_call(). 258 Returns (stdout, stderr), unlike subprocess.check_call().
247 """ 259 """
248 out, returncode = call(args, **kwargs) 260 out, returncode = communicate(args, **kwargs)
249 if returncode: 261 if returncode:
250 raise CalledProcessError( 262 raise CalledProcessError(
251 returncode, args, kwargs.get('cwd'), out[0], out[1]) 263 returncode, args, kwargs.get('cwd'), out[0], out[1])
252 return out 264 return out
253 265
254 266
255 def capture(args, **kwargs): 267 def capture(args, **kwargs):
256 """Captures stdout of a process call and returns it. 268 """Captures stdout of a process call and returns it.
257 269
258 Returns stdout. 270 Returns stdout.
259 271
260 - Discards returncode. 272 - Discards returncode.
261 - Discards stderr. By default sets stderr=STDOUT. 273 - Discards stderr. By default sets stderr=STDOUT.
262 - Blocks stdin by default since no output will be visible. 274 - Blocks stdin by default since no output will be visible.
263 """ 275 """
264 if kwargs.get('stdin') is None: 276 if kwargs.get('stdin') is None:
265 kwargs['stdin'] = VOID 277 kwargs['stdin'] = VOID
266 if kwargs.get('stdout') is None: 278 if kwargs.get('stdout') is None:
267 kwargs['stdout'] = PIPE 279 kwargs['stdout'] = PIPE
268 if kwargs.get('stderr') is None: 280 if kwargs.get('stderr') is None:
269 kwargs['stderr'] = STDOUT 281 kwargs['stderr'] = STDOUT
270 return call(args, **kwargs)[0][0] 282 return communicate(args, **kwargs)[0][0]
271 283
272 284
273 def check_output(args, **kwargs): 285 def check_output(args, **kwargs):
274 """Captures stdout of a process call and returns it. 286 """Captures stdout of a process call and returns it.
275 287
276 Returns stdout. 288 Returns stdout.
277 289
278 - Discards stderr. By default sets stderr=STDOUT. 290 - Discards stderr. By default sets stderr=STDOUT.
279 - Throws if return code is not 0. 291 - Throws if return code is not 0.
280 - Works even prior to python 2.7. 292 - Works even prior to python 2.7.
281 - Blocks stdin by default since no output will be visible. 293 - Blocks stdin by default since no output will be visible.
282 """ 294 """
283 if kwargs.get('stdin') is None: 295 if kwargs.get('stdin') is None:
284 kwargs['stdin'] = VOID 296 kwargs['stdin'] = VOID
285 if kwargs.get('stdout') is None: 297 if kwargs.get('stdout') is None:
286 kwargs['stdout'] = PIPE 298 kwargs['stdout'] = PIPE
287 if kwargs.get('stderr') is None: 299 if kwargs.get('stderr') is None:
288 kwargs['stderr'] = STDOUT 300 kwargs['stderr'] = STDOUT
289 return check_call(args, **kwargs)[0] 301 return check_call(args, **kwargs)[0]
OLDNEW
« no previous file with comments | « no previous file | tests/subprocess2_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698