
    2.a                         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dZd Zd	 Zd
 Zd Zd ZddZd Zy# e$ r  e	d      ZY 9w xY w)    N   )NpipeSocket   c                       e Zd Zy)SocketErrorN)__name__
__module____qualname__     5/usr/lib/python3/dist-packages/docker/utils/socket.pyr   r      s    r   r   c                    t         j                  t         j                  t         j                  f}t	        | t
              st        j                  | gg g        	 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w xY w)z+
    Reads at most n bytes from socket
    recvSocketION)errnoEINTREDEADLKEWOULDBLOCK
isinstancer   selecthasattrr   getattrpysocketreadosfilenoOSError)socketnrecoverable_errorses       r   r   r      s    
  ++u}}e6G6GHfk*vhB'66";;q>!fgh
;<;;q>!wwv}}** 77,, -s$   C 6*C !#C 	C'C""C'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)byteslenr   r   )r   r   data	next_datas       r   read_exactlyr'   *   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
       )r*   z>BxxxL)r'   r   structunpack)r   r%   streamactuals       r   next_frame_headerr/   8   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>Q   s     EEEs   )frames_iter_ttyframes_iter_no_tty)r   ttys     r   frames_iterr9   H   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)r/   r   r$   )r   r-   r   resultdata_lengths        r   r7   r7   V   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   r;   s     r   r6   r6   k   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outr4   s       r   consume_socket_outputrD   x   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)r2   STDERR
ValueError)	stream_idr%   s     r   demux_adaptorrI      s;    
 Fd|	f	d|I;&<=>>r   )i   )F)r   r   r   r   r   r+   	transportr   ImportErrortyper2   rF   	Exceptionr   r   r'   r/   r9   r7   r6   rD   rI   r   r   r   <module>rN      sx     	   '
 
	
	) 	* *#*
"J
?i  t*Ks   A AA