
                             d dl Z d dlZd dlZd dlZd dlZ	 ddlmZ dZ
dZ G d de      ZdZddZd	 Zd
 Zd Zd Zd ZddZd Zy# e$ r  e	d      ZY ;w xY w)    N   )NpipeSocket   c                       e Zd Zy)SocketErrorN)__name__
__module____qualname__     &lib/third_party/docker/utils/socket.pyr   r      s    r   r   m   c                 L   t         j                  t         j                  t         j                  f}t	        | t
              s~t        t        d      st        j                  | gg g        nUt        j                         }|j                  | t        j                  t        j                  z         |j                          	 t        | d      r| j                  |      S t	        | t        t        d            r| j                  |      S t!        j                  | j#                         |      S # t$        $ r}|j                   |vr Y d}~yd}~wt&        $ rO}t	        | t
              xr0 t)        |j*                        dkD  xr |j*                  d   t,        k(  }|rY d}~y d}~ww xY w)z+
    Reads at most n bytes from socket
    pollrecvSocketIONr    )errnoEINTREDEADLKEWOULDBLOCK
isinstancer   hasattrselectr   registerPOLLINPOLLPRIr   getattrpysocketreadosfilenoOSError	ExceptionlenargsNPIPE_ENDED)socketnrecoverable_errorsr   eis_pipe_endeds         r   r    r       s:   
  ++u}}e6G6GHfk*vv&MM6(B+;;=DMM&&--&.."@AIIK66";;q>!fgh
;<;;q>!wwv}}** 77,, - #FK8 2QVVq2k1 	  s7   ?D+ *D+ #D+ +	F#4EF#AFFF#c                     t               }t        |      |k  r9t        | |t        |      z
        }|st        d      ||z  }t        |      |k  r9|S )z]
    Reads exactly n bytes from socket
    Raises SocketError if there isn't enough data
    zUnexpected EOF)bytesr%   r    r   )r(   r)   data	next_datas       r   read_exactlyr1   >   sW    
 7D
d)a-SY/	.//		 d)a-
 Kr   c                 t    	 t        | d      }t        j                  d|      \  }}||fS # t        $ r Y yw xY w)z
    Returns the stream and size of the next frame of data waiting to be read
    from socket, according to the protocol defined here:

    https://docs.docker.com/engine/api/v1.24/#attach-to-a-container
       )r4   z>BxxxL)r1   r   structunpack)r(   r/   streamactuals       r   next_frame_headerr9   L   sH    FA& ]]8T2NFFF	  s   + 	77c                 @    |rd t        |       D        S t        |       S )z
    Return a generator of frames read from socket. A frame is a tuple where
    the first item is the stream number and the second item is a chunk of data.

    If the tty setting is enabled, the streams are multiplexed into the stdout
    stream.
    c              3   ,   K   | ]  }t         |f  y w)N)STDOUT).0frames     r   	<genexpr>zframes_iter.<locals>.<genexpr>e   s     E-DE-Ds   )frames_iter_ttyframes_iter_no_tty)r(   ttys     r   frames_iterrC   \   s"     E_V-DEE!&))r   c              #      K   	 t        |       \  }}|dk  ry|dkD  r1t        | |      }|t        |      }|dk(  ry||z  }||f |dkD  r1Kw)zc
    Returns a generator of data read from the socket when the tty setting is
    not enabled.
    r   N)r9   r    r%   )r(   r7   r)   resultdata_lengths        r   rA   rA   j   st     
 '/q5!e&!_F~f+KaA6"" !e	 s   AAAc              #   J   K   	 t        |       }t        |      dk(  ry| w)z^
    Return a generator of data read from the socket when the tty setting is
    enabled.
    r   N)r    r%   )r(   rE   s     r   r@   r@      s-     
 fv;! s   !#c                     |du rt               j                  |       S ddg}| D ]K  }|dk7  sJ |d   |d   	|d   |d<   |dxx   |d   z  cc<   .|d   	|d   |d<   <|dxx   |d   z  cc<   M t        |      S )a  
    Iterate through frames read from the socket and return the result.

    Args:

        demux (bool):
            If False, stdout and stderr are multiplexed, and the result is the
            concatenation of all the frames. If True, the streams are
            demultiplexed, and the result is a 2-tuple where each item is the
            concatenation of frames belonging to the same stream.
    FN)NNr   r   )r.   jointuple)framesdemuxoutr>   s       r   consume_socket_outputrN      s     ~ w||F## ,C $$$81v~qAA%("1v~qAA%("  :r   c                 R    | t         k(  r|dfS | t        k(  rd|fS t        |  d      )z[
    Utility to demultiplex stdout and stderr when reading frames from the
    socket.
    Nz is not a valid stream)r<   STDERR
ValueError)	stream_idr/   s     r   demux_adaptorrS      s;    
 Fd|	f	d|I;&<=>>r   )i   )F)r   r!   r   r(   r   r5   	transportr   ImportErrortyper<   rP   r$   r   r'   r    r1   r9   rC   rA   r@   rN   rS   r   r   r   <module>rW      s     	   '
 
	
	) 	 !H *#*
"J
?Q  t*Ks   A AA