
ȔMc           @   sw  d  Z  y d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Td d l	 Z	 d d l
 Z
 d d l Z d d l Z Wn& e k
 r Z e e e  d  n Xd Z d Z d d d	 d
 d d d g Z d e f d     YZ d e f d     YZ d	 e f d     YZ g  d d  Z d
 f  d     YZ d   Z d   Z d d l Z d   Z d S(   s  
Pexpect is a Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect can be used for automating interactive applications such as
ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts
for duplicating software package installations on different servers.
It can be used for automated software testing. Pexpect is in the spirit of
Don Libes' Expect, but Pexpect is pure Python. Other Expect-like
modules for Python require TCL and Expect or require C extensions to
be compiled. Pexpect does not use C, Expect, or TCL extensions. It
should work on any platform that supports the standard Python pty
module. The Pexpect interface focuses on ease of use so that simple
tasks are easy.

Pexpect is Open Source, Free, and all Good that stuff.
License: Python Software Foundation License
         http://www.opensource.org/licenses/PythonSoftFoundation.html

Noah Spurrier

$Revision: 1.89 $
$Date: 2004/03/05 16:51:22 $
iN(   t   *s   
A critical module was not found. Probably this OS does not support it.
Currently pexpect is intended for UNIX operating systems.s   0.999s   $Revision: 1.89 $t   ExceptionPexpectt   EOFt   TIMEOUTt   spawnt   runt   __version__t   __revision__c           B   s    e  Z d  Z d   Z d   Z RS(   s4   Base class for all exceptions raised by this module.c         C   s   | |  _  d  S(   N(   t   value(   t   selfR   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __init__7   s    c         C   s   |  j  S(   N(   R   (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __str__9   s    (   t   __name__t
   __module__t   __doc__R
   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR   5   s   	c           B   s   e  Z d  Z RS(   s%   Raised when EOF is read from a child.(   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR   ;   s   c           B   s   e  Z d  Z RS(   s,   Raised when a read time exceeds the timeout.(   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR   =   s   i   c         C   s&   t  |  | |  } | j t  | j S(   s   This runs a command; waits for it to finish; then returns
        all output as a string. This is a utility interface around
        the spawn class.
    (   R   t   expectR   t   before(   t   commandt   argst   timeoutt   child(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR   D   s    c           B   sp  e  Z d  Z g  d d  Z d   Z d   Z d   Z d d  Z d   Z d	   Z	 d
   Z
 d   Z d   Z d d( d  Z d d  Z d d  Z d   Z d   Z d d  Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d d  Z d d  Z d d   Z d!   Z  d"   Z! e" d#  d$  Z# d%   Z$ d&   Z% d( d'  Z& RS()   sn   This is the main class interface for Pexpect. Use this class to
    start and control child applications.
    i   c         C   s  t  j j   |  _ t  j j   |  _ t  j j   |  _ t  j |  _ t  j |  _ t  j |  _ | |  _ d |  _	 d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ t |  t d  k rXy t j |  Wn t k
 rt d  n Xd |  _ | |  _	 d |  _ d |  _ d |  _ d |  _ d St |  t g   k r|t d  n  | g  k rt |  |  _ |  j d |  _ n% | |  _ |  j j  d |  | |  _ d	 t! d
   |  j  d |  _ |  j"   d S(   s  This is the constructor. The command parameter may be a string
        that includes a command and any arguments to the command. For example:
            p = pexpect.spawn ('/usr/bin/ftp')
            p = pexpect.spawn ('/usr/bin/ssh user@example.com')
            p = pexpect.spawn ('ls -latr /tmp')
        You may also construct it with a list of arguments like so:
            p = pexpect.spawn ('/usr/bin/ftp', [])
            p = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
            p = pexpect.spawn ('ls', ['-latr', '/tmp'])
        After this the child application will be created and
        will be ready to talk to. For normal use, see expect() and 
        send() and sendline().

        If the command parameter is an integer AND a valid file descriptor
        then spawn will talk to the file descriptor instead. This can be
        used to act expect features to any file descriptor. For example:
            fd = os.open ('somefile.txt', os.O_RDONLY)
            s = pexpect.spawn (fd)
        The original creator of the file descriptor is responsible
        for closing it. Spawn will not try to close it and spawn will
        raise an exception if you try to call spawn.close().
        ii    t    i   s;   Command is an int type, yet is not a valid file descriptor.s   <file descriptor>Ns*   The second argument, args, must be a list.t   <c         S   s   |  d | S(   Nt    (    (   t   xt   y(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   <lambda>   s    t   >(#   t   syst   stdint   filenot   STDIN_FILENOt   stdoutt   STDOUT_FILENOt   stderrt   STDERR_FILENOR   t   child_fdt   Nonet   _spawn__child_fd_ownert
   exitstatust   pidt   log_fileR   t   aftert   matcht	   softspacet   namet   flag_eoft   buffert   maxreadt   typet   ost   fstatt   OSErrorR   R   R   t	   TypeErrort   _split_command_linet   insertt   reducet   _spawn__spawn(   R	   R   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR
   R   sT    																						 c         C   s2   y |  j  r |  j   n  Wn t k
 r- n Xd S(   sT  This makes sure that no system resources are left open.
        Python only garbage collects Python objects. OS file descriptors
        are not Python objects, so they must be handled explicitly.
        If the child file descriptor was opened outside of this class
        (passed to the constructor) then this does not close it.
        N(   R&   t   closeR4   (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __del__   s
    	c         C   sj  |  j  d k s t d  |  j d k s0 t d  t |  j  d k r[ t d |  j   n  y t j   \ |  _  |  _ Wn) t	 k
 r } t d t
 |    n X|  j  d k r]y& t j j   |  _ |  j d d  Wn n Xt j t j  d } x< t d |  D]+ } y t j |  Wqt	 k
 r2qXqWd	 t j d
 <t j |  j |  j  n  d |  _ d S(   s   This starts the given command in a child process. This does
        all the fork/exec type of stuff for a pty. This is called by
        __init__. The args parameter is a list, command is a string.
        s   The pid member is not None.s   The command member is None.s4   The command was not found or was not executable: %s.s   Pexpect: pty.fork() failed: i    i   iP   i   t   Ct   LANGi   N(   R(   R%   t   AssertionErrorR   t   _whichR   t   ptyt   forkR$   R4   t   strR   R    R   t
   setwinsizet   resourcet	   getrlimitt   RLIMIT_NOFILEt   rangeR2   R:   t   environt   execvpR   R&   (   R	   t   et   max_fdt   i(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __spawn   s.    c         C   s   |  j  S(   s:   This returns the file descriptor of the pty for the child.(   R$   (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR      s    i   c         C   s   |  j  d k r |  j s' t d   n  |  j   t j |  j   | ru y t j |  j d  Wqu t k
 rq qu Xn  d |  _  d |  _ n  d S(   s   This closes the connection with the child application.
        It makes no attempt to actually kill the child or wait for its status.
        If the file descriptor was set by passing a file descriptor
        to the constructor then this method raises an exception.
        Note that calling close() more than once is valid.
        This emulates standard Python behavior with files.
        If wait is set to True then close will wait
        for the exit status of the process. Doing a wait is a blocking call,
        but this usually takes almost no time at all. Generally,
        you don't have to worry about this. If you are
        creating lots of children then you usually want to call wait.
        Only set wait to false if you know the child will
        continue to run after closing the controlling TTY.
        Otherwise you will end up with defunct (zombie) processes.
        is~   This file descriptor cannot be closed because it was not created by spawn. The original creator is responsible for closing it.i    N(
   R$   R&   R   t   flushR2   R:   t   waitpidR(   R4   R%   (   R	   t   wait(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR:      s    	
	c         C   s   d S(   sW   This does nothing. It is here to support the interface for a File-like object.
        N(    (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyRN     s    c         C   s   t  j |  j  S(   sl   This returns 1 if the file descriptor is open and
        connected to a tty(-like) device, else 0.
        (   R2   t   isattyR$   (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyRQ     s    c         C   sc   t  j |  j  } | r0 | d t  j B| d <n | d t  j @| d <t  j |  j t  j |  d S(   s+   This sets the terminal echo mode on or off.i   N(   t   termiost	   tcgetattrR$   t   ECHOt	   tcsetattrt	   TCSADRAIN(   R	   t   ont   new(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   setecho  s
    c         C   s   | |  _  d S(   s  This sets logging output to go to the given fileobject.
        Set fileobject to None to stop logging. 
        Example:
            child = pexpect.spawn('some_command')
            fout = file('mylog.txt','w')
            child.setlog (fout)
            ...
        N(   R)   (   R	   t
   fileobject(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   setlog  s    	c         C   s   | |  _  d S(   st  This sets the maximum number of bytes to read from a TTY at one time.
        This is used to change the read buffer size. When a pexpect.spawn
        object is created the default maxread is 1 (unbuffered).
        Set this value higher to turn on buffer. This should help performance
        in cases where large amounts of output are read back from the child.
        N(   R0   (   R	   R0   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt
   setmaxread!  s    c         C   sh  |  j  d k r t d   n  |  j   sr t j |  j  g g  g  d  \ } } } | sr d |  _ t d   qr n  t j |  j  g g  g  |  \ } } } | s t d   n  |  j  | k rXy t j |  j  |  } Wn( t	 k
 r } d |  _ t d   n X| d k r%d |  _ t d	   n  |  j
 d k rT|  j
 j |  |  j
 j   n  | St d
   d S(   s]  
        This reads at most size characters from the child application.
        It includes a timeout. If the read does not complete within the
        timeout period then a TIMEOUT exception is raised.
        If the end of file is read then an EOF exception will be raised.
        If a log file was set using setlog() then all data will
        also be written to the log file.

        Notice that if this method is called with timeout=None 
        then it actually may block.

        This is a non-blocking wrapper around os.read().
        It uses select.select() to implement a timeout. 
        is   I/O operation on closed filei    i   s0   End Of File (EOF) in read(). Braindead platform.s   Timeout exceeded in read().s6   End Of File (EOF) in read(). Exception style platform.R   s9   End Of File (EOF) in read(). Empty string style platform.s&   Reached an unexpected state in read().N(   R$   t
   ValueErrort   isalivet   selectR.   R   R   R2   t   readR4   R)   R%   t   writeRN   R   (   R	   t   sizeR   t   rt   wRJ   t   s(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   read_nonblocking*  s0    '	'		ic         C   sx   | d k r d S| d k  r0 |  j  t  |  j St j d | t j  } |  j  | t g  } | d k rq |  j S|  j S(   sW  This reads at most size bytes from the file 
        (less if the read hits EOF before obtaining size bytes). 
        If the size argument is negative or omitted, 
        read all data until EOF is reached. 
        The bytes are returned as a string object. 
        An empty string is returned when EOF is encountered immediately.
        i    R   s   .{%d}(   R   R   R   t   ret   compilet   DOTALLR*   (   R	   Rb   t   cret   index(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR`   b  s    c         C   sG   | d k r d S|  j  d t g  } | d k r< |  j d S|  j Sd S(   s`  This reads and returns one entire line. A trailing newline is kept in
        the string, but may be absent when a file ends with an incomplete line. 
        Note: This readline() looks for a \r\n pair even on UNIX because this is 
        what the pseudo tty device returns. So contrary to what you may be used to
        you will receive a newline as \r\n.
        An empty string is returned when EOF is hit immediately.
        Currently, the size agument is mostly ignored, so this behavior is not
        standard for a file-like object. If size is 0 then an empty string is
        returned.
        i    R   s   
N(   R   R   R   (   R	   Rb   Rk   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   readline{  s    c         C   s   |  S(   s?   This is to support interators over a file-like object.
        (    (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __iter__  s    c         C   s%   |  j    } | d k r! t  n  | S(   s>   This is to support iterators over a file-like object.
        R   (   Rl   t   StopIteration(   R	   t   result(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   next  s    	c         C   s3   g  } x& |  j    } | s Pn  | j |  q	 | S(   s   This reads until EOF using readline() and returns a list containing 
        the lines thus read. The optional sizehint argument is ignored.
        (   Rl   t   append(   R	   t   sizehintt   linest   line(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt	   readlines  s    c         C   s   |  j  |  d S(   sH   This is similar to send() except that there is no return value.
        N(   t   send(   R	   RB   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyRa     s    c         C   s"   x | D] } |  j  |  q Wd S(   s   This calls write() for each element in the sequence.
        The sequence can be any iterable object producing strings, 
        typically a list of strings. This does not add line separators
        There is no return value.
        N(   Ra   (   R	   t   sequenceRB   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt
   writelines  s    c         C   s   t  j |  j |  S(   sd   This sends a string to the child process.
        This returns the number of bytes written.
        (   R2   Ra   R$   (   R	   RB   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyRv     s    R   c         C   s)   |  j  |  } | |  j  t j  } | S(   su   This is like send(), but it adds a line feed (os.linesep).
        This returns the number of bytes written.
        (   Rv   R2   t   linesep(   R	   RB   t   n(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   sendline  s    c         C   s   t  j j   } t j |  } t j |  } | d t j B| d <z4 t j | t j |  t j	 |  j
 d t j  Wd t j | t j |  Xd S(   sB  This sends an EOF to the child.
        This sends a character which causes the pending parent output
        buffer to be sent to the waiting child program without
        waiting for end-of-line. If it is the first character of the
        line, the read() in the user program returns 0, which
        signifies end-of-file. This means to work as expected 
        a sendeof() has to be called at the begining of a line. 
        This method does not send a newline. It is the responsibility
        of the caller to ensure the eof is sent at the beginning of a line.
        i   s   %cN(   R   R   R   RR   RS   t   ICANONRU   RV   R2   Ra   R$   t   CEOF(   R	   t   fdt   oldRX   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   sendeof  s    c         C   s   |  j  S(   sF   This returns 1 if the EOF exception was raised at some point.
        (   R.   (   R	   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   eof  s    c         C   s7  |  j  d k r? |  j r? y t j |  j  d SWq? d SXn  y" t j |  j  t j  \ } } Wn t k
 ru d SX| d k r | d k r y" t j |  j  t j  \ } } Wn t k
 r d SX| d k r | d k r d Sn  t j |  r	t j	 |  |  _
 d St j |  rd St j |  r/d Sd Sd S(   sn  This tests if the child process is running or not.
        This returns 1 if the child process appears to be running or 0 if not.
        This also sets the exitstatus attribute.
        It can take literally SECONDS for Solaris to return the right status.
        This is the most wiggly part of Pexpect, but I think I've almost got
        it nailed down.
        ii   i    N(   R(   R&   R2   R3   R$   RO   t   WNOHANGR4   t	   WIFEXITEDt   WEXITSTATUSR'   t
   WIFSTOPPEDt   WIFSIGNALED(   R	   R(   t   status(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR^     s2    ""c         C   s&   |  j    r" t j |  j |  n  d S(   s   This sends the given signal to the child application.
        In keeping with UNIX tradition it has a misleading name.
        It does not necessarily kill the child unless
        you send the right signal.
        N(   R^   R2   t   killR(   (   R	   t   sig(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR     s    c         C   s   t  |  t k	 r | g } n  g  } x | D] } t  |  t k rb | j t j | t j   q+ | t k r~ | j t  q+ | t k r | j t  q+ t  |  t  t j d   k r | j |  q+ t	 d t
 t  |    q+ W| S(   s  This compiles a pattern-string or a list of pattern-strings.
        Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or 
        a list of those.

        This is used by expect() when calling expect_list().
        Thus expect() is nothing more than::
             cpl = self.compile_pattern_list(pl)
             return self.expect_list(clp, timeout)

        If you are using expect() within a loop it may be more
        efficient to compile the patterns first and then call expect_list().
        This avoid calls in a loop to compile_pattern_list():
             cpl = self.compile_pattern_list(my_pattern)
             while some_condition:
                ...
                i = self.expect_list(clp, timeout)
                ...
        R   sZ   Argument must be one of StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s(   R1   t   ListTypet
   StringTypeRq   Rg   Rh   Ri   R   R   R5   RB   (   R	   t   patternst   compiled_pattern_listt   p(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   compile_pattern_list  s    !c         C   s   |  j  |  } |  j | |  S(   sK
  This seeks through the stream until a pattern is matched.
        The pattern is overloaded and may take several types including a list.
        The pattern can be a StringType, EOF, a compiled re, or
        a list of those types. Strings will be compiled to re types.
        This returns the index into the pattern list. If the pattern was
        not a list this returns index 0 on a successful match.
        This may raise exceptions for EOF or TIMEOUT.
        To avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to
        the pattern list.

        After a match is found the instance attributes
        'before', 'after' and 'match' will be set.
        You can see all the data read before the match in 'before'.
        You can see the data that was matched in 'after'.
        The re.MatchObject used in the re match will be in 'match'.
        If an error occured then 'before' will be set to all the
        data read so far and 'after' and 'match' will be None.

        If timeout is -1 then timeout will be set to the self.timeout value.

        Note: A list entry may be EOF or TIMEOUT instead of a string.
        This will catch these exceptions and return the index
        of the list entry instead of raising the exception.
        The attribute 'after' will be set to the exception type.
        The attribute 'match' will be None.
        This allows you to write code like this:
                index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
                if index == 0:
                    do_something()
                elif index == 1:
                    do_something_else()
                elif index == 2:
                    do_some_other_thing()
                elif index == 3:
                    do_something_completely_different()
        instead of code like this:
                try:
                    index = p.expect (['good', 'bad'])
                    if index == 0:
                        do_something()
                    elif index == 1:
                        do_something_else()
                except EOF:
                    do_some_other_thing()
                except TIMEOUT:
                    do_something_completely_different()
        These two forms are equivalent. It all depends on what you want.
        You can also just expect the EOF if you are waiting for all output
        of a child to finish. For example:
                p = pexpect.spawn('/bin/ls')
                p.expect (pexpect.EOF)
                print p.before

        If you are trying to optimize for speed then see
        expect_list() and expect_exact().
        (   R   t   expect_list(   R	   t   patternR   R   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR   A  s    9c         C   s  | d k r |  j  } n  t |  t k r6 | g } n  y |  j } x d } x | D] } | d } | t k sR | t k r qR n  | j |  } | d k rR | |  |  _ | | |  _ | | t	 |  |  _ d |  _ | SqR W|  j |  j |  } | | } qE Wn t k
 rA| |  _ t |  _ t | k r;| j t  S  nv t k
 r| |  _ t |  _ t | k ry| j t  S  n8 t k
 r| |  _ d |  _ d |  _ d |  _   n Xd S(   sU  This is similar to expect() except that it takes
        list of plain strings instead of regular expressions.
        This should be much faster than expect(). It could also be
        useful when you don't want to have to worry about escaping
        regular expression characters that you want to match.
        You may also pass just a string without a list and the string
        will be automatically converted to a list with a single string element.
        If timeout is -1 then timeout will be set to the self.timeout value.
        See also expect_list() for speed optimization.
        ii   i    R   N(   R   R1   R   R/   R   R   t   findR   R*   t   lenR%   R+   Rf   R0   Rk   t	   Exception(   R	   t   pattern_listR   t   incomingRk   t
   str_targett   match_indext   c(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   expect_exact}  sN    	
									c         C   s  | d k r |  j  } n  y |  j } x d } x | D] } | d } | t k s4 | t k rb q4 n  | j |  } | d k	 r4 | | j    |  _ | | j   |  _ | |  _	 | | j
   |  _ | Sq4 W|  j |  j |  } | | } q' Wn t k
 r+| |  _ t |  _ t | k r%| j t  S  nv t k
 ri| |  _ t |  _ t | k rc| j t  S  n8 t k
 r| |  _ d |  _ d |  _	 d |  _   n Xd S(   su  
        This takes a list of compiled regular expressions and returns 
        the index into the pattern_list that matched the child's output.
        This is called by expect(). It is similar to the expect() method
        except that expect_list() is not overloaded and it does not have to
        compile the pattern list on every call. This will help if you are
        trying to optimize for speed. You must not pass
        anything except a list of compiled regular expressions.
        If timeout is -1 then timeout will be set to the self.timeout value.
        See also expect_exact() for speed optimization.
        ii   R   N(   R   R/   R   R   t   searchR%   t   startR   R*   R+   t   endRf   R0   Rk   R   (   R	   R   R   R   Rk   Rj   R+   R   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR     sJ    	
									c         C   sP   t  j d d d d d  } t j |  j   t j |  } t  j d |  d d !S(   su   
        This returns the window size of the child tty.
        The return value is a tuple of (rows, cols).
        t   HHHHi    i   (   t   structt   packt   fcntlt   ioctlR   RR   t
   TIOCGWINSZt   unpack(   R	   Re   R   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt
   getwinsize  s    c         C   sV   t  j } | d k r d } n  t j d | | d d  } t j |  j   | |  d S(   sD  
        This sets the windowsize of the child tty.
        This will cause a SIGWINCH signal to be sent to the child.
        This does not change the physical window size.
        It changes the size reported to TTY-aware applications like
        vi or curses -- applications that respond to the SIGWINCH signal.
        l   gt  igtR   i    N(   RR   t
   TIOCSWINSZR   R   R   R   R   (   R	   Rc   R   R   Re   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyRC     s
    		i   c         C   s}   |  j  j |  j  d |  _ |  j  j   t j |  j  } t j |  j  z |  j |  Wd t j	 |  j t j
 |  Xd S(   s  This gives control of the child process to the interactive user
        (the human at the keyboard).
        Keystrokes are sent to the child process, and the stdout and stderr
        output of the child process is printed.
        When the user types the escape_character this method will stop.
        The default for escape_character is ^] (ASCII 29).
        This simply echos the child stdout and child stderr to the real
        stdout and it echos the real stdin to the child stdin.
        R   N(   R    Ra   R/   RN   t   ttyRS   R   t   setrawt   _spawn__interact_copyRU   t	   TCSAFLUSH(   R	   t   escape_charactert   mode(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   interact   s    	c         C   s?   x8 | d k r: |  j    r: t j | |  } | | } q Wd S(   s/   This is used by the interact() method.
        R   N(   R^   R2   Ra   (   R	   R~   t   dataRz   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __interact_writen5  s    c         C   s   t  j | d  S(   s/   This is used by the interact() method.
        i  (   R2   R`   (   R	   R~   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __interact_read;  s    c         C   s   x |  j    r t j |  j |  j g g  g   \ } } } |  j | k rp |  j |  j  } t j |  j |  n  |  j | k r |  j |  j  } |  j |  j |  | | k r Pq q q Wd S(   s/   This is used by the interact() method.
        N(	   R^   R_   R$   R   t   _spawn__interact_readR2   Ra   R!   t   _spawn__interact_writen(   R	   R   Rc   Rd   RJ   R   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   __interact_copy?  s    *N('   R   R   R   R
   R;   R9   R   R:   RN   RQ   RY   R[   R\   R%   Rf   R`   Rl   Rm   Rp   Ru   Ra   Rx   Rv   R{   R   R   R^   R   R   R   R   R   R   RC   t   chrR   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR   M   sF   Q		1								8									1	
	%<B?	
			c         C   s   t  j j |   d k r4 t  j |  t  j  r4 |  Sn  t  j j d  sZ t  j d d k rf t  j } n t  j d } t j	 | t  j
  } x< | D]4 } t  j j | |   } t  j | t  j  r | Sq Wd S(   sp   This takes a given filename; tries to find it in the
    environment path; then checks if it is executable.
    R   t   PATHN(   R2   t   patht   dirnamet   accesst   X_OKRH   t   has_keyt   defpatht   stringt   splitt   pathsept   joinR%   (   t   filenameR   t   pathlistR   t   f(    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR?   Q  s    &c         C   s&  g  } d } d } d } d } x |  D] } | d k r@ d } q% | d k rs | r[ d } q| sj d } qd } q% | d k r | r d } q| s d } qd } q% | d k r | r | r | r | j  |  d } q% | | } | d k r% | r% d } q% q% W| d k r"| j  |  n  | S(   s  This splits a command line into a list of arguments.
    It splits arguments on spaces, but handles
    embedded quotes, doublequotes, and escaped characters.
    It's impossible to do this with a regular expression, so
    I wrote a little state machine to parse the command line.
    R   i    s   \i   t   't   "R   (   Rq   (   t   command_linet   arg_listt   argt   state_quotet   state_doublequotet	   state_escR   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR6   k  s:    							!	
c         C   s   t  j |   S(   N(   t   shlexR   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyR6     s    (   R   R2   R   R_   R   Rg   R   RD   t   typesR@   R   RR   R   t   ImportErrorRJ   RB   R   R   t   __all__R   R   R   R   R   R   R?   R6   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/smart/util/pexpect.pyt   <module>   s>   
	   		,