| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """A wrapper for subprocess to make calling shell commands easier.""" | 5 """A wrapper for subprocess to make calling shell commands easier.""" |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import logging | 8 import logging |
| 9 import pipes | 9 import pipes |
| 10 import signal | 10 import signal |
| 11 import subprocess | 11 import subprocess |
| 12 import tempfile | 12 import tempfile |
| 13 | 13 |
| 14 import constants | 14 import constants |
| 15 | 15 |
| 16 | 16 |
| 17 def Call(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): | 17 def Popen(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): |
| 18 return subprocess.call( | 18 return subprocess.Popen( |
| 19 args=args, cwd=cwd, stdout=stdout, stderr=stderr, | 19 args=args, cwd=cwd, stdout=stdout, stderr=stderr, |
| 20 shell=shell, close_fds=True, env=env, | 20 shell=shell, close_fds=True, env=env, |
| 21 preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)) | 21 preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 22 | 22 |
| 23 | 23 |
| 24 def Call(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): |
| 25 pipe = Popen(args, stdout=stdout, stderr=stderr, shell=shell, cwd=cwd, |
| 26 env=env) |
| 27 pipe.communicate() |
| 28 return pipe.wait() |
| 29 |
| 30 |
| 24 def RunCmd(args, cwd=None): | 31 def RunCmd(args, cwd=None): |
| 25 """Opens a subprocess to execute a program and returns its return value. | 32 """Opens a subprocess to execute a program and returns its return value. |
| 26 | 33 |
| 27 Args: | 34 Args: |
| 28 args: A string or a sequence of program arguments. The program to execute is | 35 args: A string or a sequence of program arguments. The program to execute is |
| 29 the string or the first item in the args sequence. | 36 the string or the first item in the args sequence. |
| 30 cwd: If not None, the subprocess's current directory will be changed to | 37 cwd: If not None, the subprocess's current directory will be changed to |
| 31 |cwd| before it's executed. | 38 |cwd| before it's executed. |
| 32 | 39 |
| 33 Returns: | 40 Returns: |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 tmperr.close() | 97 tmperr.close() |
| 91 if stderr: | 98 if stderr: |
| 92 logging.critical(stderr) | 99 logging.critical(stderr) |
| 93 tmpout.seek(0) | 100 tmpout.seek(0) |
| 94 stdout = tmpout.read() | 101 stdout = tmpout.read() |
| 95 tmpout.close() | 102 tmpout.close() |
| 96 if len(stdout) > 4096: | 103 if len(stdout) > 4096: |
| 97 logging.debug('Truncated output:') | 104 logging.debug('Truncated output:') |
| 98 logging.debug(stdout[:4096]) | 105 logging.debug(stdout[:4096]) |
| 99 return (exit_code, stdout) | 106 return (exit_code, stdout) |
| OLD | NEW |