A tale of two execs
aha
aquil.abdullah at gmail.com
Mon Feb 23 16:54:06 EST 2009
Hello All,
So below is my naive attempt at the wrapper, it is only half baked
since I am no master at Interprocess communication... I know that it
is lacking a lot of things comment greatly appreciated:
#!/usr/bin/env python
import os, sys, re, exceptions
try:
import subprocess
except ImportError:
import popen2
subprocess = None
PIPE = 10
SOUT = 11
class Runner:
def __init__(self):
self.stdout = None
self.stdin = None
self.stderr = None
self.pid = None
self.process = None
def runCmdLine(self, args, executable=None, stdin=None,
stdout=None, stderr=None, preexec_fn=None,
close_fds=False, shell=False,cwd=None, env=None):
"""
args -- a string, or a sequence of program arguments. The
argument to execute is
normally the first item in the args sequence or the string if
a string is given,
but can be explicitly set by using the executable argument.
executable -- string to be executed
stdin, stdout, stderr -- value of standard input, standard
output, and standard
error file handles. None implies no redirection. Valid values
are PIPE, an existing
file descriptor (a positive integer), an existing file object,
and None.
shell -- specifies whether command should be executed through
shell
cwd -- specifies whether, childs current directory will be
changed to cwd
env -- A map that specifies the environment variables in child
process
that will be overwritten
NOTE: Depending on the modules available, some arguments may
not be used.
"""
if subprocess:
# Use subprocess to exec functions
if stdin == PIPE:
sin = subprocess.PIPE
else:
# NOTE: User may input invalid values that will cause
exceptions
sin = stdin
if stdout == PIPE:
sout = subprocess.PIPE
else:
sout = stdout
if stderr == SOUT:
serr = subprocess.STDOUT
else:
serr = stderr
self.process = subprocess.Popen(args, stdin=sin,
stdout=sout, stderr=serr,
preexec_fn=preexec_fn,close_fds=close_fds, shell=shell,
env=env, cwd=cwd)
self.pid = self.process.pid
# Add attributes stdin, stdout, and stderr
self.stdin = self.process.stdin
self.stdout = self.process.stdout
self.stderr = self.process.stderr
else:
# Use popen2 to exec functions
# Determine which form of popen2 to use
if stderr == SOUT or stderr == None:
self.process = popen2.Popen4(args)
self.stdin = self.process.tochild
self.stdout = self.process.fromchild
self.stderr = None
else:
self.process = popen2.Popen3(args)
self.stdin = self.process.tochild
self.stdout = self.process.fromchild
self.stderr = self.process.childerr
self.pid = self.process.pid
def wait(self,):
"""
Waits for and returns the status code of the child process.
"""
return self.process.wait()
def poll(self,):
"""
Returns -1 if the child process hasn't completed yet, or it's
return code
otherwise
"""
return self.process.poll()
Aquil
On Feb 23, 2:10 pm, Christian Heimes <li... at cheimes.de> wrote:
> aha wrote
>
> > def runner(cmd, stdin, stdout, ...):
> > try:
> > import subprocess
> > sbm = 1
> > except:
> > sbm = 0
>
> > # Now do something
> > if sbm:
> > process = subporcess(...)
> > else:
> > import popen2
> > process = popen2.Popen4(...)
>
> > Has anyone else run into a situation similar to this one?
>
> The canonical way for your try/except clause is:
>
> try:
> import subprocess
> except ImportError:
> subprocess = None
>
> ...
>
> if subprocess is not None:
> ...
> else:
> ...
>
> Christian
More information about the Python-list
mailing list