%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/utils/vt.pyo

�
���^c@@s�dZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
ejdkZyZddlmZmZddlmZddlZddlZddlZddlZWnMek
r>ddlZddlZddlZddlZddlZnXddlZ ddl!Z ddl"Z ddl#m$Z$dd	l%m&Z&ej'e(�Z)d
e*fd��YZ+gZ,d�Z-d
e.fd��YZ/dS(uh
    :codeauthor: Pedro Algarvio (pedro@algarvio.me)


    salt.utils.vt
    ~~~~~~~~~~~~~

    Virtual Terminal

    This code has been heavily inspired by Python's subprocess code, the `non
    blocking version of it`__, some minor online snippets about TTY handling
    with python including `Python's own ``pty`` source code`__ and `Pexpect`__
    which has already surpassed some of the pitfalls that some systems would
    get us into.

    .. __: http://code.activestate.com/recipes/440554/
    .. __: https://github.com/python-mirror/python/blob/3.3/Lib/pty.py
    .. __: https://github.com/pexpect/pexpect

i(tabsolute_importtprint_functiontunicode_literalsN(tsixuwin32(tReadFilet	WriteFile(t
PeekNamedPipe(tstring_types(t
LOG_LEVELStTerminalExceptioncB@seZdZRS(u%
    Terminal specific exception
    (t__name__t
__module__t__doc__(((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR	@scC@sVxOtD]F}|j�}|tk	rytj|�WqNtk
rJqNXqqWdS(uw
    Make sure that any terminal processes still running when __del__ was called
    to the waited and cleaned up.
    N(t_ACTIVEtisalivetTruetremovet
ValueError(tinsttres((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt_cleanupNs
tTerminalcB@s�eZdZddedddddddddddddd�Zd�Zejd�Z	dd�Z
eed�Ze
d��Zd�Zd	�Zd
�Zer�d�Zd�Zd
�Zd�Zd�Zd�ZeZn�d�Zd�Zd�Zd�Zd�Zd�Zd�Zejejej ej!ej"ej#ej$ej%e&j'e(d�
Z)ed�Zd�Z*d�Zd�Ze+j,e-d�Z.RS(u 
    I'm a virtual terminal
    udebugcC@s=t�|r$|r$td��n||_||_||_||_||_||_|dkr�|dkr�|j	�\}}nZ|dk	r�|dkr�|j	�\}}n-|dkr�|dk	r�|j	�\}}n||_
||_d|_d|_
d|_d|_d|_d|_t|_t|_t|_t|_d|_d|_d|_dtjj�k|_|tkr�tj|_n�|tkr�d|_nl|dk	r
t |d�s�t |d�s�t |d�r�td��n||_ntdj!|���|tkr:tj|_"n�|tkrRd|_"nl|dk	r�t |d�s�t |d�s�t |d�r�td��n||_"ntd	j!|���y|j#�WnAt$k
r}t%j&d
|dt'j(�tdj!|���nXt%j)d
|j|j|j�dj*|j�}d|ksYd|kryt%j)d�t%j+d|�nt%j)d|�t,j-|
|
�|_.|	tkr�t'j/dj!t0|j1j0|j��|_2nB|	dk	rt3|	t'j4�st5d��n|	|_2n	d|_2t,j-||�|_6|tkrgt'j/dj!t0|j1j0|j��|_7nB|dk	r�t3|t'j4�s�t5d��n||_7n	d|_7t,j-||�|_8|
tkr�t'j/dj!t0|j1j0|j��|_9nB|
dk	r0t3|
t'j4�s$t5d��n|
|_9n	d|_9dS(Nu6You need to pass at least one of "args", "executable" uirixuwriteuflushucloseuU'stream_stdout' needs to have at least 3 methods, 'write()', 'flush()' and 'close()'.uEDon't know how to handle '{0}' as the VT's 'stream_stdout' parameter.uU'stream_stderr' needs to have at least 3 methods, 'write()', 'flush()' and 'close()'.uEDon't know how to handle '{0}' as the VT's 'stream_stderr' parameter.uFailed to spawn the VT: %stexc_info_on_loglevelu"Failed to spawn the VT. Error: {0}u3Child Forked! PID: %s  STDOUT_FD: %s  STDERR_FD: %su udecode("base64")ubase64.b64decode(u<VT: Salt-SSH SHIM Terminal Command executed. Logged to TRACEuTerminal Command: %su{0}.{1}.PID-{2}.STDINu.'log_stdin' needs to subclass `logging.Logger`u{0}.{1}.PID-{2}.STDOUTu/'log_stdout' needs to subclass `logging.Logger`u{0}.{1}.PID-{2}.STDERRu/'log_stderr' needs to subclass `logging.Logger`(:RR	targst
executabletshelltcwdtenvt
preexec_fntNonet&_Terminal__detect_parent_terminal_sizetrowstcolstpidtstdintstdouttstderrtchild_fdt	child_fdeRtclosedtFalsetflag_eof_stdouttflag_eof_stderrt
terminatedt
exitstatustsignalstatuststatustsystplatformtlowert_Terminal__irix_hackt
stream_stdoutthasattrtformatt
stream_stderrt_spawnt	ExceptiontlogtwarningtloggingtDEBUGtdebugtjointtraceRtgettstdin_logger_levelt	getLoggerR
t	__class__tstdin_loggert
isinstancetLoggertRuntimeErrortstdout_logger_levelt
stdout_loggertstderr_logger_levelt
stderr_logger(tselfRRRRRRRR t	log_stdintlog_stdin_levelt
log_stdouttlog_stdout_levelt
log_stderrtlog_stderr_levelR3R6t_terrtterminal_command((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt__init__cs�																						

!	!	!cC@s
|j|�S(ui
        Send data to the terminal. You are responsible to send any required
        line feeds.
        (t_send(RLtdata((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pytsend scC@s|jdj||��S(uO
        Send the provided data to the terminal appending a line feed.
        u{0}{1}(RYR5(RLRXtlinesep((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pytsendline'scC@s7|dkrd}n|dkr*d}n|j|�S(u�
        Receive data from the terminal as a (``stdout``, ``stderr``) tuple. If
        any of those is ``None`` we can no longer communicate with the
        terminal's child process.
        iiN(Rt_recv(RLtmaxsize((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pytrecv-s
		cC@s�|js�|jdk	r4tj|j�d|_n|jdk	r_tj|j�d|_ntjd�|r�|j|�s�t	d��q�nt
|_ndS(u
        Close the communication with the terminal's child.
        If ``terminate`` is ``True`` then additionally try to terminate the
        terminal, and if ``kill`` is also ``True``, kill the terminal if
        terminating it was not enough.
        g�������?u"Failed to terminate child process.N(R'R%RtostcloseR&ttimetsleept	terminateR	R(RLRctkill((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR`9s	
cC@s|jtkp|jtkS(N(R*R(R)(RL((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pythas_unread_dataMscC@s*|dks|rdS|jdtj�S(Nu
(RtreplaceR_RZ(RLRX((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt_translate_newlinesTscC@s|S(N((RL((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt	__enter__\scC@s3|jdtdt�|j�r/|j�ndS(NRcRd(R`RRtwait(RLtexc_typet	exc_valuet	traceback((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt__exit___scC@s
t�dS(N(tNotImplementedError(RL((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt_executeiscC@s
t�dS(N(Rn(RL((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR7lscC@s
t�dS(N(Rn(RLR]((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR\oscC@s
t�dS(N(Rn(RLRX((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRWrscC@s�|tjkr|j�ne|tjkrDtj|jtj�n=|tjkrltj|jtj�ntdj	|���dS(u6
            Send a signal to the process
            uUnsupported signal: {0}N(
tsignaltSIGTERMRctCTRL_C_EVENTR_RdR!tCTRL_BREAK_EVENTRR5(RLtsig((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pytsend_signalus
cC@s_ytj|jd�WnAtk
rZtj|j�}|tjkrN�n||_nXdS(u0
            Terminates the process
            iN(	twin32apitTerminateProcesst_handletOSErrortwin32processtGetExitCodeProcesstwin32contSTILL_ACTIVER,(RLtecode((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRc�s
cC@sx|j�\|_|_|_t|jt�r?|jg}n!|jrZt|j�}ng}|jr�|jr�dddj	|�g|_n!|jr�dg|_n	||_|j
r�|j
|jd<n|j
dkr�|jd|_
n|jdkrbtj
j�|_
tjj�|_tjj�|_|j
|_tj|j�r�y|j|j|j�Wq�tk
r�}tjd|dtj�q�Xntjtj�}ytjtj d|d�Wnt!k
r�nX|j"dk	rtj#|j"�n|j$r|j$�n|j%dkrCtj&|j
|j�qbtj'|j
|j|j%�nt(|_)t(|_*dS(Nu/bin/shu-cu iu&Failed to set the VT terminal size: %sRi(+t_Terminal__fork_ptysR!R%R&RERRtlistRR>RRR/R"tfilenoR#R$R_tisattyt
setwinsizeRR tIOErrorR9R:R;R<tresourcet	getrlimitt
RLIMIT_NOFILEt
closerangetptyt
STDERR_FILENORyRtchdirRRtexecvptexecvpeR(R'R+(RLRRTtmax_fd((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR7�sR				
	
	cC@s�tj�\}}|dks*|dkr9td��ntj�\}}|dksc|dkrrtd��ntj�}|tjkr�td��n�|tjkrktj|�tj|�tjj	j
�tj|�}y<tjdtj
tjB�}|dkrtj|�nWntk
r3nXtj�yHtjdtj
tjB�}|dkr�tj|�td��nWntk
r�nXtj|tj
�}|dkr�tdj|���n
tj|�tjdkr/tjdtj�}|dkrtd	��q/tj|�ntj|tj�tj|tj�tj|tj�n*tjj	j
�tj|�tj|�|||fS(
u�
            Fork the PTY

            The major difference from the python source is that we separate the
            stdout from stderr output.
            iuFailed to open a TTY for stdoutuFailed to open a TTY for stderruFailed to forku/dev/ttyuQFailed to disconnect from controlling tty. It is still possible to open /dev/tty.uCould not open child pty, {0}uposixu(Could not open controlling tty, /dev/tty(R�topenptyR	R_tforktCHILDR`tsalttutilstcryptt
reinit_cryptotttynametopentO_RDWRtO_NOCTTYR8tsetsidR5tnametO_WRONLYtdup2tSTDIN_FILENOt
STDOUT_FILENOR�(RLtstdout_parent_fdtstdout_child_fdtstderr_parent_fdtstderr_child_fdR!t
child_namettty_fd((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt__fork_ptys�s^








cC@s�|jdkrdStjg|jggd�ds9dSye|jr^|jj|j|�ntjr�tj	|j|j
t��}ntj	|j|�}WnEtk
r�}|j
t
jkr�tj|j�d|_dS�nX|S(Nii(R%RtselectRDR9RARtPY3R_twritetencodet__salt_system_encoding__RyterrnotEPIPER`(RLRXtwrittentwhy((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRW2s""		!	c
C@s�g}|jr"|j|j�n|jr>|j|j�n|j�s�|sTdStj|ggd�\}}}|s�t|_|_t	j
d�dSnT|jr�tj|ggd�\}}}|s�t|_|_t	j
d�d	Snd}d}|jr#tj|jtj
�}n|jrGtj|jtj
�}n|jrstj|jtj|tjB�n|jr�tj|jtj|tjB�ntj|ggd�\}}}|s�|j�s�t|_|_t	j
d�d
Sn|j|krHzy�|jtjjjtj|j|���}|sPt|_d}n�|jry|jj|�|jj�n|jr�|j�}	|	jtj�r�|	ttj�}	n|	r�|jj	|j|	�q�nWn9t k
rtj!|j�d|_t|_d}nXWd|jdk	rDtj|jtj|�nXn|j|kr�z y�|jtjjjtj|j|���}|s�t|_d}n�|j"r�|j"jtjjj#|��|j"j�n|j$r9|j�}	|	jtj�r|	ttj�}	n|	r9|j$j	|j%|	�q9nWn9t k
rutj!|j�d|_t|_d}nXWd|jdk	r�tj|jtj|�nXn||fS(Niu&End of file(EOL). Brain-dead platform.iu End of file(EOL). Slow platform.uu%End of file(EOL). Very slow platform.(NN(NN(NN(NN(&R%tappendR&RRR�RR)R*R9R=R2tfcntltF_GETFLtF_SETFLR_t
O_NONBLOCKRgR�R�tstringutilst
to_unicodetreadR6R�tflushRKtrstript
startswithRZtlenRJRyR`R3tto_strRIRH(
RLR]trfdstrlistRSR$R#tfd_flagst	fde_flagststripped((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR\Hs�		!
	!
				!
				 
		 				 
		 cC@syfttdd�}tjddddd�}tjtjj�||�}tj	d|�dd!SWnt
k
rzdSXdS(	Nu
TIOCGWINSZiht@tHHHHiiiiP(iiP(tgetattrttermioststructtpackR�tioctlR/R"R�tunpackR�(RLt
TIOCGWINSZtpackedR�((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt__detect_parent_terminal_size�s
cC@sz|jdkrtd��nttdd�}tjddddd�}tj|j||�}tj	d|�dd!S(u�
            This returns the terminal window size of the child tty. The return
            value is a tuple of (rows, cols).

            Thank you for the shortcut PEXPECT
            uTCan't check the size of the terminal since we're not connected to the child process.u
TIOCGWINSZiht@R�iiN(
R%RR	R�R�R�R�R�R�R�(RLR�R�R�((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt
getwinsize�scC@s\ttdd�}|dkr'd}ntjd||dd�}tj|j||�dS(u�
            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.

            Thank you for the shortcut PEXPECT
            u
TIOCSWINSZigt�Igt�R�iN(R�R�R�R�R�R�R%(RLRR t
TIOCSWINSZR�((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR��s
	cC@s�|jr
tS|jtkr%d}n|}y||j|�\}}
WnE|k
r�tj�d}|j|	kr�|
d��q�|�nX|dkry||j|�\}}
Wn4|k
r�}|j|	kr�|
d��q��nX|dkrtSn|dkrtS||
�rJ|
|_||
�|_	d|_t|_nT||
�r�|
|_d|_	||
�|_t|_n||
�r�|
d��ntS(u�
            This 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.
            iiu�isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?u�isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?u�isalive() encountered condition where child process is stopped. This is not supported. Is some other process attempting job control with our child pid?N(R+R(ReR!R/texc_infoR�RR.R,RR-(RLt_waitpidt_wnohangt
_wifexitedt_wexitstatust_wifsignaledt_wifstoppedt	_wtermsigt	_os_errort
_errno_echildt_terminal_exceptiontwaitpid_optionsR!R.RTtexc((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR�sN		

				cC@s.|js|jdt�n|j�s,tSy�|jtj�tj	d�|j�s\tS|jtj
�tj	d�|j�s�tS|jtj�tj	d�|j�s�tS|r�|jtj�tj	d�|j�s�tStSntSWn2t
k
r)tj	d�|j�s"tStSnXdS(u$
            This 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.
            Rcg�������?N(R'R`R(RRRuRptSIGHUPRaRbtSIGCONTtSIGINTtSIGKILLRy(RLtforce((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRcZs:	





cC@sg|j�rTxQ|j�rP|j�\}}|dkr=Pn|dkrPqqWntd��|jS(u�
            This waits until the child exits internally consuming any remaining
            output from the child, thus, no blocking forever because the child
            has unread data.
            u#Cannot wait for dead child process.N(RR^RR	R,(RLR#R$((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRi�scC@stj|j|�dS(u6
            Send a signal to the process
            N(R_RdR!(RLRt((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRu�scC@s|jtj�dS(u7
            Kill the process with SIGKILL
            N(RuRpR�(RL((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyRd�scC@s?|jdkrdS|j�r;tdk	r;tj|�ndS(N(R!RRR
R�(RLt_maxsizet_active((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt__del__�sN(/R
RRRR(RVRYR_RZR[R^RR`tpropertyReRgRhRmt	mswindowsRoR7R\RWRuRcRdRRR�R�twaitpidtWNOHANGt	WIFEXITEDtWEXITSTATUStWIFSIGNALEDt
WIFSTOPPEDtWTERMSIGterrorR�tECHILDR	RRiR/R]R
R�(((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyR_sn�												A	Z		}			R,				(0Rt
__future__RRRR_R/RaR�RpR�R;tsalt.extRR0R�t	win32fileRRt	win32pipeRtmsvcrtRvR|RztImportErrorR�R�R�R�R�tsalt.utils.cryptR�tsalt.utils.datatsalt.utils.stringutilstsalt.ext.sixRtsalt.log.setupRRBR
R9R8R	R
RtobjectR(((s1/usr/lib/python2.7/site-packages/salt/utils/vt.pyt<module>sB
	

Zerion Mini Shell 1.0