detect suprocess interaction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robin Becker

    #1

    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

  • Nick Craig-Wood

    #2
    Re: detect suprocess interaction

    Robin Becker <robin@reportla b.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://user@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://user@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.d evnull,"r"), preexec_fn=os.s etsid)

    --
    Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

    Comment

    • Robin Becker

      #3
      Re: detect suprocess interaction

      Nick Craig-Wood wrote:
      Robin Becker <robin@reportla b.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://user@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://user@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.d evnull,"r"), preexec_fn=os.s etsid)
      ......

      this just makes life more interesting :)
      --
      Robin Becker

      Comment

      Working...