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

前端 未结 2 832
梦如初夏
梦如初夏 2021-01-24 00:27

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

using the following method:



        
相关标签:
2条回答
  • 2021-01-24 01:23

    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).

    0 讨论(0)
  • 2021-01-24 01:30

    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.

    0 讨论(0)
提交回复
热议问题