问题
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:
Unfortunately when trying to navigate the menus I get:
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:
- xfce4-terminal 0.6.3
- Linux 3.13.0-32-lowlatency #57-Ubuntu SMP PREEMPT Tue Jul 15 04:08:59 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
- Python 2.7.6
- NCURSES_VERSION "5.9"
- GNU Make 3.81
- crosstool-NG version 1.19.0
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:
回答1:
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).
回答2:
After careful tracing of the execution there was a script redirecting to tee
that was causing the problem.
Thanks to everyone who looked at the problem. I should have seen that in the first place; sorry for the noise.
来源:https://stackoverflow.com/questions/24946988/using-python-subprocess-call-to-launch-an-ncurses-process