# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+"""A runtool which wraps subprocess related"""
+
import os
+import shlex
import subprocess
def runtool(cmdln_or_args, catch=1):
return None
if isinstance(cmdln_or_args, list):
- cmd = cmdln_or_args[0]
- shell = False
+ cmd, shell = cmdln_or_args[0], False
else:
- import shlex
- cmd = shlex.split(cmdln_or_args)[0]
- shell = True
+ cmd, shell = shlex.split(cmdln_or_args)[0], True
if catch != 3:
dev_null = os.open("/dev/null", os.O_WRONLY)
if catch == 0:
- sout = dev_null
- serr = dev_null
+ sout, serr = dev_null, dev_null
elif catch == 1:
- sout = subprocess.PIPE
- serr = dev_null
+ sout, serr = subprocess.PIPE, dev_null
elif catch == 2:
- sout = dev_null
- serr = subprocess.PIPE
+ sout, serr = dev_null, subprocess.PIPE
elif catch == 3:
- sout = subprocess.PIPE
- serr = subprocess.STDOUT
+ sout, serr = subprocess.PIPE, subprocess.STDOUT
try:
process = subprocess.Popen(cmdln_or_args, stdout=sout,
if catch != 3:
os.close(dev_null)
- #print 'execute cmd: %s\nreturncode: %s\n%s' %(cmdln_or_args, process.returncode, out)
return (process.returncode, out)
def show(cmdln_or_args):
- # show all the message
+ """Show all the message"""
rcode, out = runtool(cmdln_or_args, catch=3)
cmd = cmdln_or_args
msg = 'running command: "%s"' % cmd
- if out: out = out.strip()
- if out:
+ if out and out.strip():
msg += ', with output::'
msg += '\n +----------------'
for line in out.splitlines():
return rcode, out
def outs(cmdln_or_args, catch=1):
- # get the outputs of tools
+ """Get the outputs of tools"""
return runtool(cmdln_or_args, catch)[1].strip()
def quiet(cmdln_or_args):
+ """Execute a cmd quiet, no debug information printed"""
return runtool(cmdln_or_args, catch=0)[0]
def embed(cmdln_or_args):
- # embed shell script into python frame code
+ """Eembed shell script into python frame code"""
if isinstance(cmdln_or_args, list):
args = cmdln_or_args
else:
- import shlex
args = shlex.split(cmdln_or_args)
try: