Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, set terminal type in pexpect

Tags:

python

pexpect

I have a script which uses pexpect to start a CLI program. It works a bit like a shell where you get a prompt where you can enter some commands.

The problem I have, I think, is that this program uses a coloured prompt.

This is what I do

import pprint
import pexpect

1 a = pexpect.spawn('program')
2 a.expect("prompt>")
3 print "---------start------------"
4 print(a.before)
5 a.sendline("command")
6 a.expect("prompt>")
7 print "---------before------------"
8 pprint.pprint(a.before)
9 print "---------after------------"
10 pprint.pprint(a.after)

This is the output:

> python borken.py
---------start------------
A lot of text here from the enjoying programs start-up, lorem ipsum ...  
---------before------------
' \x1b[0m\x1b[8D\x1b[K\x1b[1m\x1b[34m'
---------after------------
'prompt>'

For some reason the first prompt colour coding borkens up things and a.before at line 8 is garbled, normal print does not work, even if I see that the command at line 5 actually produced a lot of output.

Does someone know what the problem could be, or is it possible to set the terminal type in pexpect to avoid the colours?

I am using tcsh shell

like image 617
Rickard Lindroth Avatar asked Dec 30 '22 07:12

Rickard Lindroth


2 Answers

Ok, I found the answer. csl's answer set me on the right path.

pexpect has a "env" option which I thought I could use. like this:

a = pexpect.spawn('program', env = {"TERM": "dumb"})

But this spawns a new shell which does not work for me, our development environment depends on a lot of environmental variables :/

But if I do this before spawning a shell:

import os
os.environ["TERM"] = "dumb"

I change the current "TERM" and "dumb" does not support colours, which fixed my issue.

like image 183
Rickard Lindroth Avatar answered Jan 01 '23 19:01

Rickard Lindroth


Couldn't find anything in the pexpect documentation for setting terminals, but you could probably start your program explicitly with a shell, and then set the terminal type there:

shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
child.expect(pexpect.EOF)

You could try something like

child = pexpect.spawn('TERM=vt100 /bin/bash', ['-c', shell_cmd])

You can also start bash with --norc and similar to avoid reading the initialization files. Check out the bash man page.

like image 40
csl Avatar answered Jan 01 '23 21:01

csl