get the return code when piping something to a python script?

M

mhenry1384

On WinXP, I am doing this

nant.exe | python MyFilter.py

This command always returns 0 (success) because MyFilter.py always
succeeds.

MyFilter.py looks like this

while 1:
line = sys.stdin.readline()
if not line:
break
...
sys.stdout.write(line)
sys.stdout.flush()

How do I set the return code from MyFilter.py based on the return of
nant.exe? Is this possible? I have googled around for an hour wihtout
success. I understand that I could have MyFilter.py call "nant.exe",
but for various reasons, that's not idea for my situation.
 
B

BranoZ

mhenry1384 said:
On WinXP, I am doing this

nant.exe | python MyFilter.py

How do I set the return code from MyFilter.py based on the return of
nant.exe? Is this possible?

I don't know how it is on WinXP, but in UNIX you IMHO cannot easily
get the retcode of the peer _if_ the pipe was created by your common
parent, the shell. Only the parent knows retcodes of its children.

You can communicate the status over the pipe. E.g. when producer will
successufuly finish it will write (let say) last line with only one '.'

Didn't help you much..

BranoZ
 
M

mhenry1384

Didn't help you much..

Thanks, actually even hints that it's not possible helps. So I won't
keep driving myself crazy figuring out how to do it. :)
 
P

Paul Watson

mhenry1384 said:
On WinXP, I am doing this

nant.exe | python MyFilter.py

This command always returns 0 (success) because MyFilter.py always
succeeds.

MyFilter.py looks like this

while 1:
line = sys.stdin.readline()
if not line:
break
...
sys.stdout.write(line)
sys.stdout.flush()

How do I set the return code from MyFilter.py based on the return of
nant.exe? Is this possible? I have googled around for an hour wihtout
success. I understand that I could have MyFilter.py call "nant.exe",
but for various reasons, that's not idea for my situation.

As you have heard, it may not be possible to get the exit code from a
command line pipe.

Of course, even if you could, this code does not yet set an exit code.

sys.exit(exitcode)
 
J

Jeff Schwab

mhenry1384 said:
On WinXP, I am doing this

nant.exe | python MyFilter.py

This command always returns 0 (success) because MyFilter.py always
succeeds.
....

How do I set the return code from MyFilter.py based on the return of
nant.exe? Is this possible? I have googled around for an hour wihtout
success. I understand that I could have MyFilter.py call "nant.exe",
but for various reasons, that's not idea for my situation.

It's probably not possible. The reason is that the left-most process in
the pipe is not guaranteed to exit before the right-most process, so in
general, there may not be any return code to get. If you're not too
attached to cmd.exe, you might want to try a different shell that can
provide a similar effect to what you requested. For example, in bash,
you might set the pipefail option.


$ ls nonesuch | ./return_zero.py
ls: nonesuch: No such file or directory
$ echo $? # Print the status of the last foreground pipe.
0
$ set -o pipefail
$ ls nonesuch | ./return_zero.py
ls: nonesuch: No such file or directory
$ echo $?
2
 
M

mhenry1384

Now that I know it's not possible to do what I wanted, I can work
around this without a problem. I'll just scan the output of the .exe
to determine whether it appears to have failed or not.

Thanks to everyone for the info.
 
C

chris

I use the following when I have an external command to do:

#----------------------------------------------------
def doCommand( cmd,forked=False,runFrom=None ):
"""
Do a command, and return the (stdout+stderr) and the exit status.
This was written to work both on Windows and *nix, and in the
limited
testing to which it has been subjected, it works well.
"""

import sys;
import os;

exitcode = None;
output = None;
origin = None;

if runFrom:
origin = os.getcwd()
try:
os.chdir( runFrom );
except:
pass;

# "forked" to us means "run in background and forget about it".
The
# method of execution is the same on both windows and unix
if forked:
theCommand = cmd.split()[0];
theArgs = cmd.split(); # Include the cmd itself in the
v.
# Guaranteed to be a list.

# P_DETACH: we don't want the process to be our child, and
# we don't want to know what happens to it... Some father!
exitstatus = os.spawnve(
os.P_DETACH,theCommand,theArgs,os.environ );

# if we're not spawning off a separate child, then we do care about
# the results of the process, and execution is different between
# windows and unix
else:
if( sys.platform == "win32" ):
import win32pipe;
(stdin,stdout) = win32pipe.popen4( cmd,'t' );

stdin.close();
output = stdout.read();
try:
exitcode = stdout.close() or 0;
except IOError:
exitcode = ( -1 );

else:
import commands;
( exitstatus,output ) = commands.getstatusoutput(cmd)

#---- exitstatus is a smashing of the return value and any
signal
# sent back from the child process... break them out.
exitcode = (( exitstatus >> 8) & 0xFF );
signal = ( exitstatus & 0xFF );


if runFrom:
#return( 0,"Returning to %s from %s" %(origin,os.getcwd()) )
os.chdir( origin );

return( exitcode,output );
#-----------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top