| 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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 249 |
| 250 Automatically convert stdout=PIPE or stderr=PIPE to VOID. | 250 Automatically convert stdout=PIPE or stderr=PIPE to VOID. |
| 251 """ | 251 """ |
| 252 if kwargs.get('stdout') == PIPE: | 252 if kwargs.get('stdout') == PIPE: |
| 253 kwargs['stdout'] = VOID | 253 kwargs['stdout'] = VOID |
| 254 if kwargs.get('stderr') == PIPE: | 254 if kwargs.get('stderr') == PIPE: |
| 255 kwargs['stderr'] = VOID | 255 kwargs['stderr'] = VOID |
| 256 return communicate(args, **kwargs)[1] | 256 return communicate(args, **kwargs)[1] |
| 257 | 257 |
| 258 | 258 |
| 259 def check_call(args, **kwargs): | 259 def check_call_out(args, **kwargs): |
| 260 """Improved version of subprocess.check_call(). | 260 """Improved version of subprocess.check_call(). |
| 261 | 261 |
| 262 Returns (stdout, stderr), unlike subprocess.check_call(). | 262 Returns (stdout, stderr), unlike subprocess.check_call(). |
| 263 """ | 263 """ |
| 264 out, returncode = communicate(args, **kwargs) | 264 out, returncode = communicate(args, **kwargs) |
| 265 if returncode: | 265 if returncode: |
| 266 raise CalledProcessError( | 266 raise CalledProcessError( |
| 267 returncode, args, kwargs.get('cwd'), out[0], out[1]) | 267 returncode, args, kwargs.get('cwd'), out[0], out[1]) |
| 268 return out | 268 return out |
| 269 | 269 |
| 270 | 270 |
| 271 def check_call(args, **kwargs): |
| 272 """Emulate subprocess.check_call().""" |
| 273 check_call_out(args, **kwargs) |
| 274 return 0 |
| 275 |
| 276 |
| 271 def capture(args, **kwargs): | 277 def capture(args, **kwargs): |
| 272 """Captures stdout of a process call and returns it. | 278 """Captures stdout of a process call and returns it. |
| 273 | 279 |
| 274 Returns stdout. | 280 Returns stdout. |
| 275 | 281 |
| 276 - Discards returncode. | 282 - Discards returncode. |
| 277 - Discards stderr. By default sets stderr=STDOUT. | 283 - Discards stderr. By default sets stderr=STDOUT. |
| 278 - Blocks stdin by default since no output will be visible. | 284 - Blocks stdin by default since no output will be visible. |
| 279 """ | 285 """ |
| 280 if kwargs.get('stdin') is None: | 286 if kwargs.get('stdin') is None: |
| 281 kwargs['stdin'] = VOID | 287 kwargs['stdin'] = VOID |
| 282 if kwargs.get('stdout') is None: | 288 if kwargs.get('stdout') is None: |
| 283 kwargs['stdout'] = PIPE | 289 kwargs['stdout'] = PIPE |
| 284 if kwargs.get('stderr') is None: | 290 if kwargs.get('stderr') is None: |
| 285 kwargs['stderr'] = STDOUT | 291 kwargs['stderr'] = STDOUT |
| 286 return communicate(args, **kwargs)[0][0] | 292 return communicate(args, **kwargs)[0][0] |
| 287 | 293 |
| 288 | 294 |
| 289 def check_output(args, **kwargs): | 295 def check_output(args, **kwargs): |
| 290 """Captures stdout of a process call and returns it. | 296 """Emulates subprocess.check_output(). |
| 291 | 297 |
| 292 Returns stdout. | 298 Captures stdout of a process call and returns stdout only. |
| 293 | 299 |
| 294 - Discards stderr. By default sets stderr=STDOUT. | 300 - Discards stderr. By default sets stderr=STDOUT. |
| 295 - Throws if return code is not 0. | 301 - Throws if return code is not 0. |
| 296 - Works even prior to python 2.7. | 302 - Works even prior to python 2.7. |
| 297 - Blocks stdin by default since no output will be visible. | 303 - Blocks stdin by default since no output will be visible. |
| 298 """ | 304 """ |
| 299 if kwargs.get('stdin') is None: | 305 if kwargs.get('stdin') is None: |
| 300 kwargs['stdin'] = VOID | 306 kwargs['stdin'] = VOID |
| 301 if kwargs.get('stdout') is None: | 307 if kwargs.get('stdout') is None: |
| 302 kwargs['stdout'] = PIPE | 308 kwargs['stdout'] = PIPE |
| 303 if kwargs.get('stderr') is None: | 309 if kwargs.get('stderr') is None: |
| 304 kwargs['stderr'] = STDOUT | 310 kwargs['stderr'] = STDOUT |
| 305 return check_call(args, **kwargs)[0] | 311 return check_call_out(args, **kwargs)[0] |
| OLD | NEW |