469,926 Members | 2,174 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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

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.

Aug 16 '05 #1
6 2253
mhenry1384 wrote:
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

Aug 16 '05 #2
>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. :-)

Aug 16 '05 #3
mhenry1384 wrote:
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)
Aug 16 '05 #4
mhenry1384 wrote:
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
Aug 17 '05 #5
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.

Aug 17 '05 #6
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 );
#-----------------------------------------------------------

Aug 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve | last post: by
7 posts views Thread by Stuart McGraw | last post: by
reply views Thread by Matt Price | last post: by
5 posts views Thread by runes | last post: by
8 posts views Thread by Paul Cochrane | last post: by
1 post views Thread by saibotorama | last post: by
22 posts views Thread by bambam | last post: by
1 post views Thread by Calder Coalson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.