0

pexpect is not working with time outs.

import pexpect
import sys
import os
os.system("""git config --global user.name '#{git_name}'""")
os.system("""git config --global user.email '#{git_email}'""")
child = pexpect.spawn ('git clone https://github.com/davidmontgom/rtbopsConfig.git')
child.logfile = sys.stdout
child.expect ('Username:*')
child.sendline ('#{git_username}')
child.expect ('Pass*')
child.sendline ('#{git_password}')
child.expect(pexpect.EOF, timeout=420)
os.system('cd /home/ubuntu/workspace/rtbopsConfig; git checkout #{branch_name}')


Receiving objects:  16% (1751/10655), 19.36 MiB | 876 KiB/s     KiB/s   
Receiving objects:  16% (1751/10655), 20.64 MiB | 861 KiB/s
103.4.112.50 
103.4.112.50 STDERR: Traceback (most recent call last):
103.4.112.50 
103.4.112.50   File "/tmp/chef-script20121006-19825-5yog10", line 12, in <module>
103.4.112.50 
103.4.112.50     child.expect(pexpect.EOF)
103.4.112.50 
103.4.112.50   File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1316, in expect
103.4.112.50 
103.4.112.50     return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
103.4.112.50 
103.4.112.50   File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1330, in expect_list
103.4.112.50 
103.4.112.50     return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
103.4.112.50 
103.4.112.50   File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1414, in expect_loop
103.4.112.50 
103.4.112.50     raise TIMEOUT (str(e) + '\n' + str(self))
103.4.112.50 
103.4.112.50 pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
103.4.112.50 
103.4.112.50 <pexpect.spawn object at 0x7fbb24704750>
103.4.112.50 
103.4.112.50 version: 2.4 ($Revision: 516 $)
103.4.112.50 
103.4.112.50 command: /usr/bin/git
103.4.112.50 
103.4.112.50 args: ['/usr/bin/git', 'clone', 'https://github.com/davidmontgom/rtbopsConfig.git']
103.4.112.50 
103.4.112.50 searcher: searcher_re:
103.4.112.50 
103.4.112.50     0: EOF
103.4.112.50 
Receiving objects:  16% (1751/10655), 20.64 MiB | 861 KiB/s   873 KiB/s   
103.4.112.50 
Receiving objects:  16% (1751/10655), 20.64 MiB | 861 KiB/s   873 KiB/s   
103.4.112.50 
103.4.112.50 after: <class 'pexpect.TIMEOUT'>

How do I get pexpect to work?

Stals
  • 1,543
  • 4
  • 27
  • 52
Tampa
  • 75,446
  • 119
  • 278
  • 425

2 Answers2

4

pexpect raises an exception on timeout (in 30 seconds by default). If you don't want an exception; specify pexpect.TIMEOUT as a pattern for .expect().

In your example git clone produces pexpect.Timeout exception after default timeout on .expect(pexpect.EOF). Try:

i = child.expect([EOF, TIMEOUT], timeout=git_clone_timeout)
if i == 1: 
    # handle timeout
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I am also using a git clone operation with pexpect. However, I am using `pexpect.run(cmd, logfile=sys.stdout)` How do I make it not timeout? I tried adding a timeout=999 argument but that didn't work. – Frak Jun 02 '17 at 20:22
  • @frakman1: I would expect `timeout=git_clone_timeout` to work unless `git clone` produces too much output. You could also would try: `timeout=1, events={TIMEOUT: every_second}` It seems like a good separate question (create [an artificial minimal code example that demonstrates your issue](https://stackoverflow.com/help/mcve)). – jfs Jun 15 '17 at 23:01
  • I finally got it to work using by adding the `timeout=None` argument. `pexpect.run(cmd, logfile=sys.stdout, timeout=None)` – Frak Jun 16 '17 at 18:10
2

Another way to let TIMEOUT work:

try:
    child.expect([],timeout=300)
except pexpect.EOF:
    #do somthing
except pexpect.TIMEOUT:
    #do somthing
IvanaGyro
  • 598
  • 7
  • 25