| 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 | |
| 8 import logging | 7 import logging |
| 9 import pipes | 8 import pipes |
| 10 import signal | 9 import signal |
| 11 import subprocess | 10 import subprocess |
| 12 import tempfile | 11 import tempfile |
| 13 | 12 |
| 14 import constants | 13 from utils import timeout_retry |
| 15 | 14 |
| 16 | 15 |
| 17 def Popen(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): | 16 def Popen(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): |
| 18 return subprocess.Popen( | 17 return subprocess.Popen( |
| 19 args=args, cwd=cwd, stdout=stdout, stderr=stderr, | 18 args=args, cwd=cwd, stdout=stdout, stderr=stderr, |
| 20 shell=shell, close_fds=True, env=env, | 19 shell=shell, close_fds=True, env=env, |
| 21 preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)) | 20 preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 22 | 21 |
| 23 | 22 |
| 24 def Call(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): | 23 def Call(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 """Executes a subprocess and returns its exit code and output. | 65 """Executes a subprocess and returns its exit code and output. |
| 67 | 66 |
| 68 Args: | 67 Args: |
| 69 args: A string or a sequence of program arguments. The program to execute is | 68 args: A string or a sequence of program arguments. The program to execute is |
| 70 the string or the first item in the args sequence. | 69 the string or the first item in the args sequence. |
| 71 cwd: If not None, the subprocess's current directory will be changed to | 70 cwd: If not None, the subprocess's current directory will be changed to |
| 72 |cwd| before it's executed. | 71 |cwd| before it's executed. |
| 73 shell: Whether to execute args as a shell command. | 72 shell: Whether to execute args as a shell command. |
| 74 | 73 |
| 75 Returns: | 74 Returns: |
| 76 The tuple (exit code, output). | 75 The 2-tuple (exit code, output). |
| 77 """ | 76 """ |
| 78 if isinstance(args, basestring): | 77 if isinstance(args, basestring): |
| 79 args_repr = args | 78 args_repr = args |
| 80 if not shell: | 79 if not shell: |
| 81 raise Exception('string args must be run with shell=True') | 80 raise Exception('string args must be run with shell=True') |
| 82 elif shell: | 81 elif shell: |
| 83 raise Exception('array args must be run with shell=False') | 82 raise Exception('array args must be run with shell=False') |
| 84 else: | 83 else: |
| 85 args_repr = ' '.join(map(pipes.quote, args)) | 84 args_repr = ' '.join(map(pipes.quote, args)) |
| 86 | 85 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 tmperr.close() | 96 tmperr.close() |
| 98 if stderr: | 97 if stderr: |
| 99 logging.critical(stderr) | 98 logging.critical(stderr) |
| 100 tmpout.seek(0) | 99 tmpout.seek(0) |
| 101 stdout = tmpout.read() | 100 stdout = tmpout.read() |
| 102 tmpout.close() | 101 tmpout.close() |
| 103 if len(stdout) > 4096: | 102 if len(stdout) > 4096: |
| 104 logging.debug('Truncated output:') | 103 logging.debug('Truncated output:') |
| 105 logging.debug(stdout[:4096]) | 104 logging.debug(stdout[:4096]) |
| 106 return (exit_code, stdout) | 105 return (exit_code, stdout) |
| 106 |
| 107 |
| 108 def GetCmdStatusAndOutputWithTimeoutAndRetries(args, timeout, retries): |
| 109 """Executes a subprocess with a timeout and retries. |
| 110 |
| 111 Args: |
| 112 args: List of arguments to the program, the program to execute is the first |
| 113 element. |
| 114 timeout: the timeout in seconds. |
| 115 retries: the number of retries. |
| 116 |
| 117 Returns: |
| 118 The 2-tuple (exit code, output). |
| 119 """ |
| 120 return timeout_retry.Run(GetCmdStatusAndOutput, timeout, retries, [args]) |
| OLD | NEW |