2

I am trying to push all the data in a folder to Github. If I put the following commands simply in linux shell, everything is OK, but it dots now work on Python script with Pexpect. It seems to me that it is very-very simple and frequently used task, but I have not find any mudule for this task. Here is my code:

import subprocess
import pexpect
from pexpect import spawn
import os
os.chdir("/home/python/Desktop/")
subprocess.run("git add .", shell=True)
subprocess.run( '''git commit -m "update"''', shell=True)
child = pexpect.spawn('git push origin master')
child.expect('Username.+')
child.sendline('***n@mail.net')
child.expect('Password.+')
child.sendline('sdfsdfsdfsdfs')
result = str(child.before)
print(result)
Artem
  • 77
  • 1
  • 1
  • 7
  • you can add user in `.git/config` and it will not ask for user – furas Mar 02 '20 at 07:28
  • [How do I disable password prompts when doing git push/pull?](https://superuser.com/questions/338511/how-do-i-disable-password-prompts-when-doing-git-push-pull) – furas Mar 02 '20 at 07:31
  • it seems you can do it with module gitpython - [gitpython git authentication using user and password](https://stackoverflow.com/questions/44784828/gitpython-git-authentication-using-user-and-password) – furas Mar 02 '20 at 07:35

1 Answers1

1

Same problem, I needed to wait for EOF:

import pexpect

child = pexpect.spawn('git push origin master')
child.expect('Username .*')
child.sendline('<username>')
child.expect('Password .*')
child.sendline('<password>')
child.expect(pexpect.EOF)   
child.close()
upe
  • 1,862
  • 1
  • 19
  • 33