o
    ;iE1                     @   s  d dl Z d dlZd dlZd dlmZmZ d dlmZ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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) ddl*m+Z+ ddl,m-Z-m.Z. dedef de/e0e
j1j2f fddZ3de/e0e
j1j2f de/e0ef dee0ge4f de5e0 fddZ6d e0d!e0de4fd"d#Z7d e	e0 de4fd$d%Z8d e0d&e0d'e4ddfd(d)Z9d e0d*e4defd+d,Z:d-ed.e5e0 d/e0d0e;d1ed'e4ddfd2d3Z<d-ed.e5e0 d/e0d0e;d4e	e d5e5e0 d6e5e0 d7e5e0 d8e	e; d9e	e; d:e	e0 d;e	e0 d<e	e0 d'e4ddfd=d>Z=e
j>dd?d@e
j?dAdBdCdDdEe-e
j?ddFdGdEe
j?ddHdIdEe
j?ddJdKdEe
j?ddLdMdEe
j?ddNdOdEe
j?ddPdQdEe
j?ddRdSdEe
j?ddTdUdEe
j?ddVdWdEe
j?ddXdYdEe
j?ddZd[dEe
j?d\d]d^dEfd e	e0 d&e0d/e	e0 d_e	e0 d`e	e0 d5e	e5e0  d7e	e5e0  d6e	e5e0  d8e	e; d9e	e; d:e	e0 d;e	e0 d<e	e0 d'e	e4 d*e4fdadbZ@dS )c    N)PathPurePosixPath)AnyCallableOptional)ClickException   )_FunctionSpec)App)
ensure_env)InvalidErrorNotFoundError)Function)Image)_Mount)interactive_shell)Sandbox)Secret)Volume   )exec)MethodReferenceimport_and_filterparse_import_ref)_get_runnable_list)
ENV_OPTIONis_ttyfunc.returnc                 C   sJ   t | }dd |j D }tdd | D s#J d| j d|S )Nc                 S   s   i | ]\}}||j qS  )default).0
param_nameparamr   r   C/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/shell.py
<dictcomp>$   s    z*_params_from_signature.<locals>.<dictcomp>c                 s   s    | ]
}t |tjjV  qd S )N)
isinstancetypermodelsParameterInfo)r!   r#   r   r   r$   	<genexpr>%   s    z)_params_from_signature.<locals>.<genexpr>zAll params to z, must be of type typer.models.ParameterInfo.)inspect	signature
parametersitemsallvalues__name__)r   sigparamsr   r   r$   _params_from_signature    s   
r4   
param_objspassed_argsallowedc                 C   sX   g }|   D ]#\}}||rq|jdusJ d|||jkr)|d|j q|S )zDCheck which forbidden arguments were passed with non-default values.NzDAll params must be typer.models.ParameterInfo, and have param_decls./)r.   param_declsgetr    appendjoin)r5   r6   r7   passed_forbiddenr"   	param_objr   r   r$   _passed_forbidden_args+   s   r?   refprefixc                 C   sD   | dsJ | |o!t| t|d  dko!| t|d   S )N-r   )endswith
startswithlenisalnum)r@   rA   r   r   r$   _is_valid_modal_id>   s   6rG   c                 C   s    | d u rdS t | dpt | dS )NFsb-ta-)rG   )r@   r   r   r$   _is_running_container_refC   s   rJ   cmdptyc              
   C   s   t | dr<zt| }| } W n+ ty$ } ztd|  dd }~w ty; } ztd|  dt| d }~ww t | dsCJ zt| t	
||d W d S  tyd } ztd|  dd }~w ty{ } ztd	|  dt| d }~ww )
NrH   z	Sandbox 'z"' not found (is it still running?)zError connecting to Sandbox 'z': rI   )container_idcommandrL   zContainer 'zError connecting to container ')rG   r   from_id_get_task_idr   r   	Exceptionstrr   shlexsplit)r@   rK   rL   sandboxer   r   r$   !_start_shell_in_running_containerI   s*   

rW   use_module_modec                 C   s   t | |d}t|dddd\}}|s9d|j d}|r)d|j d	}|t|7 }nd|j d
}t| d| t|trF|j }|j	S t|t
rN|j	S td)N)rX   modal shellFT)base_cmdaccept_local_entrypointaccept_webhookzJSpecify a Modal function to start a shell session for. E.g.
> modal shell z::my_functionzThe selected module 'z' has the following choices:

z$' has no Modal functions or classes.z

z0Referenced entity is not a Modal Function or Cls)r   r   file_or_moduler   r   r&   r   cls_get_class_service_functionspecr   
ValueError)r@   rX   
import_refrunnableall_usable_commandshelp_headerhelp_footerclass_service_functionr   r   r$   _function_spec_from_ref\   s(   



rh   appcmdsenvtimeoutfunction_specc                 C   sR   t | f||||j|j|j|j|j|j|j|j|j	|j
r|j
jnd ||jd d S )N)rj   environment_namerl   imagemountssecretsnetwork_file_systemsgpucloudcpumemoryvolumesregionrL   proxy)r   ro   rp   rq   rr   gpusrt   ru   rv   rw   scheduler_placementregionsry   )ri   rj   rk   rl   rm   rL   r   r   r$   _start_shell_from_function_specy   s&   r}   modal_imagevolumesecret	add_localru   rv   rs   rt   rx   c                 C   s   dd |D }dd |D }g }|D ]*}t |  }td|j }| r0tj||d}ntj||d}|	| qt
| |||||||	|
||||rQ|dng |d d S )	Nc                 S   s   i | ]}d | t |qS )/mnt/)r   	from_name)r!   volr   r   r$   r%      s    z+_start_shell_from_image.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r   )r   r   )r!   sr   r   r$   
<listcomp>   s    z+_start_shell_from_image.<locals>.<listcomp>r   )remote_path,)rj   rn   rl   ro   rp   ru   rv   rs   rt   rw   rq   rx   rL   )r   
expanduserresolver   nameis_dirr   _from_local_dir_from_local_filer;   r   rT   )ri   rj   rk   rl   r~   r   r   r   ru   rv   rs   rt   rx   rL   rw   rq   rp   local_path_str
local_pathr   mr   r   r$   _start_shell_from_image   s4   
r   zID of running container or Sandbox, or path to a Python file containing an App. Can also include a Function specifier, like `module.py::func`, if the file defines multiple Functions.)r    helpz	/bin/bashz-cz--cmdz&Command to run inside the Modal image.)r   z--imagez<Container image tag for inside the shell (if not using REF).z--add-pythonz+Add Python to the image (if not using REF).z--volumezsName of a `modal.Volume` to mount inside the shell at `/mnt/{name}` (if not using REF). Can be used multiple times.z--add-localzvLocal file or directory to mount inside the shell at `/mnt/{basename}` (if not using REF). Can be used multiple times.z--secretzbName of a `modal.Secret` to mount inside the shell (if not using REF). Can be used multiple times.z--cpuz;Number of CPUs to allocate to the shell (if not using REF).z--memoryz<Memory to allocate for the shell, in MiB (if not using REF).z--gpuz_GPUs to request for the shell, if any. Examples are `any`, `a10g`, `a100:4` (if not using REF).z--cloudzgCloud provider to run the shell on. Possible values are `aws`, `gcp`, `oci`, `auto` (if not using REF).z--regionzvRegion(s) to run the container on. Can be a single region or a comma-separated list to choose from (if not using REF).z--ptyzRun the command using a PTY.Fz-mzHInterpret argument as a Python module path instead of a file/script pathro   
add_pythonc                 C   s  |du rt  }t dkrtdtt}| dur?t| r?t|t dd d }r7t	dd
| d	|  d
t| || dS t|}td}td| d}d}| durt| dst|t dd d }rut	dd
| d|  d
t| |}t|||||| dS | durt| drt|t dd d }rt	dd
| d|  d
t| }n|rtj||dnd}t||||||pg |pg |pg ||	|
||| dS )a  Run a command or interactive shell inside a Modal container.

    **Examples:**

    Start an interactive shell inside the default Debian-based image:

    ```
    modal shell
    ```

    Start an interactive shell with the spec for `my_function` in your App
    (uses the same image, volumes, mounts, etc.):

    ```
    modal shell hello_world.py::my_function
    ```

    Or, if you're using a [modal.Cls](https://modal.com/docs/reference/modal.Cls)
    you can refer to a `@modal.method` directly:

    ```
    modal shell hello_world.py::MyClass.my_method
    ```

    Start a `python` shell:

    ```
    modal shell hello_world.py --cmd=python
    ```

    Run a command with your function's spec and pipe the output to a file:

    ```
    modal shell hello_world.py -c 'uv pip list' > env.txt
    ```

    Connect to a running Sandbox by ID:

    ```
    modal shell sb-abc123xyz
    ```
    NWindowsz3`modal shell` is currently not supported on Windowsc                 S      | dv S )N>   rK   rL   r@   r   pr   r   r$   <lambda><      zshell.<locals>.<lambda>)r7   z2Cannot specify container configuration arguments (z, z>) when attaching to an already running container or Sandbox ('z').rY   z/bin/bash -c ""i  zim-c                 S   r   )N>   rK   rk   rL   r@   rX   r   r   r   r   r$   r   Q  r   z<) when starting a new container from a function reference ('c                 S   s   | dvS )N>   ro   r   r   r   r   r   r$   r   ^  r   zCannot specify zC argument(s) when starting a new container from a Modal Image ID (')r   )r   platformsystemr   r4   shellrJ   r?   localsr   r<   rW   r   r
   rS   rT   rG   rh   r}   r   rO   from_registryr   )r@   rK   rk   ro   r   r   r   r   ru   rv   rs   rt   rx   rL   rX   r5   r=   ri   rj   rl   rm   r~   r   r   r$   r      sv   l



r   )Ar+   r   rS   pathlibr   r   typingr   r   r   r'   clickr   
_functionsr	   ri   r
   environmentsr   	exceptionr   r   	functionsr   ro   r   mountr   runnerr   rU   r   r   r   r   r   	containerr   import_refsr   r   r   runr   utilsr   r   dictrR   r(   r)   r4   boollistr?   rG   rJ   rW   rh   intr}   r   ArgumentOptionr   r   r   r   r$   <module>   sN  




	

1	



#&).5=>