o
    Nrf                     @   s   d dl Z d dlZd dlZd dlZd dlZzddlmZ W n ey)   e	dZY nw dZ
dZG dd deZdZdd	d
Zdd Zdd Zdd Zdd Zdd ZdddZdd ZdS )    N   )NpipeSocket   c                   @   s   e Zd ZdS )SocketErrorN)__name__
__module____qualname__ r	   r	   \/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/docker/utils/socket.pyr      s    r   m      c              
   C   s  t jt jt jf}t| ts-ttdst| gg g  nt }|	| tj
tjB  |  z t| dr9| |W S t| tjrE| |W S t|  |W S  tyf } z|j |vr[ W Y d}~dS d}~w ty } zt| tot|jdko|jd tk}|rW Y d}~dS  d}~ww )z+
    Reads at most n bytes from socket
    pollrecvNr    )errnoZEINTRZEDEADLKEWOULDBLOCK
isinstancer   hasattrselectr   registerPOLLINPOLLPRIr   pysocketSocketIOreadosfilenoOSError	ExceptionlenargsNPIPE_ENDED)socketnZrecoverable_errorsr   eZis_pipe_endedr	   r	   r
   r      s:   




r   c                 C   sF   d}t ||k r!t| |t | }|std||7 }t ||k s|S )z]
    Reads exactly n bytes from socket
    Raises SocketError if there isn't enough data
        zUnexpected EOF)r   r   r   )r"   r#   dataZ	next_datar	   r	   r
   read_exactly>   s   r'   c                 C   s<   zt | d}W n
 ty   Y dS w td|\}}||fS )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actualr	   r	   r
   next_frame_headerL   s   r.   c                 C   s   |rd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                 s   s    | ]}t |fV  qd S )N)STDOUT).0framer	   r	   r
   	<genexpr>e   s    zframes_iter.<locals>.<genexpr>)frames_iter_ttyframes_iter_no_tty)r"   ttyr	   r	   r
   frames_iter\   s   r6   c                 c   sh    	 t | \}}|dk rdS |dkr3t| |}|du rqt|}|dkr&dS ||8 }||fV  |dksq)zc
    Returns a generator of data read from the socket when the tty setting is
    not enabled.
    Tr   N)r.   r   r   )r"   r,   r#   resultZdata_lengthr	   r	   r
   r4   j   s    

r4   c                 c   s$    	 t | }t|dkrdS |V  q)z^
    Return a generator of data read from the socket when the tty setting is
    enabled.
    Tr   N)r   r   )r"   r7   r	   r	   r
   r3      s   r3   Fc                 C   s   |du r	d | S ddg}| D ]=}|dksJ |d dur5|d du r*|d |d< q|d  |d 7  < q|d du rB|d |d< q|d  |d 7  < q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.
    Fr%   N)NNr   r   )jointuple)framesZdemuxoutr1   r	   r	   r
   consume_socket_output   s   
r<   c                 C   s.   | 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)Z	stream_idr&   r	   r	   r
   demux_adaptor   s
   r?   )r   )F)r   r   r   r"   r   r*   	transportr   ImportErrortyper/   r=   r   r   r!   r   r'   r.   r6   r4   r3   r<   r?   r	   r	   r	   r
   <module>   s,    
$
%