* Added pipe_out to pass the output of a spawn command to python.

This is equivalent to backtick operator with optional line splitting.
master
wirawan 15 years ago
parent 86c7558758
commit d27c00e979
  1. 15
      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 # wpylib.shell_tools
# Created: 20100106 # Created: 20100106
@ -42,12 +42,25 @@ def errchk(cmd, args, retcode):
err = "Command %s returned %d" % (cmd, retcode) err = "Command %s returned %d" % (cmd, retcode)
raise RuntimeError, err raise RuntimeError, err
def run(prg, args): def run(prg, args):
retcode = subprocess.call((prg,) + args) retcode = subprocess.call((prg,) + args)
errchk(prg, args, retcode) errchk(prg, args, retcode)
return 0 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 # coreutils
def cp(*args): def cp(*args):

Loading…
Cancel
Save