o
    ;i                  
   @   s  d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZmZmZmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" ej#ddddZ$e$%dej&edfdee' de(fddZ)e$%dej*ddej+ddddfd e'd!e(fd"dZ,e$%d#ej&ej+dd$d%ej*ddej*d&dfd'ee( d e'd(e-e' fd)d#Z.e$%d*ej&ej*ddfd e'fd+d*Z/dS ),    )OptionalUnionN)Column)Text)_get_environment_name)get_pty_info)synchronizer)timestamp_to_localized_str)
ENV_OPTIONdisplay_tableis_ttystream_app_logs)_Client)config)_ContainerProcess)
ensure_env)InvalidError)
StreamType)api_pb2	containerz)Manage and connect to running containers.T)namehelpno_args_is_helplistFenvjsonc              	      s   t | } t I dH }t| }|jtj|dI dH }tdddtddddd	g}g }|j	j
d
d dd |j	D ]}||j|j|j|jrMt|j|ndg q:t|||d| d dS )z/List all containers that are currently running.N)environment_nameContainer ID   )	min_widthzApp ID   zApp Namez
Start Timec                 S   s   | j S )N)
started_at)task r#   G/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/container.py<lambda>*   s    zlist_.<locals>.<lambda>T)keyreversePendingz"Active Containers in environment: )r   title)r   r   from_envr   stubTaskListr   TaskListRequestr   taskssortappendtask_idapp_idapp_descriptionr!   r	   r   )r   r   clientr   rescolumn_namesrows
task_statsr#   r#   r$   list_   s.   




	r9   logsr   )r   z--timestampsz!Show timestamps for each log linecontainer_id
timestampsc                 C   sH   d\}}|  dr| }n|  dr| }ntd|  t|||d dS )z;Show logs for a specific container, streaming while active.)NNzsb-zta-zInvalid container ID: )r1   
sandbox_idshow_timestampsN)
startswithr   r   )r;   r<   r1   r=   r#   r#   r$   r:   8   s   

execzRun the command using a PTY.)defaultr   zA command to run inside the container.

To pass command-line flags or options, add `--` before the start of your commands. For example: `modal container exec <id> -- /bin/bash -c 'echo hi'`ptycommandc                    s   | du rt  } t I dH }tj||| rtddndtdd}|j	|I dH }| r;t
|j|| I dH  dS t
|j||tjtjd I dH  dS )z!Execute a command in a container.NT)shellfunction_runtime_debug)r1   rC   pty_inforuntime_debug)stdoutstderr)r   r   r*   r   ContainerExecRequestr   r   getr+   ContainerExecr   exec_idattachr   STDOUTwait)rB   r;   rC   r4   reqr5   r#   r#   r$   r@   H   s$   stopc                    s2   t  I dH }tj| d}|j|I dH  dS )zStop a currently-running container and reassign its in-progress inputs.

    This will send the container a SIGINT signal that Modal will handle.
    N)r1   )r   r*   r   ContainerStopRequestr+   ContainerStop)r;   r4   requestr#   r#   r$   rR   k   s   )0typingr   r   typer
rich.tabler   	rich.textr   modal._objectr   modal._output.ptyr   modal._utils.async_utilsr   modal._utils.time_utilsr	   modal.cli.utilsr
   r   r   r   modal.clientr   modal.configr   modal.container_processr   modal.environmentsr   modal.exceptionr   modal.stream_typer   modal_protor   Typercontainer_clirC   create_blockingstrboolr9   ArgumentOptionr:   r   r@   rR   r#   r#   r#   r$   <module>   sZ    

!"