o
    ;i^!                     @   s  d dl Z d dlmZmZ d dl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 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 edddZ ej!dddddZ"ej#ddddZ$ej%edddej&edddej'edddej(edd dej)ed!ddej*ed"d#dej+ed$d%dej,ed&d%diZ-ej.dGd'e/d(ee/ d)ee d*e/fd+d,Z0e$1d-ej.ed.fd(ee/ d/e2fd0d1Z3e$j1d2dd3e feej!d.d4d5dd6d'e/d(ee/ d7e2fd8d2Z4e$j1d9dd:did;ej.e ejdd<dfed=d'e/d>e/d(ee/ fd?d9Z5e$j1d@dd3ej.e fed=d'e/d(ee/ fdAd@Z6e$j1dBdd3ej.e fed.dCd'e/d(ee/ d/e2fdDdBZ7e$j1dEdd3ej.e fed=d'e/d(ee/ fdFdEZ8dS )H    N)OptionalUnion)
UsageError)Column)Text)Argument)_get_environment_name)synchronizer)open_url_and_display)_Client)
ensure_env)api_pb2   )timestamp_to_localized_str   )
ENV_OPTIONdisplay_tableget_app_id_from_namestream_app_logs zApp name or ID)helpz-nz--namez2Deprecated: Pass App name as a positional argumentappz!Manage deployed and running apps.T)namer   no_args_is_helpdeployedgreenstylezephemeral (detached)disableddim	ephemeralzinitializing...yellowstoppedbluezstopping...app_identifierenvclientreturnc                    s&   t d| r	| S t| ||I dH S )zAResolve an app_identifier that may be a name or an ID into an ID.z^ap-[a-zA-Z0-9]{22}$N)rematchr   aio)r$   r%   r&    r+   A/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/app.py
get_app_id'   s   r-   listFjsonc           	         s   t | } t I dH }|jtjt| dI dH }tddddddd	d
g}g }|j	D ]%}t
|jtddd}||j|j|t|jt|j|t|j|g q,| rZd|  dnd}t|||d| d dS )zHList Modal apps that are currently deployed/running or recently stopped.N)environment_namezApp ID   )	min_widthDescriptionStateTasksz
Created atz
Stopped atunknowngrayr   z in environment ''r   Apps)title)r   r   from_envstubAppListr   AppListRequestr   r   appsAPP_STATE_TO_MESSAGEgetstater   appendapp_iddescriptionstrn_running_tasksr   
created_at
stopped_atr   )	r%   r/   r&   respcolumnsrows	app_statsrB   env_partr+   r+   r,   list_/   s6   




rO   logs)r   z--timestampsz!Show timestamps for each log line)r%   
timestampsrQ   c                C   s   t | |}t||d dS )a   Show App logs, streaming while active.

    **Examples:**

    Get the logs based on an app ID:

    ```
    modal app logs ap-123456
    ```

    Get the logs for a currently deployed App based on its name:

    ```
    modal app logs my-app
    ```

    )show_timestampsN)r-   r   )r$   r%   rQ   rD   r+   r+   r,   rP   T   s   
rollbackignore_unknown_options)r   context_settingszTarget version for rollback.)r%   versionc                   s   t |}t I dH }t| ||I dH }|sd}ntd| }r+t|d}nt	d| t
j||d}|j|I dH  td dS )aW  Redeploy a previous version of an App.

    Note that the App must currently be in a "deployed" state.
    Rollbacks will appear as a new deployment in the App history, although
    the App state will be reset to the state at the time of the previous deployment.

    **Examples:**

    Rollback an App to its previous version:

    ```
    modal app rollback my-app
    ```

    Rollback an App to a specific version:

    ```
    modal app rollback my-app v3
    ```

    Rollback an App using its App ID instead of its name:

    ```
    modal app rollback ap-abcdefghABCDEFGH123456
    ```

    Nzv(\d+)r   zInvalid version specifer: )rD   rV   u2   [green]✓[/green] Deployment rollback successful!)r   r   r;   r-   r*   r(   r)   intgroupr   r   AppRollbackRequestr<   AppRollbackrichprint)r$   rV   r%   r&   rD   version_numbermreqr+   r+   r,   rS   p   s   #stopc                   sH   t  I dH }t| |I dH }tj|tjd}|j|I dH  dS )zStop an app.N)rD   source)	r   r;   r-   r*   r   AppStopRequestAPP_STOP_SOURCE_CLIr<   AppStop)r$   r%   r&   rD   r`   r+   r+   r,   ra      s
   history)r%   r/   c                   s  t |}t I dH }t| ||I dH }|jtj|dI dH }g d}g }d}t	|j
D ]j\}	}
|	dkr:dnd}td|
j |d	tt|
j||d	t|
j|d	t|
j|d	g}|
jjr}|
jjdd
 }|
jjrsd}| d}|t||d	 n|d |
jr|t|
j|d	 n|d || q0tdd |D sdd |D }|dd }t|dd dd}t||| |r|std dS dS dS )a  Show App deployment history, for a currently deployed app

    **Examples:**

    Get the history based on an app ID:

    ```
    modal app history ap-123456
    ```

    Get the history for a currently deployed App based on its name:

    ```
    modal app history my-app
    ```

    N)rD   )VersionzTime deployedClientzDeployed byCommitTagFr   z
bold greenr   vr      T*c                 s   s    | ]}|d  V  qdS )rW   Nr+   .0rowr+   r+   r,   	<genexpr>   s    zhistory.<locals>.<genexpr>c                 S   s   g | ]}|d d qS )NrW   r+   rn   r+   r+   r,   
<listcomp>       zhistory.<locals>.<listcomp>rW   c                 S   s   t t| d dd  S )Nr   r   )rX   rF   )xr+   r+   r,   <lambda>   rs   zhistory.<locals>.<lambda>)keyreversez * - repo had uncommitted changes)r   r   r;   r-   r*   r<   AppDeploymentHistoryr   AppDeploymentHistoryRequest	enumerateapp_deployment_historiesr   rV   r   deployed_atclient_versiondeployed_bycommit_infocommit_hashdirtyrC   taganysortedr   r\   r]   )r$   r%   r/   r&   rD   rJ   rK   rL   deployments_with_dirty_commitidxrM   r   rp   
short_hashr+   r+   r,   rf      sD   


	dashboardc                   s<   t  I dH }t| ||I dH }d| }t|d dS )zOpen an App's dashboard page in your web browser.

    **Examples:**

    Open dashboard for an app by name:

    ```
    modal app dashboard my-app
    ```

    Use a specified environment:

    ```
    modal app dashboard my-app --env dev
    ```
    Nzhttps://modal.com/id/zApp dashboard)r   r;   r-   r*   r
   )r$   r%   r&   rD   urlr+   r+   r,   r      s
   
)N)9r(   typingr   r   r\   typerclickr   
rich.tabler   	rich.textr   r   modal._objectr   modal._utils.async_utilsr	   modal._utils.browser_utilsr
   modal.clientr   modal.environmentsr   modal_protor   _utils.time_utilsr   utilsr   r   r   r   APP_IDENTIFIEROptionNAME_OPTIONTyperapp_cliAPP_STATE_DEPLOYEDAPP_STATE_DETACHEDAPP_STATE_DETACHED_DISCONNECTEDAPP_STATE_DISABLEDAPP_STATE_EPHEMERALAPP_STATE_INITIALIZINGAPP_STATE_STOPPEDAPP_STATE_STOPPINGr@   create_blockingrF   r-   commandboolrO   rP   rS   ra   rf   r   r+   r+   r+   r,   <module>   s   & #0L