469,267 Members | 1,094 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

detect suprocess interaction

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.
--
Robin Becker

Mar 23 '07 #1
2 1210
Robin Becker <ro***@reportlab.comwrote:
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://us**@127.0.0.1/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://us**@127.0.0.1/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)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Mar 26 '07 #2
Nick Craig-Wood wrote:
Robin Becker <ro***@reportlab.comwrote:
> I'm using subprocess to carry out svn commands (probably should use the svn api
..............
>>
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!
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://us**@127.0.0.1/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://us**@127.0.0.1/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 :)
--
Robin Becker
Mar 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by vivek | last post: by
2 posts views Thread by Alberto | last post: by
1 post views Thread by dwij2u | last post: by
reply views Thread by Omar Abid | last post: by
10 posts views Thread by Xu, Qian | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.