|
From: <di...@us...> - 2010-11-19 14:56:52
|
Revision: 687
http://safekeep.svn.sourceforge.net/safekeep/?rev=687&view=rev
Author: dimi
Date: 2010-11-19 14:56:46 +0000 (Fri, 19 Nov 2010)
Log Message:
-----------
Provide a more expressive function to call external commands
and deal with the standard input/output.
Modified Paths:
--------------
safekeep/trunk/safekeep
Modified: safekeep/trunk/safekeep
===================================================================
--- safekeep/trunk/safekeep 2010-11-19 14:41:04 UTC (rev 686)
+++ safekeep/trunk/safekeep 2010-11-19 14:56:46 UTC (rev 687)
@@ -112,7 +112,7 @@
else:
return args
-def do_spawn(args, shell=False, stdout=False):
+def do_spawn(args, shell=False, stdin=None, stdout=False):
global cmd
argslist = args_to_list(args)
cmd = argslist[0]
@@ -124,6 +124,8 @@
# Otherwise split into separate elements.
debug('Run [' + ' '.join(argslist) + ']')
proc = subprocess.Popen(argslist, bufsize=1, shell=False, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
+ if stdin:
+ proc.stdin.write(stdin)
proc.stdin.close()
lines=[]
for line in proc.stdout:
@@ -134,12 +136,14 @@
proc.stdout.close()
return (proc.wait(), ''.join(lines))
-def spawn(args, shell=False):
+def _spawn(args, shell=False, stdin=None, stdout=False):
global cmd
try:
- rc, out = do_spawn(args, shell=shell)
+ rc, out = do_spawn(args, shell, stdin, stdout)
except OSError, ex:
- error("OSError: %s: %s" % (cmd, ex))
+ ret = "OSError: %s" % (ex)
+ error('%s failed: %s' % (cmd, ret));
+ return ret
if not rc:
ret = None
@@ -151,8 +155,23 @@
ret = 'unknown exit status: %d' % rc
if ret:
error('%s failed: %s' % (cmd, ret));
- return ret
+ return (ret, out)
+# this just spawns an external program (optionally through a shell)
+# and returns True it it fails, and False if it successed
+def spawn(args, shell=False):
+ rc, out = _spawn(args, shell)
+ return rc
+
+# this spawans an external program (optionally through a shell),
+# feeds it any input via stdin, captures the output and returns it.
+# if it fails it returns None, otherwise it returns the output
+def call(args, shell=False, stdin=None):
+ rc, out = _spawn(args, shell, stdin, stdout=True)
+ if not rc:
+ return None
+ return out
+
def try_to_run(args):
try:
rc, out = do_spawn(args)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|