Skip to content

Instantly share code, notes, and snippets.

@maw
Created January 31, 2012 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maw/1711614 to your computer and use it in GitHub Desktop.
Save maw/1711614 to your computer and use it in GitHub Desktop.
replacement for commands.getstatusoutput that prints as it goes
# -*- python -*-
# This meant as a replacement for commands.getstatusoutput. By default, it
# works more or less identically. However, if you pass showoutput=True as
# its second parameter, it will show the process of your command as it goes.
# This is useful for long-running commands whose output is interesting to
# watch as well as to programatically analyse once it's finished.
# Maybe we could also consider a way to pass callbacks, or something, to
# run for each line. Or this could be broken down a bit, with simpler
# and more complex stuff.
def bicommand(command, showoutput=False, read_bytes=1):
import subprocess
import select
import sys
if not showoutput in [True, False]:
raise "showoutput takes a boolean argument only"
pipe = subprocess.Popen(command, shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = ""
while True:
to_read, _, _ = select.select([pipe.stdout, pipe.stderr], [], [], 0.1)
if len(to_read) == 0:
continue
eof = True
for stream in to_read:
line = stream.read(read_bytes)
if line != "":
eof = False
output += line
if showoutput == True:
sys.stdout.write(line)
pass
if eof == True:
break
pass
if output[-1:] == '\n':
output = output[:-1]
pass
status = pipe.wait()
return status, output
if __name__ == "__main__":
def doer(cmd):
import commands
import os
import tempfile
bc_status, bc_output = bicommand(cmd)
c_status, c_output = commands.getstatusoutput(cmd)
if bc_output != c_output:
print "Different output when running the command `%s':" % cmd
bc_tmp = tempfile.mkstemp("bct_bicommand.out")
bc_tmp_file = os.fdopen(bc_tmp[0], "w")
bc_tmp_file.write(bc_output)
bc_tmp_file.flush()
c_tmp = tempfile.mkstemp("bct_commands.getstatusoutput.out")
c_tmp_file = os.fdopen(c_tmp[0], "w")
c_tmp_file.write(c_output)
c_tmp_file.flush()
diff_cmd = "diff -u %s %s" % (c_tmp[1], bc_tmp[1])
print "diff_cmd is `%s'" % diff_cmd
status, output = commands.getstatusoutput(diff_cmd)
print output
print
print
else:
print "Identical output when running the command `%s'" % cmd
pass
pass
cmds = ["ls -1 /tmp", # rarely will fail, will usually be fast
"ls ~", # never (?) will fall, will usually be fast
"uname", # shouldn't ever fail, will be fast
"find /usr/share/YaST2/data", # will sometimes be stderr
"find /usr" # will be slow
]
for cmd in cmds:
doer(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment