pythonlinuxncursessconscrosstool-ng

Using Python subprocess.call() to launch an ncurses process


I'm trying to call ct-ng (http://crosstool-ng.org/) from a SCons SConstruct script, so basically from Python.

using the following method:

 ret =  subprocess.call(["/mnt/build/pw_build/crosstool-ng/bin/ct-ng  menuconfig"], env=env_cross,shell=True)

crosstool-ng uses ncurses to present the user with a menu:

Enter image description here

Unfortunately when trying to navigate the menus I get:

Enter image description here

Using cat to display the sequences when using the arrow keys I see:

:/mnt/build$ cat > /dev/null
^[OA^[OD^[OB^[OC^[OA^[OB^[OD^[OC^[OA^[OB

It seems like something is possibly stripping the escape characters from the sequence.

When I call the ct-ng script from the command line in the terminal it works correctly, it is when I call it using subprocess.call() that things go bad.

My environment consists of:

Is what I am seeing a known problem? Is there something else I should be doing?

UPDATE: Thanks Eric... That got me 0.5f way there... I can navigate the menu now, but it still leaves droppings behind:

Enter image description here


Solution

  • To run curses programs under Python I'd recommend that you use pexpect.

    For example here's a simple program that starts a copy of vim, add some text, escape to command mode, issue a :w command, and then interact with the user (allowing him or her to continue editing or whatever). Then the control returns to Python:

    #!/usr/bin/env python
    import pexpect
    child = pexpect.spawn("/usr/bin/vim")
    child.send('a\n\nThis is another test.')
    child.send('\x1b')
    child.send(':w! test.txt\n')
    child.interact()
    

    You can also pass arguments (such as escape character, and filter functions for input and output) to the interact method. But those get a bit tricky. (On the other hand they then become your custom keyboard macro system interposed between users and the application being run under the .spawn()).

    (BTW: you can send your desired sequences of keystrokes into this ct-ng dialog/menu ... it's just a matter of figuring out what those sequences need to be for your terminal settings. For example on my iTerm under MacOS X running with TERM=xterm-256color a "down arrow" cursor movement comes out as ^[[B ([Esc][Bracket][B]). That would be '\x1b[B' as a Python string literal).