472,351 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

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 2347
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi, I want to know if I can 'pipe' output from a program into a python script. Is that possible? I need to pipe some output at runtime from a...
7
by: Stuart McGraw | last post by:
I just spent a $*#@!*&^&% hour registering at ^$#@#%^ Sourceforce and trying to submit a Python bug report but it still won't let me. I give up. ...
0
by: Matt Price | last post by:
Hi folks, I'm writing a python script which I plan to use in a macro for mutt, the mailer. The script is supposed to generate and send an email...
5
by: runes | last post by:
I trying to figure out a way to make a python script accept data output from another process under Windows XP, but I fail miserably. I have a vague...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the...
1
by: saibotorama | last post by:
What is the Python translation for this Bash statement: tar cf - "${files}" | bzip2 "$file".tar.bz2 (Ignoring the fact that "tar cjf" also...
22
by: bambam | last post by:
I have about 30 pages (10 * 3 pages each) of code like this (following). Can anyone suggest a more compact way to code the exception handling? If...
1
by: Calder Coalson | last post by:
I was searching for ways to get the return value from os.system() calls, when I came across this from 6 years ago. I tried what was suggested in...
1
by: Ivan Ven Osdel | last post by:
The free Python editors/IDEs really do need work as far as code completion goes but I am hopeful. IMO Stani's Python Editor comes closest by...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.