detect suprocess interaction

R

Robin Becker

I'm using subprocess to carry out svn commands (probably should use the svn api
package, but that's a dependency too far). Anyhow my code looks like

from subprocess import Popen, PIPE
p = Popen((svn,'ls',u),stdout=PIPE,stderr=PIPE)
i = p.wait()

and this sort of thing works well under most circumstances. However, when this
code is executed for the very first time by a particular user it hangs waiting
on user input.

This code is being used purely for testing correctness of a particular svn url
so in the normal case we want to throw away both stdout and stderr. In the
exceptional case is it possible to detect that input is required and only in
that case issue the current contents of stdout (presumably a request for a
password)?

Clearly I need to supply some kind of input filelike object, but is this sort of
thing possible.
 
N

Nick Craig-Wood

Robin Becker said:
I'm using subprocess to carry out svn commands (probably should use the svn api
package, but that's a dependency too far). Anyhow my code looks like

from subprocess import Popen, PIPE
p = Popen((svn,'ls',u),stdout=PIPE,stderr=PIPE)
i = p.wait()

and this sort of thing works well under most circumstances. However, when this
code is executed for the very first time by a particular user it hangs waiting
on user input.

This code is being used purely for testing correctness of a particular svn url
so in the normal case we want to throw away both stdout and stderr. In the
exceptional case is it possible to detect that input is required and only in
that case issue the current contents of stdout (presumably a request for a
password)?

Clearly I need to supply some kind of input filelike object, but is this sort of
thing possible.

Yes it is possible, but if you try it you'll find you'll need to
implement the fileno() method of file objects which is asked to return
an OS file handle. This is obviously a problem!

In general subprocess isn't really designed for interactive
processes like the above. You'll find it much easier to use pexpect
for interactive stuff.

Note that svn has its own devious ways of finding a terminal to ask
the user for the password, eg...

$ svn ls svn+ssh://[email protected]/svn </dev/null >/dev/null 2>&1
Password:

I don't know exactly how it does that but I suspect it is to do with
the controlling terminal...

On my system

$ setsid svn ls svn+ssh://[email protected]/svn </dev/null >/dev/null 2>&1

Pops up a gui box asking for the password!

You can simulate the above with

Popen(..., stdin=file(os.devnull,"r"), preexec_fn=os.setsid)
 
R

Robin Becker

Nick said:
Yes it is possible, but if you try it you'll find you'll need to
implement the fileno() method of file objects which is asked to return
an OS file handle. This is obviously a problem!

yes I figured I might have to do something low level, but it's not
terribly obvious that I can detect the read at the other end ie normally
we seem to detect when output is available, not when a write is required
ie I might be able to write to the input of my command even if the
command won't read.
In general subprocess isn't really designed for interactive
processes like the above. You'll find it much easier to use pexpect
for interactive stuff.

Note that svn has its own devious ways of finding a terminal to ask
the user for the password, eg...

$ svn ls svn+ssh://[email protected]/svn </dev/null >/dev/null 2>&1
Password:

I don't know exactly how it does that but I suspect it is to do with
the controlling terminal...

On my system

$ setsid svn ls svn+ssh://[email protected]/svn </dev/null >/dev/null 2>&1

Pops up a gui box asking for the password!

You can simulate the above with

Popen(..., stdin=file(os.devnull,"r"), preexec_fn=os.setsid)
......

this just makes life more interesting :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top