1

Need little assistance with making this code work with pexpect module.

This code executes git pull by logging into a server and then downloads the latest code (if upgrade is available) or just sends out a messages saying "Already up-to-date."

The code actually idenfies the password screen but does not identify the text "already up-to-date"

not sure if I'm missing anything here.

A snippet from the code is:

        p = pexpect.spawn('git pull',cwd = comp_dir,maxread = 10000, timeout = 100)
        i = p.expect(['password:','Already up-to-date.',pexpect.EOF])
        if i == 0:
            output_lines = p.before
            output_line_list = output_lines.split('\r\n')
            for line in output_line_list: print line
            count = 0
            p.sendline(pwd)
            while count < 3:  **# The server in case of unsuccessful login asks for password thrice so this check...  (not sure if there is a better way of achieving this)**
                try:
                    output = p.expect('Permission denied')
                    count+=1
                    p.sendline(pwd)
                    p.logfile = sys.stdout
                except:
                    print 'Successful Login !!!! ------'
                    p.expect('Already up-to-date',timeout=None)
                    count = 3
        if i == 1:
            output_lines = p.before
            output_line_list = output_lines.split('\r\n')
            for line in output_line_list: print line
            p.expect(pexpect.EOF)

Any help is greatly appreciated.

Thanks, -Vijay

user596922
  • 1,501
  • 3
  • 18
  • 27

1 Answers1

1

This logic seems to be a bit incorrect.

At first: you are expecting from a set of prompts

i = p.expect(['password:','Already up-to-date.',pexpect.EOF])

Which works correctly as password prompt is first to be sent. Then even if you receive the prompt - 'Already up-to-date.'

you have altered pexpect to check for 'password denied'

output = p.expect('Permission denied')

I believe that any point only one of the expect lines are active. Also you can not set expectations after a prompt has occurred.

You have to alter the script to sequentially check on the expected lines

i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 0:
   .... send password
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 2:
   .... send password
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 1:
   .... print "already updated"
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 3:
   .... print output

Basically, you have at any point only one active expect command active. if you are expecting a line "xyz" and it receives "123". It will just time out waiting for "xyz".

pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • Hi Sir, One quick question on this logic.. When user enters gives password it gets into the condition i == 2: and after 3 attempts how does it come out? does it trigger out EOF after 3 attempts? I dont think it tirggers EOF as I dont see the print statement given on the condition i==3 – user596922 Jul 04 '12 at 12:41
  • @user596922: EOF should come after the password has been supplied. Can you try making it sequential – pyfunc Jul 04 '12 at 17:17