mirror of
https://github.com/flatcar/scripts.git
synced 2025-08-08 05:26:58 +02:00
Add combine_stdout_stderr as well as make success less verbose.
Change-Id: If46fd1558a00498ce4721dd756532e916f49bc19 BUG=chromium-os:12950 TEST=Ran with autestharness and ran unittests. Review URL: http://codereview.chromium.org/6877015
This commit is contained in:
parent
eb5246f8d9
commit
c894a0a0a0
@ -28,7 +28,7 @@ def GetCallerName():
|
|||||||
def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
||||||
exit_code=False, redirect_stdout=False, redirect_stderr=False,
|
exit_code=False, redirect_stdout=False, redirect_stderr=False,
|
||||||
cwd=None, input=None, enter_chroot=False, num_retries=0,
|
cwd=None, input=None, enter_chroot=False, num_retries=0,
|
||||||
log_to_file=None):
|
log_to_file=None, combine_stdout_stderr=False):
|
||||||
"""Runs a shell command.
|
"""Runs a shell command.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -46,6 +46,8 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
|||||||
cwd must point to the scripts directory.
|
cwd must point to the scripts directory.
|
||||||
num_retries: the number of retries to perform before dying
|
num_retries: the number of retries to perform before dying
|
||||||
log_to_file: Redirects all stderr and stdout to file specified by this path.
|
log_to_file: Redirects all stderr and stdout to file specified by this path.
|
||||||
|
combine_stdout_stderr: Combines stdout and stdin streams into stdout. Auto
|
||||||
|
set to true if log_to_file specifies a file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
If exit_code is True, returns the return code of the shell command.
|
If exit_code is True, returns the return code of the shell command.
|
||||||
@ -71,6 +73,7 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
|||||||
else:
|
else:
|
||||||
if redirect_stdout: stdout = subprocess.PIPE
|
if redirect_stdout: stdout = subprocess.PIPE
|
||||||
if redirect_stderr: stderr = subprocess.PIPE
|
if redirect_stderr: stderr = subprocess.PIPE
|
||||||
|
if combine_stdout_stderr: stderr = subprocess.STDOUT
|
||||||
|
|
||||||
if input: stdin = subprocess.PIPE
|
if input: stdin = subprocess.PIPE
|
||||||
if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
|
if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
|
||||||
@ -106,13 +109,11 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
|||||||
return proc.returncode
|
return proc.returncode
|
||||||
|
|
||||||
# If the command (and all retries) failed, handle error result
|
# If the command (and all retries) failed, handle error result
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0 and not error_ok:
|
||||||
if error_ok:
|
error_info = ('Command "%r" failed.\n' % (cmd) +
|
||||||
Warning('Command "%r" failed.\n' % (cmd) +
|
(error_message or error or output or ''))
|
||||||
(error_message or error or output or ''))
|
if log_to_file: error_info += '\nOutput logged to %s' % log_to_file
|
||||||
else:
|
raise RunCommandException(error_info)
|
||||||
raise RunCommandException('Command "%r" failed.\n' % (cmd) +
|
|
||||||
(error_message or error or output or ''))
|
|
||||||
|
|
||||||
# return final result
|
# return final result
|
||||||
return output
|
return output
|
||||||
|
@ -31,7 +31,6 @@ function blocking_kill() {
|
|||||||
local timeout=1
|
local timeout=1
|
||||||
sudo kill -$2 $1
|
sudo kill -$2 $1
|
||||||
while ps -p $1 > /dev/null && [ ${timeout} -le $3 ]; do
|
while ps -p $1 > /dev/null && [ ${timeout} -le $3 ]; do
|
||||||
warn "Process still running, sleeping for ${timeout}"
|
|
||||||
sleep ${timeout}
|
sleep ${timeout}
|
||||||
timeout=$((timeout*2))
|
timeout=$((timeout*2))
|
||||||
done
|
done
|
||||||
@ -129,7 +128,6 @@ function stop_kvm() {
|
|||||||
echo "Stopping the KVM instance" >&2
|
echo "Stopping the KVM instance" >&2
|
||||||
local pid=$(get_pid)
|
local pid=$(get_pid)
|
||||||
if [ -n "${pid}" ]; then
|
if [ -n "${pid}" ]; then
|
||||||
echo "Killing ${pid}" >&2
|
|
||||||
blocking_kill ${pid} 1 16 || blocking_kill 9 1
|
blocking_kill ${pid} 1 16 || blocking_kill 9 1
|
||||||
sudo rm "${KVM_PID_FILE}"
|
sudo rm "${KVM_PID_FILE}"
|
||||||
else
|
else
|
||||||
|
Loading…
Reference in New Issue
Block a user