
                             S SK r S SKrS SKrS SKrS SKr SSKJr  Sr
Sr " S S\5      rSrSS jrS	 rS
 rS rS rS rSS jrS rg! \ a    \	" S5      r N>f = f)    N   )NpipeSocket   c                       \ rS rSrSrg)SocketError    N)__name__
__module____qualname____firstlineno____static_attributes__r	       &lib/third_party/docker/utils/socket.pyr   r      s    r   r   m   c                    [         R                  [         R                  [         R                  4n[	        U [
        5      (       d  [        [        S5      (       d  [        R                  " U // / 5        OV[        R                  " 5       nUR                  U [        R                  [        R                  -  5        UR                  5          [        U S5      (       a  U R                  U5      $ [	        U [        [        S5      5      (       a  U R                  U5      $ [         R                  " U R#                  5       U5      $ ! [$         a  nUR                   U;  a  e  SnAgSnAf[&         a^  n[	        U [
        5      =(       a5    [)        UR*                  5      S:  =(       a    UR*                  S   [,        :H  nU(       a   SnAge SnAff = f)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#      sF   
  ++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   !E -/E $E 
GE""G/AGGGc                     [        5       n[        U5      U:  a>  [        X[        U5      -
  5      nU(       d  [        S5      eX#-  n[        U5      U:  a  M>  U$ )zQ
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_exactlyr4   >   sS    
 7D
d)a-SY/	.//	 d)a-
 Kr   c                 t     [        U S5      n[        R                  " SU5      u  p#X#4$ ! [         a     gf = f)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
   )r7   z>BxxxL)r4   r   structunpack)r+   r2   streamactuals       r   next_frame_headerr<   L   sD    FA& ]]8T2NF	  s   * 
77c                 J    U(       a  S [        U 5       5       $ [        U 5      $ )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   2   #    U  H  n[         U4v   M     g 7f)N)STDOUT).0frames     r   	<genexpr>frames_iter.<locals>.<genexpr>e   s     E-DE-Ds   )frames_iter_ttyframes_iter_no_tty)r+   ttys     r   frames_iterrG   \   s"     E_V-DEE!&))r   c              #      #     [        U 5      u  pUS:  a  gUS:  a3  [        X5      nUc  M  [        U5      nUS:X  a  gX$-  nX4v   US:  a  M3  MO  7f)zW
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   rE   rE   j   sk     
 '/q5!e&_F~f+KaA"" !e	 s   AAAc              #   N   #     [        U 5      n[        U5      S:X  a  gUv   M!  7f)zR
Return a generator of data read from the socket when the tty setting is
enabled.
r   N)r#   r(   )r+   rI   s     r   rD   rD      s,     
 fv;! s   #%c                    USL a  [        5       R                  U 5      $ SS/nU  HS  nUS:w  d   eUS   b"  US   c
  US   US'   M!  US==   US   -  ss'   M3  US   c
  US   US'   MC  US==   US   -  ss'   MU     [        U5      $ )as  
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   )r1   jointuple)framesdemuxoutrA   s       r   consume_socket_outputrR      s     ~ w||F## ,C $$$81v~qAA%("1v~qAA%("  :r   c                 V    U [         :X  a  US4$ U [        :X  a  SU4$ [        U  S35      e)zO
Utility to demultiplex stdout and stderr when reading frames from the
socket.
Nz is not a valid stream)r?   STDERR
ValueError)	stream_idr2   s     r   demux_adaptorrW      s;    
 Fd|	f	d|I;&<=>>r   )i   )F)r   r$   r   r+   r"   r8   	transportr   ImportErrortyper?   rT   r'   r   r*   r#   r4   r<   rG   rE   rD   rR   rW   r	   r   r   <module>r[      s     	   '
 
	
	) 	 !H *#*
"J
?Q  t*Ks   A
 
AA