OLD | NEW |
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 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 # Now that the process died, reset the cursor and read the file. | 242 # Now that the process died, reset the cursor and read the file. |
243 buff.seek(0) | 243 buff.seek(0) |
244 out = [buff.read(), None] | 244 out = [buff.read(), None] |
245 return out, proc.returncode | 245 return out, proc.returncode |
246 | 246 |
247 | 247 |
248 def call(args, **kwargs): | 248 def call(args, **kwargs): |
249 """Emulates subprocess.call(). | 249 """Emulates subprocess.call(). |
250 | 250 |
251 Automatically convert stdout=PIPE or stderr=PIPE to VOID. | 251 Automatically convert stdout=PIPE or stderr=PIPE to VOID. |
| 252 In no case they can be returned since no code path raises |
| 253 subprocess2.CalledProcessError. |
252 """ | 254 """ |
253 if kwargs.get('stdout') == PIPE: | 255 if kwargs.get('stdout') == PIPE: |
254 kwargs['stdout'] = VOID | 256 kwargs['stdout'] = VOID |
255 if kwargs.get('stderr') == PIPE: | 257 if kwargs.get('stderr') == PIPE: |
256 kwargs['stderr'] = VOID | 258 kwargs['stderr'] = VOID |
257 return communicate(args, **kwargs)[1] | 259 return communicate(args, **kwargs)[1] |
258 | 260 |
259 | 261 |
260 def check_call_out(args, **kwargs): | 262 def check_call_out(args, **kwargs): |
261 """Improved version of subprocess.check_call(). | 263 """Improved version of subprocess.check_call(). |
(...skipping 12 matching lines...) Expand all Loading... |
274 check_call_out(args, **kwargs) | 276 check_call_out(args, **kwargs) |
275 return 0 | 277 return 0 |
276 | 278 |
277 | 279 |
278 def capture(args, **kwargs): | 280 def capture(args, **kwargs): |
279 """Captures stdout of a process call and returns it. | 281 """Captures stdout of a process call and returns it. |
280 | 282 |
281 Returns stdout. | 283 Returns stdout. |
282 | 284 |
283 - Discards returncode. | 285 - Discards returncode. |
284 - Discards stderr. By default sets stderr=STDOUT. | 286 - Blocks stdin by default if not specified since no output will be visible. |
285 - Blocks stdin by default since no output will be visible. | |
286 """ | 287 """ |
287 if kwargs.get('stdin') is None: | 288 kwargs.setdefault('stdin', VOID) |
288 kwargs['stdin'] = VOID | 289 |
289 if kwargs.get('stdout') is None: | 290 # Like check_output, deny the caller from using stdout arg. |
290 kwargs['stdout'] = PIPE | 291 return communicate(args, stdout=PIPE, **kwargs)[0][0] |
291 if kwargs.get('stderr') is None: | |
292 kwargs['stderr'] = STDOUT | |
293 return communicate(args, **kwargs)[0][0] | |
294 | 292 |
295 | 293 |
296 def check_output(args, **kwargs): | 294 def check_output(args, **kwargs): |
297 """Emulates subprocess.check_output(). | 295 """Emulates subprocess.check_output(). |
298 | 296 |
299 Captures stdout of a process call and returns stdout only. | 297 Captures stdout of a process call and returns stdout only. |
300 | 298 |
301 - Discards stderr. By default sets stderr=STDOUT. | |
302 - Throws if return code is not 0. | 299 - Throws if return code is not 0. |
303 - Works even prior to python 2.7. | 300 - Works even prior to python 2.7. |
304 - Blocks stdin by default since no output will be visible. | 301 - Blocks stdin by default if not specified since no output will be visible. |
| 302 - As per doc, "The stdout argument is not allowed as it is used internally." |
305 """ | 303 """ |
306 if kwargs.get('stdin') is None: | 304 kwargs.setdefault('stdin', VOID) |
307 kwargs['stdin'] = VOID | 305 return check_call_out(args, stdout=PIPE, **kwargs)[0] |
308 if kwargs.get('stdout') is None: | |
309 kwargs['stdout'] = PIPE | |
310 if kwargs.get('stderr') is None: | |
311 kwargs['stderr'] = STDOUT | |
312 return check_call_out(args, **kwargs)[0] | |
OLD | NEW |