popen2

Discussion in 'Python' started by Guy, Aug 12, 2003.

  1. Guy

    Guy Guest

    Hi

    I was given an exstremly useful answer to a problem that I had in
    windows. I'm relativly new with python, but find it exstremly useful.
    I'm creating a script to run test files and record output, the script
    already works on win32 and I've made a telnet procedure which works
    quite well when you want to do the testing on one of the many unix
    boxes (but its very slow). What I want to do now is run the script on
    a unix system, and later on, create a rsh function to run the tests on
    different unix boxes which have not got python on them.

    The following snippet of code is what I use on win32. just example
    sets some env vars and executes an exe, collects and processes the
    output. :

    output, input = popen2("cmd.exe")
    # Sets the enviroment.
    input.write("set LI_DIR=value\n")
    input.write("set LI_DIR_ALT=value\n")
    input.write("set LI_ARCH=value\n")
    input.write("set LI_PRECISION=value\n")
    input.write("set LI_PERFORMANCE=value\n")
    input.write("set LI_LINKING=\n")
    input.write("set LI_ARCHFLAG=\n")
    input.write("set LI\n")

    # Writes commands to it.
    input.write(f_mwtest_exe_str + " " + f_mwtest_args + " " +
    os.path.dirname(f_testlwc_str) + " " + f_testlwc_str + "\n")

    input.write("exit\n")

    while 1:
    text = output.readline()
    if text:
    process text line
    else:
    break:

    The unix equivalant is something like this :

    from popen2 import popen2
    output, input = popen2("csh")
    # Sets the enviroment.
    input.write("setenv DISPLAY doors:0.0\n")

    input.writeinput.write(f_mwtest_exe_str + " " + f_mwtest_args + " " +
    os.path.dirname(f_testlwc_str) + " " + f_testlwc_str + "\n")

    print "finished"
    process the output from the test file.


    The problem is at the moment I can't get the process to stop and wait
    until the exe has finished executing, the python script executes
    everything and then the test file is executed afterwards. I'm thinking
    that there must be a switch on the csh or sh command to stop this
    happening but I don't know what it is. The unix machines that I'm
    working on are all different plat types, so the platform type proberly
    won't help you.

    NOTE the commands above will be a lot more varied, and are not just
    used to set the enviroment up.

    Any help would be of great value to me
    TIA

    Guy
    Guy, Aug 12, 2003
    #1
    1. Advertising

  2. Guy

    Donn Cave Guest

    In article <>,
    (Guy) wrote:

    > The unix equivalant is something like this :
    >
    > from popen2 import popen2
    > output, input = popen2("csh")
    > # Sets the enviroment.
    > input.write("setenv DISPLAY doors:0.0\n")
    >
    > input.writeinput.write(f_mwtest_exe_str + " " + f_mwtest_args + " " +
    > os.path.dirname(f_testlwc_str) + " " + f_testlwc_str + "\n")
    >
    > print "finished"
    > process the output from the test file.
    >
    >
    > The problem is at the moment I can't get the process to stop and wait
    > until the exe has finished executing, the python script executes
    > everything and then the test file is executed afterwards. I'm thinking
    > that there must be a switch on the csh or sh command to stop this
    > happening but I don't know what it is. The unix machines that I'm
    > working on are all different plat types, so the platform type proberly
    > won't help you.


    Try something like
    status = input.close()
    result = output.read()
    if status is None:
    print 'success:', repr(result)
    else:
    print 'error exit', status, repr(result)

    The close() is the important part:

    1. flush your buffer so data actually goes to the shell
    2. closes the shell's input so it doesn't stay around
    waiting for more.
    3. returns exit status for the shell process, or None
    if the exit status is 0. (This won't work so well
    for rsh.)

    While I'm at it, I would recommend that you use "sh" instead
    of "csh", because in the end it will give you less grief with
    bugs and misfeatures. "setenv x v" -> "x=v export x" if you
    do this. I would also recommend popen2("sh 2>&1") so that
    the piped output includes error/diagnostic output.

    Donn Cave,
    Donn Cave, Aug 12, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cherico

    popen2 with large input

    cherico, Jan 29, 2004, in forum: Python
    Replies:
    2
    Views:
    319
    Jeff Epler
    Jan 29, 2004
  2. Diez B. Roggisch

    popen2 trouble

    Diez B. Roggisch, Apr 2, 2004, in forum: Python
    Replies:
    2
    Views:
    307
    Diez B. Roggisch
    Apr 5, 2004
  3. A. Lloyd Flanagan

    Possible problem with popen2 module

    A. Lloyd Flanagan, Apr 30, 2004, in forum: Python
    Replies:
    2
    Views:
    304
    A. Lloyd Flanagan
    May 3, 2004
  4. Guest
    Replies:
    1
    Views:
    471
    Donn Cave
    Jun 15, 2004
  5. Jeffrey Barish

    Bug in popen2.Popen3?

    Jeffrey Barish, Jun 16, 2004, in forum: Python
    Replies:
    1
    Views:
    342
    Donn Cave
    Jun 17, 2004
Loading...

Share This Page