mirror of
https://github.com/flatcar/scripts.git
synced 2025-11-15 07:31:36 +01:00
Add shell parameter to allow running commands directly.
For chromite, we often want to run shell commands directly instead of using the array syntax currently accepted by RunCommand. This makes it easier to run piped commands, for example. BUG=chromium-os:6573 TEST=I'm using it in cros_build. Change-Id: Ic846e257fec4e423b0253d06641eac232545f6f6 Review URL: http://codereview.chromium.org/3323019
This commit is contained in:
parent
b3d04a0529
commit
2e79fe48ce
@ -12,12 +12,11 @@ _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
|
|||||||
|
|
||||||
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):
|
cwd=None, input=None, enter_chroot=False, shell=False):
|
||||||
"""Runs a shell command.
|
"""Runs a command.
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
cmd - cmd to run. Should be input to subprocess.POpen. If a string,
|
cmd - cmd to run. Should be input to subprocess.Popen.
|
||||||
converted to an array using split().
|
|
||||||
print_cmd -- prints the command before running it.
|
print_cmd -- prints the command before running it.
|
||||||
error_ok -- does not raise an exception on error.
|
error_ok -- does not raise an exception on error.
|
||||||
error_message -- prints out this message when an error occurrs.
|
error_message -- prints out this message when an error occurrs.
|
||||||
@ -28,6 +27,7 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
|||||||
input -- input to pipe into this command through stdin.
|
input -- input to pipe into this command through stdin.
|
||||||
enter_chroot -- this command should be run from within the chroot. If set,
|
enter_chroot -- this command should be run from within the chroot. If set,
|
||||||
cwd must point to the scripts directory.
|
cwd must point to the scripts directory.
|
||||||
|
shell -- If shell is True, the specified command will be executed through the shell.
|
||||||
Raises:
|
Raises:
|
||||||
Exception: Raises generic exception on error with optional error_message.
|
Exception: Raises generic exception on error with optional error_message.
|
||||||
"""
|
"""
|
||||||
@ -41,21 +41,27 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
|
|||||||
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 input: stdin = subprocess.PIPE
|
if input: stdin = subprocess.PIPE
|
||||||
if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
|
if isinstance(cmd, basestring):
|
||||||
|
if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd
|
||||||
|
cmd_str = cmd
|
||||||
|
else:
|
||||||
|
if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
|
||||||
|
cmd_str = ' '.join(cmd)
|
||||||
|
|
||||||
# Print out the command before running.
|
# Print out the command before running.
|
||||||
if print_cmd:
|
if print_cmd:
|
||||||
Info('RunCommand: %s' % ' '.join(cmd))
|
Info('RunCommand: %s' % cmd_str)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
|
proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
|
||||||
stdout=stdout, stderr=stderr)
|
stdout=stdout, stderr=stderr,
|
||||||
|
shell=shell)
|
||||||
(output, error) = proc.communicate(input)
|
(output, error) = proc.communicate(input)
|
||||||
if exit_code:
|
if exit_code:
|
||||||
return proc.returncode
|
return proc.returncode
|
||||||
|
|
||||||
if not error_ok and proc.returncode:
|
if not error_ok and proc.returncode:
|
||||||
raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) +
|
raise Exception('Command "%s" failed.\n' % cmd_str +
|
||||||
(error_message or error or output or ''))
|
(error_message or error or output or ''))
|
||||||
except Exception,e:
|
except Exception,e:
|
||||||
if not error_ok:
|
if not error_ok:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user