cross platform (linux/windows) problem

Miah Gregory miah at picsel.com
Wed Feb 27 04:52:53 EST 2002


Hi,

Looking through the google newsgroup archive, I've noticed that other
people have had similar problems to mine, but I couldn't find a solution
which fitted me well enough.

I'm trying to write a script, which will run on windows and linux. It's job
is to loop continuously, running an executable passed in as a command line
parameter each time, until that executable exits with a non-zero exit code.
Some exit codes, however, should not stop the script, so I need to know
the specific exit code, and not just whether it was non-zero or not.

The other requirement is that the stdout/stderr from the executable being
run should not be displayed normally, but I need a way to actually display
that output when a suitable exit code is returned.

Simple example which works for linux:

import popen2

testapp = ""
for arg in sys.argv[1:len(sys.argv)]:
    testapp = testapp + " " + arg

while 1:
    p = popen2.Popen3(testapp)
    p.wait
    
    retval   = p.poll()
    exitcode = (retval >> 8) & 0xFF
    signal   = retval & 0xFF
    
    if (exitcode != 0):
        if (exitcode != 2):
            print p.fromchild.read()
            sys.exit(1)
    else:
        sys.exit(0)


This however doesn't work for windows, as it seems the popen call isn't
implemented there.

I've also noticed that the return code from os.system under win2k seems
to be reversed, ie. the exitcode is returned as the signal in the above
script.

This is ok, though, as I can do an explicit check to get around that:

if sys.platform == "win32":
    exitcode = signal
    signal   = 0

Does anyone have any suggestions as to how, if at all, I can make the above
work on both platforms?

Thanks in advance.

-- 
Miah Gregory



More information about the Python-list mailing list