JVc @s?dZyddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZWn7ek rejdZeeednXdZdZddd d d d d ddddg ZejddkZdefdYZdefdYZd efdYZdeeeeeedZ deeeeeedZ!dZ"d e#fdYZ$d e$fdYZ%de#fdYZ&de#fdYZ'd Z(d!Z)dS("s Pexpect is a Python module for spawning child applications and controlling them automatically. 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. There are two main interfaces to the Pexpect system; these are the function, run() and the class, spawn. The spawn class is more powerful. The run() function is simpler than spawn, and is good for quickly calling program. When you call the run() function it executes a given program and then returns the output. This is a handy replacement for os.system(). For example:: pexpect.run('ls -la') The spawn class is the more powerful interface to the Pexpect system. You can use this to spawn a child program then interact with it by sending input and expecting responses (waiting for patterns in the child's output). For example:: child = pexpect.spawn('scp foo user@example.com:.') child.expect('Password:') child.sendline(mypassword) This works even for commands that ask for passwords or other input outside of the normal stdio streams. For example, ssh reads input directly from the TTY device which bypasses stdin. Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett, Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin, Jacques-Etienne Baudoux, Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, John Spiegel, Jan Grant, and Shane Kerr. Let me know if I forgot anyone. Pexpect is free, open source, and all that good stuff. http://pexpect.sourceforge.net/ PEXPECT LICENSE This license is approved by the OSI and FSF as GPL-compatible. http://opensource.org/licenses/isc-license.txt Copyright (c) 2012, Noah Spurrier PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. iNis A critical module was not found. Probably this operating system does not support it. Pexpect is intended for UNIX-like operating systems.s3.1ttExceptionPexpecttEOFtTIMEOUTtspawntspawnutruntrunutwhichtsplit_command_linet __version__t __revision__iicBs)eZdZdZdZdZRS(s9Base class for all exceptions raised by this module. cCs#tt|j|||_dS(N(tsuperRt__init__tvalue(tselfR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR fscCs t|jS(N(tstrR(R((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__str__jscCs^tjtjd}g|D]}d|dkr |^q }tj|}dj|S(sThis returns an abbreviated stack trace with lines that only concern the caller. In other words, the stack trace inside the Pexpect module is not included. ispexpect/__init__iR(t tracebackt extract_tbtsystexc_infot format_listtjoin(Rttblisttitem((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt get_tracems)(t__name__t __module__t__doc__R RR(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRbs  cBseZdZRS(sRRaised when EOF is read from a child. This usually means the child has exited.(RRR(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRxscBseZdZRS(s-Raised when a read time exceeds the timeout. (RRR(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR}scCs:t|d|d|d|d|d|d|d|dtS( s This function runs the given command; waits for it to finish; then returns all output as a string. STDERR is included in output. If the full path to the command is not given then the path is searched. Note that lines are terminated by CR/LF (\r\n) combination even on UNIX-like systems because this is the standard for pseudottys. If you set 'withexitstatus' to true, then run will return a tuple of (command_output, exitstatus). If 'withexitstatus' is false then this returns just command_output. The run() function can often be used instead of creating a spawn instance. For example, the following code uses spawn:: from pexpect import * child = spawn('scp foo user@example.com:.') child.expect('(?i)password') child.sendline(mypassword) The previous code can be replace with the following:: from pexpect import * run('scp foo user@example.com:.', events={'(?i)password': mypassword}) **Examples** Start the apache daemon on the local machine:: from pexpect import * run("/usr/local/apache/bin/apachectl start") Check in a file using SVN:: from pexpect import * run("svn ci -m 'automatic commit' my_file.py") Run a command and capture exit status:: from pexpect import * (command_output, exitstatus) = run('ls -l /bin', withexitstatus=1) The following will run SSH and execute 'ls -l' on the remote machine. The password 'secret' will be sent if the '(?i)password' pattern is ever seen:: run("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\n'}) This will start mencoder to rip a video from DVD. This will also display progress ticks every 5 seconds as it runs. For example:: from pexpect import * def print_ticks(d): print d['event_count'], run("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5) The 'events' argument should be a dictionary of patterns and responses. Whenever one of the patterns is seen in the command out run() will send the associated response string. Note that you should put newlines in your string if Enter is necessary. The responses may also contain callback functions. Any callback is function that takes a dictionary as an argument. The dictionary contains all the locals from the run() function, so you can access the child spawn object or any other variable defined in run() (event_count, child, and extra_args are the most useful). A callback may return True to stop the current run process otherwise run() continues until the next event. A callback may also return a string which will be sent to the child. 'extra_args' is not used by directly run(). It provides a way to pass data to a callback function through run() through the locals dictionary passed to a callback. ttimeouttwithexitstatusteventst extra_argstlogfiletcwdtenvt_spawn(t_runR(tcommandRRR R!R"R#R$((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRsIc Ks=t|d|d|d|d|d|d|d|dt|S( sThis offers the same interface as :func:`run`, but using unicode. Like :class:`spawnu`, you can pass ``encoding`` and ``errors`` parameters, which will be used for both input and output. RRR R!R"R#R$R%(R&R( R'RRR R!R"R#R$tkwargs((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRsc  Ks2|dkr6||ddd|d|d|| } n-||d|ddd|d|d|| } |dk rt|j} t|j} n d} d} g} d}xGtry| j| }t| j| jr| j | j | jn| j | j t| || jr2| j | |nqt| |t j r| |t}tjjt|| jr| j |q|rPqn td |d }Wqtk r| j | j Pqtk r| j | j PqXqW| jj| }|r*| j|| jfS|SdS( NitmaxreadiR"R#R$Ris*The callback must be a string or function.i(tNonetlisttkeystvaluestTruetexpectt isinstancetaftertallowed_string_typestappendtbeforetsendttypest FunctionTypetlocalsRtstdouttflusht TypeErrorRRt string_typeRtcloset exitstatus(R'RRR R!R"R#R$R%R(tchildtpatternst responsestchild_result_listt event_counttindextcallback_resultt child_result((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR&sP           c BseZdZeZerWeefZedZ e j j dZ edZ n*efZeeZ e j Z ejjZ d<Zgddd<d<d<d<edZedZedZed Zd Zd Zgd Zd ZdZdZedZ dZ!dZ"ddZ#dZ$dZ%dZ&dddZ'ddZ(ddZ)dZ*ddZ+dZdZ,d Z-d!Z.d"d#Z/d$Z0d%Z1d&Z2d'Z3e4d(Z5d)Z6d*Z7d+Z8d,Z9d-Z:ddd.Z;ddd/Z<ddd0Z=ddd1Z>d2Z?d3Z@ed4d<d<d5ZAd6ZBd7ZCd<d<d<d8ZDd<d9ZEd:ZFd;ZGRS(=sjThis is the main class interface for Pexpect. Use this class to start and control child applications. cCs t|gS(N(tbytes(tc((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt_chrstasciicCsHytjjj|SWn*tk rCtjj|jddSXdS(NRJtreplace(RR9tbuffertwritetAttributeErrortdecode(tb((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytwrite_to_stdout s iic Cstj|_tj|_tj|_tj|_tj|_tj|_d |_ t |_ d |_ d |_ d |_d |_t|_d |_d |_d |_t |_d |_d|_||_t|_||_d |_d |_||_|j|_ ||_!d|_"d|_#d|_$t |_%dt&|d|_'t|_(||_)||_*| |_+tj,j-j.ddk|_/tj,j-j.ddkstj,j-j.d dkrt |_0n t|_0|d krd |_1d |_2d |_'n|j3||d S( sThis is the constructor. The command parameter may be a string that includes a command and any arguments to the command. For example:: child = pexpect.spawn('/usr/bin/ftp') child = pexpect.spawn('/usr/bin/ssh user@example.com') child = pexpect.spawn('ls -latr /tmp') You may also construct it with a list of arguments like so:: child = pexpect.spawn('/usr/bin/ftp', []) child = pexpect.spawn('/usr/bin/ssh', ['user@example.com']) child = 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(). Remember that Pexpect does NOT interpret shell meta characters such as redirect, pipe, or wild cards (``>``, ``|``, or ``*``). This is a common mistake. If you want to run a command and pipe it through another command then you must also start a shell. For example:: child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"') child.expect(pexpect.EOF) The second form of spawn (where you pass a list of arguments) is useful in situations where you wish to spawn a command and pass it its own argument list. This can make syntax more clear. For example, the following is equivalent to the previous example:: shell_cmd = 'ls -l | grep LOG > logs.txt' child = pexpect.spawn('/bin/bash', ['-c', shell_cmd]) child.expect(pexpect.EOF) The maxread attribute sets the read buffer size. This is maximum number of bytes that Pexpect will try to read from a TTY at one time. Setting the maxread size to 1 will turn off buffering. Setting the maxread value higher may help performance in cases where large amounts of output are read back from the child. This feature is useful in conjunction with searchwindowsize. The searchwindowsize attribute sets the how far back in the incoming seach buffer Pexpect will search for pattern matches. Every time Pexpect reads some data from the child it will append the data to the incoming buffer. The default is to search from the beginning of the incoming buffer each time new data is read from the child. But this is very inefficient if you are running a command that generates a large amount of data where you want to match. The searchwindowsize does not affect the size of the incoming data buffer. You will still have access to the full buffer after expect() returns. The logfile member turns on or off logging. All input and output will be copied to the given file object. Set logfile to None to stop logging. This is the default. Set logfile to sys.stdout to echo everything to standard output. The logfile is flushed after each write. Example log input and output to a file:: child = pexpect.spawn('some_command') fout = file('mylog.txt','w') child.logfile = fout Example log to stdout:: child = pexpect.spawn('some_command') child.logfile = sys.stdout The logfile_read and logfile_send members can be used to separately log the input from the child and output sent to the child. Sometimes you don't want to see everything you write to the child. You only want to log what the child sends back. For example:: child = pexpect.spawn('some_command') child.logfile_read = sys.stdout To separately log output sent to the child use logfile_send:: self.logfile_send = fout If ``ignore_sighup`` is True, the child process will ignore SIGHUP signals. For now, the default is True, to preserve the behaviour of earlier versions of Pexpect, but you should pass this explicitly if you want to rely on it. The delaybeforesend helps overcome a weird behavior that many users were experiencing. The typical problem was that a user would expect() a "Password:" prompt and then immediately call sendline() to send the password. The user would then see that their password was echoed back to them. Passwords don't normally echo. The problem is caused by the fact that most applications print out the "Password" prompt and then turn off stdin echo, but if you send your password before the application turned off echo, then you get your password echoed. Normally this wouldn't be a problem when interacting with a human at a real keyboard. If you introduce a slight delay just before writing then this seems to clear up the problem. This was such a common problem for many users that I decided that the default pexpect behavior should be to sleep just before writing to the child application. 1/20th of a second (50 ms) seems to be enough to clear up the problem. You can set delaybeforesend to 0 to return to the old behavior. Most Linux machines don't like this to be below 0.03. I don't know why. Note that spawn is clever about finding commands on your path. It uses the same logic that "which" uses to find executables. If you wish to get the exit status of the child you must call the close() method. The exit or signal status of the child will be stored in self.exitstatus or self.signalstatus. If the child exited normally then exitstatus will store the exit return code and signalstatus will be None. If the child was terminated abnormally with a signal then signalstatus will store the signal value and exitstatus will be None. If you need more detail you can also read the self.status member which stores the status returned by os.waitpid. You can interpret this using os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG. ig?g?ttirixitsolaristsunos5sN(4tptyt STDIN_FILENOt STDOUT_FILENOt STDERR_FILENORtstdinR9tstderrR*tsearchertFalset ignorecaseR4R1tmatcht match_indexR.t terminatedR>t signalstatuststatustflag_eoftpidtchild_fdRRt delimiterR"t logfile_readt logfile_sendR)R<RLtsearchwindowsizetdelaybeforesendtdelayafterclosetdelayafterterminatet softspacetreprtnametclosedR#R$t ignore_sighuptplatformtlowertfindt_spawn__irix_hacktuse_native_pty_forkR'targsR%( RR'RyRR)RkR"R#R$Rs((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR /s\u                                  !      cCs t|ts|jdS|S(NRJ(R0RGtencode(ts((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt_coerce_expect_strings cCs t|ts|jdS|S(Nsutf-8(R0RGRz(R{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt_coerce_send_strings cCs|S(N((R{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt_coerce_read_stringscCs(|js$y|jWq$q$XndS(sLThis 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(RrR=(R((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__del__s  cCsg}|jt||jdt|jdt|j|jd|jf|jd|jf|jd|jd|jd|jd|jd|j f|jd |j f|jd t|j |jd t|j |jd t|j |jd t|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|j|jdt|jdj|S(sVThis returns a human-readable string that represents the state of the object. s version: s command: sargs: %rs searcher: %rsbuffer (last 100 chars): %risbefore (last 100 chars): %rs after: %rs match: %rs match_index: s exitstatus: s flag_eof: spid: s child_fd: sclosed: s timeout: s delimiter: s logfile: slogfile_read: slogfile_send: s maxread: s ignorecase: ssearchwindowsize: sdelaybeforesend: sdelayafterclose: sdelayafterterminate: s (R3RpR RR'RyR]RLR4R1R`RaR>ReRfRgRrRRhR"RiRjR)R_RkRlRmRnR(RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs8cCst|tdr0tddddnt|tgsTtdn|gkrt||_|jd|_n&||_|jjd|||_t|j}|dkrtdd|jn||_|j|jdThis is to support iterators over a file-like object. (titerRR<(R((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__iter__scCs:g}x-tr5|j}|s%Pn|j|q W|S(sThis reads until EOF using readline() and returns a list containing the lines thus read. The optional 'sizehint' argument is ignored. Remember, because this reads until EOF that means the child process should have closed its stdout. If you run this method on a child that is still running with its stdout open then this method will block until it timesout.(R.RR3(Rtsizehinttlinestline((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt readliness  cCs|j|dS(sHThis is similar to send() except that there is no return value. N(R5(RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRMscCs"x|D]}|j|qWdS(sThis 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(RM(RtsequenceR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt writeliness cCs<tj|j|j|}|j|d|j|S(sSends string ``s`` to the child process, returning the number of bytes written. If a logfile is specified, a copy is written to that log. R5(RRRlR}Rt_send(RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR5scCstj|j|S(N(RRMRg(RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRsRcCs)|j|}||j|j}|S(sWraps send(), sending string ``s`` to child process, with os.linesep automatically appended. Returns number of bytes written. (R5tlinesep(RR{tn((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytsendlinescCs|j}t|}|dkrZ|dkrZ|tdd}|j|j|Si dd6dd6dd 6dd 6d d 6d d 6dd6dd6dd6dd6dd6dd6}||krdS|j|j||S(sHelper method that wraps send() with mnemonic access for sending control character to the child (such as Ctrl-C or Ctrl-D). For example, to send Ctrl-G (ASCII 7, bell, ''):: child.sendcontrol('g') See also, sendintr() and sendeof(). iaiztaiit@t`it[t{is\t|it]t}it^t~it_it?(RutordR5RI(RtcharRtd((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt sendcontrols    cCsUttdr5ttj|jdtj}nd}|j|j|dS(s1This 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 beginning 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. tVEOFiiN(thasattrRRRRgRR5RI(RR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytsendeof+s&cCsUttdr5ttj|jdtj}nd}|j|j|dS(snThis sends a SIGINT to the child. It does not require the SIGINT to be the first character on a line. tVINTRiiN(RRRRRgRR5RI(RR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytsendintrMs&cCs|jS(s@This returns True if the EOF exception was ever raised. (Re(R((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyteofYscCs!|jstSy|jtjtj|j|jsCtS|jtjtj|j|jsstS|jtj tj|j|jstS|r|jtj tj|j|jstSt Snt SWn5t k rtj|j|jstSt SnXdS(sThis forces a child process to terminate. It starts nicely with SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This returns True if the child was terminated. This returns False if the child could not be terminated. N( RR.tkillRRRRRntSIGCONTtSIGINTtSIGKILLR^R(RR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR`s6       cCs|jr*tj|jd\}}n tdtj||_tj|r||_tj||_d|_ t |_ netj |r||_d|_tj||_ t |_ n&tj|rtdddn|jS(smThis waits until the child exits. This is a blocking call. This will not read any data from the child, so this will block forever if the child has unread output and has terminated. In other words, the child may have printed output then called exit(), but, the child is technically still alive until its output is read by the parent. is#Cannot wait for dead child process.s!Called wait() on a stopped child s.process. This is not supported. Is some other s2process attempting job control with our child pid?N(RRtwaitpidRfRt WEXITSTATUSR>t WIFEXITEDRdR*RcR.Rbt WIFSIGNALEDtWTERMSIGt WIFSTOPPED(RRfRd((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytwaits&         cCs|jr tS|jrd}n tj}ytj|j|\}}WnTtk rtj d}|j t j krt ddddq|nX|dkr"ytj|j|\}}WnCtk r}|j t j krt ddddqnX|dkr"t Sn|dkr2t Stj|rq||_tj||_d |_t |_nitj|r||_d |_tj||_t |_n*tj|rt ddd d ntS( sZThis tests if the child process is running or not. This is non-blocking. If the child was terminated then this will read the exitstatus or signalstatus of the child. This returns True if the child process appears to be running or False if not. It can take literally SECONDS for Solaris to return the right status. iis isalive() encountered condition s0where "terminated" is 0, but there was no child s)process. Did someone else call waitpid() son our process?s-that should never happen. There was no child s,where child process is stopped. This is not s,supported. Is some other process attempting sjob control with our child pid?N(RbR^ReRtWNOHANGRRfRRRterrnotECHILDRR.RRdRR>R*RcRRR(Rtwaitpid_optionsRfRdRR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRsT                     cCs&|jr"tj|j|ndS(sThis 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(RRRRf(Rtsig((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs c CsStdjdt|d|ddjg|jD]}t|^q.dS(Ns`got {badtype} ({badobj!r}) as pattern, must be one of: {goodtypes}, pexpect.EOF, pexpect.TIMEOUTtbadtypetbadobjt goodtypess, (R;tformatRRR2R(Rtpatterntast((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt_pattern_type_errs    cCs$|dkrgSt|ts+|g}ntj}|jrM|tjB}ng}xt|D]\}}t||jr|j |}|j tj ||q`|t kr|j t q`|t kr|j t q`t|ttj dr|j |q`|j|q`W|S(sThis compiles a pattern-string or a list of pattern-strings. Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those. Patterns may also be None which results in an empty list (you might do this if waiting for an EOF or TIMEOUT condition without expecting any pattern). 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(cpl, 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) ... RN(R*R0R+RRR_t IGNORECASEt enumerateR2R|R3RRRRR(RR@t compile_flagstcompiled_pattern_listtidxtp((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytcompile_pattern_list s(      cCs"|j|}|j|||S(sl This seeks through the stream until a pattern is matched. The pattern is overloaded and may take several types. The pattern can be a StringType, EOF, a compiled re, or a list of any 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. That will cause expect to match an EOF or TIMEOUT condition instead of raising an exception. If you pass a list of patterns and more than one matches, the first match in the stream is chosen. If more than one pattern matches at that point, the leftmost in the pattern list is chosen. For example:: # the input is 'foobar' index = p.expect(['bar', 'foo', 'foobar']) # returns 1('foo') even though 'foobar' is a "better" match Please note, however, that buffering can affect this behavior, since input arrives in unpredictable chunks. For example:: # the input is 'foobar' index = p.expect(['foobar', 'foo']) # returns 0('foobar') if all input is available at once, # but returs 1('foo') if parts of the final 'bar' arrive late 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 occurred 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. 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(). (Rt expect_list(RRRRkR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR/;sM cCs|jt|||S(sThis takes a list of compiled regular expressions and returns the index into the pattern_list that matched the child output. The list may also contain EOF or TIMEOUT(which are not compiled regular expressions). This method is similar to the expect() method except that expect_list() does not recompile the pattern list on every call. This may help if you are trying to optimize for speed, otherwise just use the expect() method. This is called by expect(). If timeout==-1 then the self.timeout value is used. If searchwindowsize==-1 then the self.searchwindowsize value is used. (t expect_loopt searcher_re(Rt pattern_listRRk((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs cst|js$|ttfkr0|g}nfd}yt|}Wntk roj|nXg|D]}||^qw}jt|||S(sKThis is similar to expect(), but uses plain string matching instead of compiled regular expressions in 'pattern_list'. The 'pattern_list' may be a string; a list or other sequence of strings; or TIMEOUT and EOF. This call might be faster than expect() for two reasons: string searching is faster than RE matching and it is possible to limit the search to just the end of the input buffer. This method is also useful when you don't want to have to worry about escaping regular expression characters that you want to match.csF|ttfkr|St|jr5j|Sj|dS(N(RRR0R2R|R(R(R(se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytprepare_patterns  ( R0R2RRRR;RR tsearcher_string(RR RRkR R((Rse/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt expect_exacts  c Cs||_|dkr!|j}n|dk r@tj|}n|dkrX|j}ny|j}t|}xtrm|j|||}|dkr||j |_||j |_ ||j |j !|_ |j |_ ||_|jS|dk r|dkrtdn|j|j|}t|}tjd||}|dk rs|tj}qsqsWWnjtk rtjd} |j|_||_ t|_ |j}|dkrt|_ ||_|jSd|_ d|_tt| dt|ntk rtjd} ||_||_ t|_ |j}|dkryt|_ ||_|jSd|_ d|_tt| dt|n.||_ d|_ d|_ d|_nXdS(sThis is the common loop used inside expect. The 'searcher' should be an instance of searcher_re or searcher_string, which describes how and what to search for in the input. See expect() for other arguments, return value and exceptions. iis!Timeout exceeded in expect_any().g-C6?is N(R]RR*RRkRLtlenR.tsearchtendtstartR4R1R`RaRRR)RRRRR<t eof_indexRt timeout_index( RR]RRkRtincomingtfreshlenRDRHR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR sv                         #          #    cCs\ttdd}tjddddd}tj|j||}tjd|dd!S(smThis returns the terminal window size of the child tty. The return value is a tuple of (rows, cols). t TIOCGWINSZiht@tHHHHii(tgetattrRtstructtpacktfcntltioctlRgtunpack(RRR{tx((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt getwinsizescCs_ttdd}|dkr'd}ntjd||dd}tj|j||dS(s=This sets the terminal window size 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. t TIOCSWINSZigtlgtRiN(RRRRRRR(RtrowstcolsR!R{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs   icCs|j|j|jj|j|_tj|j}tj|jt rf|j d}nz|j |||Wdtj |jtj |XdS(sThis 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. This simply echos the child stdout and child stderr to the real stdout and it echos the real stdin to the child stdin. When the user types the escape_character this method will stop. The default for escape_character is ^]. This should not be confused with ASCII 27 -- the ESC character. ASCII 29 was chosen for historical merit because this is the character used by 'telnet' as the escape character. The escape_character will not be sent to the child process. You may pass in optional input and output filter functions. These functions should take a string and return a string. The output_filter will be passed all the output from the child process. The input_filter will be passed all the keyboard input from the user. The input_filter is run BEFORE the check for the escape_character. Note that if you change the window size of the parent the SIGWINCH signal will not be passed through to the child. If you want the child window size to change when the parent's window size changes then do something like the following example:: import pexpect, struct, fcntl, termios, signal, sys def sigwinch_passthrough (sig, data): s = struct.pack("HHHH", 0, 0, 0, 0) a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s)) global p p.setwinsize(a[0],a[1]) # Note this 'p' global and used in sigwinch_passthrough. p = pexpect.spawn('/bin/bash') signal.signal(signal.SIGWINCH, sigwinch_passthrough) p.interact() slatin-1N(RQRLR9R:R<tttyRRXtsetrawtPY3Rzt_spawn__interact_copyRt TCSAFLUSH(Rtescape_charactert input_filtert output_filtertmode((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytinteract)s' cCs?x8|dkr:|jr:tj||}||}qWdS(s/This is used by the interact() method. RN(RRRM(RRtdataR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__interact_writen\scCstj|dS(s/This is used by the interact() method. i(RR(RR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__interact_readdsc Csvxo|jrq|j|j|jggg\}}}|j|kry|j|j}Wn+tk r}|jtjkrqnX|r||}n|jdk r|jj ||jj nt j |j |n|j|kr|j|j}|r||}n|j|}|dkrX|| }|j|j|Pn|j|j|qqWdS(s/This is used by the interact() method. iN(RRRgRXt_spawn__interact_readRRtEIOR"R*RMR:RRYtrfindt_spawn__interact_writen( RR)R*R+RRRR.R((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__interact_copyjs0*   cCs|dk rtj|}nxtrytj||||SWq"tjk rtjd}|jtjkr|dk r|tj}|dkrgggfSqqq"Xq"WdS(sThis is a wrapper around select.select() that ignores signals. If select.select raises a select.error exception and errno is an EINTR error then it is ignored. Mainly this is used to ignore sigwinch (terminal resize). iiN( R*RR.tselectterrorRRRtEINTR(RtiwtdtowtdtewtdRRR((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt__selects    cCstddddS(soThis method is no longer supported or allowed. I don't like getters and setters without a good reason. s#This method is no longer supported s'or allowed. Just assign a value to the smaxread member variable.N(R(RR)((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt setmaxreadscCstddddS(s7This method is no longer supported or allowed. s#This method is no longer supported s/or allowed. Just assign a value to the logfile smember variable.N(R(Rt fileobject((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pytsetlogsN(HRRRRGR<R&RR2t staticmethodRIRRRzRQt basestringtchrRR9RMR*tencodingR.R R|R}R~RRR%RRRR=R:RRRRRRRRRRRR5RRRRRRR^RRRRRRR/RRR R RR-R4R1R'RR=R?(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs          ! [ & .      ) M         "  )  O 1Q!K  2    cBseZdZer9eZefZeeZ e j Z n-e Ze fZee Z e j jdZ ejjZdZedZedZdZdZRS(s~Works like spawn, but accepts and returns unicode strings. Extra parameters: :param encoding: The encoding to use for communications (default: 'utf-8') :param errors: How to handle encoding/decoding errors; one of 'strict' (the default), 'ignore', or 'replace', as described for :meth:`~bytes.decode` and :meth:`~str.encode`. RJcOsh|jdd|_|jdd|_tj|jd|j|_tt|j||dS(NRCsutf-8terrorststrict( tpopRCRDtcodecstgetincrementaldecodert_decoderR RR (RRyR(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR s!cCs|S(N((R{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR|scCs|S(N((R{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR}scCs|jj|dtS(Ntfinal(RIROR^(RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR~scCs%tj|j|j|j|jS(N(RRMRgRzRCRD(RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs(RRRR&RR<R2R@RBRIRRtunicodetunichrRORR9RMRQR R|R}R~R(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs          R cBs,eZdZdZdZddZRS(s-This is a plain string search helper for the spawn.expect_any() method. This helper class is for speed. For more powerful regex patterns see the helper class, searcher_re. Attributes: eof_index - index of EOF, or -1 timeout_index - index of TIMEOUT, or -1 After a successful match by the search() method the following attributes are available: start - index into the buffer, first byte of match end - index into the buffer, first byte after match match - the matching string itself cCsd|_d|_g|_xft|D]X\}}|tkrO||_q(n|tkrj||_q(n|jj||fq(WdS(sThis creates an instance of searcher_string. This argument 'strings' may be a list; a sequence of strings; or the EOF or TIMEOUT types. iN(RRt_stringsRRRR3(RtstringsRR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR s       cCsg|jD]}|dd|f^q }|jd |jdkrf|j|jd|jfn|jdkr|j|jd|jfn|jtt|d}dj|S( sUThis returns a human-readable string that represents the state of the object.is %d: "%s"issearcher_string:s %d: EOFs %d: TIMEOUTis (issearcher_string:(RMR3RRtsortR+tzipR(Rtnstss((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs*    c Csd}x|jD]\}}|dkr<|t| }n| }|j||}|dkr|dksy||kr|}||} } qqW|dkrdS| |_||_|jt|j|_| S(sThis searches 'buffer' for the first occurence of one of the search strings. 'freshlen' must indicate the number of bytes at the end of 'buffer' which have not been searched before. It helps to avoid searching the same, possibly big, buffer over and over again. See class spawn for the 'searchwindowsize' argument. If there is a match this returns the index of that string, and sets 'start', 'end' and 'match'. Otherwise, this returns -1. iiN(R*RMRRvR`RR( RRLRRkt first_matchRDR{toffsetRt best_indext best_match((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR s  $   N(RRRR RR*R(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR s  R cBs,eZdZdZdZddZRS(sUThis is regular expression string search helper for the spawn.expect_any() method. This helper class is for powerful pattern matching. For speed, see the helper class, searcher_string. Attributes: eof_index - index of EOF, or -1 timeout_index - index of TIMEOUT, or -1 After a successful match by the search() method the following attributes are available: start - index into the buffer, first byte of match end - index into the buffer, first byte after match match - the re.match object returned by a succesful re.search cCsd|_d|_g|_x{tttt||D]X\}}|tkrd||_q=n|tkr||_q=n|jj ||fq=WdS(sThis creates an instance that searches for 'patterns' Where 'patterns' may be a list or other sequence of compiled regular expressions, or the EOF or TIMEOUT types.iN( RRt _searchesRPR+RRRRR3(RR@RR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR bs   .    cCst}xo|jD]d\}}y$|j|d||jffWqtk rv|j|d||jffqXqW|jd |jdkr|j|jd|jfn|jdkr|j|jd|jfn|jtt|d}d j |S( sUThis returns a human-readable string that represents the state of the object.s %d: re.compile("%s")s %d: re.compile(%r)is searcher_re:is %d: EOFs %d: TIMEOUTis (is searcher_re:( R+RWR3RtUnicodeEncodeErrorRRRORPR(RRRRR{((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRts $ (   c Csd}|dkrd}ntdt||}xt|jD]i\}}|j||}|dkrnq>n|j} |dks| |kr>| }|} |} q>q>W|dkrdS||_| |_|jj|_| S(sThis searches 'buffer' for the first occurence of one of the regular expressions. 'freshlen' must indicate the number of bytes at the end of 'buffer' which have not been searched before. See class spawn for the 'searchwindowsize' argument. If there is a match this returns the index of that string, and sets 'start', 'end' and 'match'. Otherwise, returns -1.iiN(R*tmaxRRWRRR`R( RRLRRkRSt searchstartRDR{R`Rt the_matchRU((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs&         N(RRRR RR*R(((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR Ns  cCstjj|dkr4tj|tjr4|SndtjksVtjddkrbtj}n tjd}|jtj}x<|D]4}tjj ||}tj|tjr|SqWdS(sThis takes a given filename; tries to find it in the environment path; then checks if it is executable. This returns the full path to the filename if found and executable. Otherwise this returns None.RtPATHN( RtpathtdirnametaccesstX_OKtenvirontdefpathtsplittpathsepRR*(tfilenameRtpathlistR]tff((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyRs"   c Csg}d}d}d}d}d}d}|}x.|D]&} ||ksU||kr| dkrj|}q]| dkr|}q]| d kr|}q]| jr||krd q|j|d}|}q]|| }|}q7||kr|| }|}q7||kr/| dkr"|}q]|| }q7||kr7| d krP|}q]|| }q7q7W|dkr}|j|n|S( sThis 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. Riiiiis\t't"N(tisspaceR*R3( t command_linetarg_listtargt state_basict state_esctstate_singlequotetstate_doublequotetstate_whitespaceRRH((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyR sL                        (*RRRRR6RRRR6RWR$RRRRRRGt ImportErrorRRRR R t__all__t version_infoR&t ExceptionRRRR^R*RRR&tobjectRRR R RR (((se/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/pexpect/__init__.pyt@sZ                  L  3,cc