summaryrefslogtreecommitdiff
path: root/obalie/utils.py
blob: dcf476ae8d1546ec80ac72a70a22802a73d6d108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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)