|
From: <di...@us...> - 2010-11-19 19:16:31
|
Revision: 702
http://safekeep.svn.sourceforge.net/safekeep/?rev=702&view=rev
Author: dimi
Date: 2010-11-19 19:16:25 +0000 (Fri, 19 Nov 2010)
Log Message:
-----------
First cut at a decent fallback for the subprocess module
Modified Paths:
--------------
safekeep/trunk/safekeep
Modified: safekeep/trunk/safekeep
===================================================================
--- safekeep/trunk/safekeep 2010-11-19 18:50:23 UTC (rev 701)
+++ safekeep/trunk/safekeep 2010-11-19 19:16:25 UTC (rev 702)
@@ -17,11 +17,17 @@
from __future__ import generators
import getopt, os, os.path, re, sys, fnmatch, stat, types
-import subprocess
import commands, tempfile, time, traceback
import getpass, pwd, xml.dom.minidom
import socket, smtplib
+try:
+ import subprocess
+ from subprocess import PIPE, STDOUT
+except:
+ PIPE = -1
+ STDOUT = -2
+
######################################################################
# Global settings
######################################################################
@@ -108,26 +114,47 @@
debug('Run [' + ' '.join(args) + ']')
_shell = isinstance(args, types.StringTypes)
if stdin:
- _stdin = subprocess.PIPE
+ _stdin = PIPE
else:
_stdin = None
if stdout:
_stderr = None
else:
- _stderr = subprocess.STDOUT
- proc = subprocess.Popen(args, bufsize=1, shell=_shell, stdin=_stdin, stdout=subprocess.PIPE, stderr=_stderr, close_fds=True)
+ _stderr = STDOUT
+ if 'subprocess' in dir():
+ proc = subprocess.Popen(args, bufsize=1, shell=_shell, stdin=_stdin, stdout=PIPE, stderr=_stderr, close_fds=True)
+ child_in = proc.stdin
+ child_out = proc.stdout
+ def do_wait():
+ return proc_wait()
+
+ else:
+ if _shell:
+ args = ["/bin/sh", "-c"].extends(args)
+ if _stderr:
+ (child_in, child_out) = os.popen4(args)
+ else:
+ (child_in, child_out) = os.popen3(args)
+
+ if not stdin:
+ child_in.close()
+
+ def do_wait():
+ return 0
+
if stdin:
- proc.stdin.write(stdin)
- proc.stdin.close()
+ child_in.write(stdin)
+ child_in.close()
+
lines=[]
- for line in proc.stdout:
+ for line in child_out:
if stdout:
lines.append(line)
else:
info(line.rstrip())
- proc.stdout.close()
- return (proc.wait(), ''.join(lines))
+ child_out.close()
+ return (do_wait(), ''.join(lines))
def _spawn(args, stdin=None, stdout=False):
if isinstance(args, types.StringTypes):
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|