1

I am adding Git support to an existing application that is written in Python (with PyQt). I decided to interact directly with Git commands through subprocess.Popen and parse the results of the different commands (because it doesn't seem appropriate to import a complete third-party library for the few things we want to implement).

So far I've successfully incorporated git branch and git remote, which more or less worked because I'm still within my local repo.
Now I'm tackling git fetch and run into difficulties.

First I got a "WARNING" from gnome-keyring on stderr. This is due to a problem with my installation, but of course we should not hang up because of such things. So I simply ignore this warning. Probably that's hacky, but for now I think we can live with adding such workarounds if problems should be reported.

But then I have the problem that the shell asks me (on stderr again) to enter my SSH passphrase. This is an issue with my installation on a particular computer too, but we should really accept if users prefer typing their passwords personally. So I have to handle the situation.


So after the lengthy introduction the question is rather concise:

If I run a shell command like git fetch from Python through subprocess.Popen and this shell program asks for user feedback, how can I interact with it from Python?
Basically I would like to display the stderr output to the user and allow him to enter his password. (Then of course implement an option to store this password in the application.)

uli_1973
  • 705
  • 1
  • 5
  • 22

2 Answers2

0

If I recall correctly, ssh is very particular about how it interacts with users, going so far as to directly open /dev/tty (not just try to work with its STDERR_FILENO). However, you can set SSH_ASKPASS and DISPLAY to make it use a particular program on an X11 display. The manual page notes that you may have to connect its stdin to /dev/null to make this work.

So, the two alternatives appear to be to work with that, or to allocate a pseudo-tty from within Python, and run the git command(s) that need ssh with that pty as their controlling terminal. The built-in (but Unix/Linux-only) os.openpty will get you partway there, but there is more to do.

The pexpect module has everything in it that you would need, though I have never used it myself.

torek
  • 448,244
  • 59
  • 642
  • 775
0

It depends on exactly what you are building but in my case, could detect this programatically then ask the user to run the following and please restart the program:

eval $(ssh-agent -s)
ssh-add

Once they do this, the error goes away. My call to git fetch looks like this:

p = Popen(['git', 'fetch'], cwd=repo_dir, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()

On the next lines, check if p.returncode is 128. That seems to be the number that comes back when this happens, as discussed here.

Would be nice to get python to prompt for the ssh password but sounds a bit painful to get it to work, especially without external libraries.

cardamom
  • 6,873
  • 11
  • 48
  • 102