# vim: set filencoding=utf8 """ Miscellaneous Utility Commands @author: Mike Crute (mcrute@ag.com) @organization: SoftGroup Interactive, Inc. @date: April 20, 2010 """ import shlex from subprocess import Popen, PIPE from obalie.exceptions import ExecutableError def run_command(command, raw_output=False): """ Run a command string returning the output. Passing raw_output will cause the function to return the raw stdout; otherwise lines will be split. """ cmd = shlex.split(command.encode('ascii')) proc = Popen(cmd, close_fds=False, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if proc.returncode != 0: raise ExecutableError(proc.returncode, command) if raw_output: return stdout else: return stdout.splitlines(True)