diff options
Diffstat (limited to 'obalie/utils.py')
-rw-r--r-- | obalie/utils.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/obalie/utils.py b/obalie/utils.py new file mode 100644 index 0000000..dcf476a --- /dev/null +++ b/obalie/utils.py | |||
@@ -0,0 +1,32 @@ | |||
1 | # vim: set filencoding=utf8 | ||
2 | """ | ||
3 | Miscellaneous Utility Commands | ||
4 | |||
5 | @author: Mike Crute (mcrute@ag.com) | ||
6 | @organization: SoftGroup Interactive, Inc. | ||
7 | @date: April 20, 2010 | ||
8 | """ | ||
9 | |||
10 | |||
11 | import shlex | ||
12 | from subprocess import Popen, PIPE | ||
13 | from obalie.exceptions import ExecutableError | ||
14 | |||
15 | |||
16 | def run_command(command, raw_output=False): | ||
17 | """ | ||
18 | Run a command string returning the output. Passing raw_output | ||
19 | will cause the function to return the raw stdout; otherwise | ||
20 | lines will be split. | ||
21 | """ | ||
22 | cmd = shlex.split(command.encode('ascii')) | ||
23 | proc = Popen(cmd, close_fds=False, stdout=PIPE, stderr=PIPE) | ||
24 | stdout, stderr = proc.communicate() | ||
25 | |||
26 | if proc.returncode != 0: | ||
27 | raise ExecutableError(proc.returncode, command) | ||
28 | |||
29 | if raw_output: | ||
30 | return stdout | ||
31 | else: | ||
32 | return stdout.splitlines(True) | ||