sulimmesh
sulimmesh

Reputation: 763

Respond to git command line prompt from Python program

I'm currently using a Python program to make automated commits to a repository after it makes some edits to a file. I am using subprocess.Popen to make a child process that calls the git binary. I though I could just use stdin=PIPE when creating the child process and be able to write my user credentials when prompted by the command line, but for some reason this isn't happening. The code looks like the following:

proc = Popen(["git","push","origin","master"], stdin=PIPE)
proc.communicate("username")
proc.communicate("password")

What's happening right now is that it's calling the binary but still showing me the command line prompt. I've taken a look at some other questions on here, but they are all using essentially the same code I am, so I'm a little stumped as to the problem. I know there are plenty of other ways of achieving this result, like caching my credentials, but I'm curious to see if this way can work for a number of reasons.

Upvotes: 1

Views: 691

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114068

proc = Popen(["git","push","origin","master"], stdin=PIPE,stdout=PIPE,stderr=PIPE)
time.sleep(1)
proc.stdin.write("username")
time.sleep(1)
proc.stdin.write("password")
#or maybe
for letter in "password":
    proc.stdin.write(letter)
    time.sleep(0.1)
print proc.communicate()

something like that i guess ...

Upvotes: 1

Related Questions