From d27c00e9794f72fba4ae71961e36d535701fd094 Mon Sep 17 00:00:00 2001 From: wirawan Date: Wed, 20 Jan 2010 17:25:54 +0000 Subject: [PATCH] * Added pipe_out to pass the output of a spawn command to python. This is equivalent to backtick operator with optional line splitting. --- shell_tools.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/shell_tools.py b/shell_tools.py index b34a280..d779d53 100644 --- a/shell_tools.py +++ b/shell_tools.py @@ -1,4 +1,4 @@ -# $Id: shell_tools.py,v 1.2 2010-01-18 20:55:44 wirawan Exp $ +# $Id: shell_tools.py,v 1.3 2010-01-20 17:25:54 wirawan Exp $ # # wpylib.shell_tools # Created: 20100106 @@ -42,12 +42,25 @@ def errchk(cmd, args, retcode): err = "Command %s returned %d" % (cmd, retcode) raise RuntimeError, err + def run(prg, args): retcode = subprocess.call((prg,) + args) errchk(prg, args, retcode) return 0 +def pipe_out(args, split=False, shell=False): + """Executes a shell command, piping out the stdout to python for parsing. + This is my customary shortcut for backtick operator. + The result is either a single string (if split==False) or a list of strings + with EOLs removed (if split==True).""" + retval = subprocess.Popen(args, stdout=subprocess.PIPE, shell=shell).communicate()[0] + if not split: + return retval + else: + return retval.splitlines() + + # coreutils def cp(*args):