o
    ;i                      @   s   d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZmZ dd	lmZ erLdd
lmZ ddlmZ ddededejfddZdddedejfddZ			d dddddee dee dee f
ddZdS )!zPTY utilities and log streaming for Modal CLI.

This module contains PTY/terminal handling utilities and log streaming functions
that handle interactive terminal sessions and application log output.
    N)TYPE_CHECKINGOptional)StreamTerminatedError)Retry)	get_winszstream_from_stdin)logger)InternalErrorServiceError)api_pb2)OutputManager)_ClientFshellno_terminate_on_idle_stdinreturnc              
   C   sR   t  \}}tjd||tjdtjdtjd| r"tjj|dS tjj|dS )NTTERM	COLORTERMTERM_PROGRAM)enabled
winsz_rows
winsz_colsenv_termenv_colortermenv_term_programpty_typer   )r   r   PTYInfoosenvirongetPTY_TYPE_SHELLPTY_TYPE_FUNCTION)r   r   rowscols r#   E/home/ubuntu/.local/lib/python3.10/site-packages/modal/_output/pty.pyget_pty_info   s   




r%   clientr   exec_idfinish_eventc              	      sn   dt dtf fdd}t|dd4 I dH  | I dH  W d  I dH  dS 1 I dH s0w   Y  dS )zL
    Streams stdin to the given exec id until finish_event is triggered
    datamessage_indexc                    s6    j jtjtj| |ddtdddI d H  d S )N)messager*   )r'   input
   )total_timeout)retry)stubContainerExecPutInputr   ContainerExecPutInputRequestRuntimeInputMessager   )r)   r*   r&   r'   r#   r$   _handle_input7   s   z-stream_pty_shell_input.<locals>._handle_inputT)use_raw_terminalN)bytesintr   wait)r&   r'   r(   r5   r#   r4   r$   stream_pty_shell_input2   s
   .r:   
output_mgrr   app_idtask_id
sandbox_idc                    sN  dd d d fdd	dt jdt jffdd  	
fdd	}	 z| I d H  W n\ ttttjtfy } zJt	|ttfrWt
d W Y d }~q.t	|trgt
d W Y d }~q.t	|tjrxt
d W Y d }~q.t	|trdt|v rt
d W Y d }~q. d }~ww d u rnq/	 I d H  t
d d S )N c               
      s    rAt ddd    d  rCz,zI d H  W n ty3 }  ztd|   W Y d } ~ n
d } ~ ww W d d S W d d S d w d S d S )Nr?   )endz#Exception in PTY shell input task: )printset	Exceptionr   	exception)exc)pty_shell_finish_eventpty_shell_input_taskr#   r$   stop_pty_shellP   s$   z)get_app_logs_loop.<locals>.stop_pty_shell	log_batchlogc                    s   |j r  | j|j  |j tjkr j| jddd d d S d S |jjs(|jj	rM|jj
tjkrA j| j|jj	|jj|jjd d S td|jj
  d S |jrfr\ |I d H  d S  |I d H  d S d S )N   )function_id	completedtotaldescriptionz%Received unrecognized progress type: )
task_stateupdate_task_stater=   r   TASK_STATE_WORKER_ASSIGNEDupdate_queueing_progressrM   task_progresslenposprogress_typeFUNCTION_QUEUEDrP   r   debugr)   put_pty_contentput_log_content)rJ   rK   )r;   rG   r#   r$   _put_log_   s,   


z#get_app_logs_loop.<locals>._put_logc                     s  t jpd
p	dpddd} jj| 2 zd3 d H W }|jr$|j|jr0td d  nN|j	r7j
s7n7|jr`rAtd n-t |j    d tt|jn|jD ]
} ||I d H  qc|jr||jkr|	 I d H  q6   d S )Nr?   7   )r<   r=   r>   timeoutlast_entry_idzApp logs are donez/ERROR: concurrent PTY shells are not supported.T)r   AppGetLogsRequestr0   
AppGetLogsunary_streamentry_idapp_doner   rZ   image_id_show_image_logspty_exec_idrB   asyncioEventr=   flush_linesstop_status_spinnerset_quiet_modecreate_taskr:   itemseof)requestrJ   rK   r]   r<   r&   last_log_batch_entry_idr;   rG   rH   pty_shell_task_idr>   rI   r=   r#   r$   	_get_logsw   sF   



&z$get_app_logs_loop.<locals>._get_logsTz$Log fetching timed out. Retrying ...zStream closed. Retrying ...zLost connection. Retrying ..._write_appdatazLogging exited gracefully)r   TaskLogsBatchTaskLogsr
   r	   r   socketgaierrorAttributeError
isinstancer   rZ   str)r&   r;   r<   r=   r>   ru   rF   r#   rr   r$   get_app_logs_loopC   sB    4





r~   )F)NNN) __doc__ri   r   ry   typingr   r   grpclib.exceptionsr   modal._utils.grpc_utilsr   modal._utils.shell_utilsr   r   modal.configr   modal.exceptionr	   r
   modal_protor   modal._output.managerr   modal.clientr   boolr   r%   r}   rj   r:   r~   r#   r#   r#   r$   <module>   s<   