0

I'm launching sbt via Popen(), and my python process stdin reading is not working. Here is an example: On the first line I'm launching Popen, on the second line I'm trying to browse throught the history with an arrow key. This does not work for some time, printing ^[[A.

$ python
Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess; f = open("/dev/null", "rw"); subprocess.Popen(["sbt"], stdout=f, stderr=f, stdin=f)
<subprocess.Popen object at 0x10fc03950>
>>> ^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[Aimport subprocess; f = open("/dev/null", "rw"); subprocess.Popen(["sbt"], stdout=f, stderr=f, stdin=f)

This seems to only happen with sbt. Any idea why and how to bypass this behavior ?

Thanks

yazgoo
  • 1
  • 2

1 Answers1

0

My guess is that sbt is misbehaving when there is no pseudo-tty to interact with the user (probably because of jline).

Hence, let's use a python module to run the commands in a pseudo-tty. Install pexpect via pip (pip3 install pexpect for Python 3.x).

Then, run the following:

import pexpect, sys
f = open("sbt.log", "w+")
# Use `spawnu` for Python3, `spawn` otherwise
sbt = pexpect.spawnu("sbt -Dsbt.log.noformat=true \"version\" \"another-command\"", logfile=f)

# Do whatever is needed while sbt is running

# Force the process to expect EOF and file to be written
sbt.expect(pexpect.EOF)

Tested in Python 3.4.3 (Gentoo Linux) and works.

Jorge
  • 784
  • 4
  • 17