I have attached a patch for commands.py to provide a callback ability. Example use:
-------------------------------------
import commands
cmd = 'top -b -n2'
def fancy(out):
print 'GOT(%s)' % out.strip()
commands.cb = fancy
(s,o) = commands.getstatusoutput(cmd)
print 'OUTPUT (%s)' % o
-------------------------------------
I am not sure if this is the proper forum or means to submit something like this, so please forgive me and advise accordingly if I am in error. The basic idea is obvious, to allow long-running commands to call back whenever there is output. This is against python 2.4. Please let me know if you have any questions or suggestions.
thanks!!
-brad
--- commands.py.ORIG 2006-06-30 12:19:40.000000000 -0600
+++ commands.py 2006-07-05 09:15:29.000000000 -0600
@@ -47,16 +47,28 @@
# Ditto but preserving the exit status.
# Returns a pair (sts, output)
#
+
def getstatusoutput(cmd):
- """Return (status, output) of executing cmd in a shell."""
+
import os
- pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
- text = pipe.read()
- sts = pipe.close()
+
+ try:
+ myCB = cb
+ except:
+ myCB = None
+
+ text = ''
+
+ (stdin,stdout) = os.popen2( '{ ' + cmd + '; } 2>&1', 'r')
+ for line in stdout:
+ if myCB != None:
+ myCB(line)
+ text += line
+ sts = stdout.close()
+
if sts is None: sts = 0
if text[-1:] == '\n': text = text[:-1]
- return sts, text
-
+ return sts,text
# Make command argument from directory and pathname (prefix space, add quotes).
#_______________________________________________ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
