o
    ;i                     @   s  d dl Z d dlZd dlmZ d dl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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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ddddZ&e&j'ddddej(edfdee) dee* fddZ+dd  Z,e&j'd!d"ddefd#e)dee) fd$d!Z-e&j'd%d&d'dej(ejd(d)efd*e)d+e)dee) fd,d%Z.e&j'd-d.d'dej(ejd(d)efd*e)d/e)d0e)dee) fd1d-Z/G d2d3 d3e0Z1e&j'd4d'd5ej(ed6defd*e)d0e)d7e)d8e*dee) f
d9d4Z2e&j'd:d;d'dej(ej3dd<d=d>d?efd*e)d0e)d@e*dee) fdAd:Z4e&j'dBdCddej(edDd?eefdEe)dFe*dee) fdGdBZ5dS )H    N)Path)Optional)
UsageError)Syntax)Table)ArgumentTyper)display_location)synchronizer)timestamp_to_localized_str)_volume_download)
ENV_OPTION
YES_OPTIONdisplay_table)_Client)
ensure_env)_NetworkFileSystem)OutputManager)api_pb2nfsz5Read and edit `modal.NetworkFileSystem` file systems.T)namehelpno_args_is_helplistz+List the names of all network file systems.
Management)r   r   rich_help_panelFenvjsonc                    s   t | } t I d H }|jtj| dI d H }| r!d|  dnd}g d}g }|jD ]}||j	t
|jt|j|g q,t|||d| d d S )Nenvironment_namez in environment '' )NameLocationz
Created atzShared Volumes)title)r   r   from_envstubSharedVolumeListr   SharedVolumeListRequestitemsappendlabelr	   cloud_providerr   
created_atr   )r   r   clientresponseenv_partcolumn_namesrowsitem r4   Q/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/network_file_system.pylist_   s   

r6   c                 C   s   d|  dS )NzS
@app.function(network_file_systems={"/my_vol": modal.NetworkFileSystem.from_name("z0")})
def some_func():
    os.listdir("/my_vol")
r4   )r+   r4   r4   r5   gen_usage_code1   s   r7   createz#Create a named network file system.r   c                 C   sN   t | tjj| |d t }|d|  d tt| d}|| d S )Nr   zCreated volume 'z'. 

Code example:
python)	r   modalNetworkFileSystemcreate_deployedr   getprintr   r7   )r   r   outputusager4   r4   r5   r8   9   s   lsz4List files and directories in a network file system.zFile operations/)defaultvolume_namepathc           	         s   t | t| }||I d H }tj rSt }|	d| d|  d t
 }|d |d |D ]}|jtjjjkrBdnd}||j| q6|	| d S |D ]}t	|j qUd S )NzDirectory listing of 'z' in 'r    filenametypedirfile)r   r   	from_namelistdirsysstdoutisattyr   r=   r>   r   
add_columnrG   r   	FileEntryFileType	DIRECTORYadd_rowrE   )	rD   rE   r   volumeentriesr?   tableentryfiletyper4   r4   r5   rA   F   s"   



puta  Upload a file or directory to a network file system.

Remote parent directories will be created as needed.

Ending the REMOTE_PATH with a forward slash (/), it's assumed to be a directory and the file
will be uploaded with its current name under that directory.

local_pathremote_pathc              	      s,  t | t| }|dr|tj| }t }t	|
 rV|d}|j|||jdI d H  |jdd W d    n1 sCw   Y  |d| d| d d S d	|v r^td
|d}|j|||jdI d H }|jdd W d    n1 sw   Y  |d| d| d| d d S )NrB   uploadprogress_cbT)completezUploaded directory 'z' to 'r    *z(Glob uploads are currently not supportedzUploaded file 'z' (z bytes written))r   r   rJ   endswithosrE   basenamer   r=   r   is_dirtransfer_progressadd_local_dirprogressstep_completedr   add_local_file)rD   rZ   r[   r   rT   r?   rg   written_bytesr4   r4   r5   rY   f   s&   

"c                   @   s   e Zd Zdd ZdS )CliErrorc                 C   s
   || _ d S )N)message)selfrl   r4   r4   r5   __init__   s   
zCliError.__init__N)__name__
__module____qualname__rn   r4   r4   r4   r5   rk      s    rk   r=   )r   r   .local_destinationforcec           	         sv   t | t|}t| }t }|d}t|||||jdI dH  W d   n1 s/w   Y  |	d dS )a  Download a file from a network file system.

    Specifying a glob pattern (using any `*` or `**` patterns) as the `remote_path` will download
    all matching files, preserving their directory structure.

    For example, to download an entire network file system into `dump_volume`:

    ```
    modal nfs get <volume-name> "**" dump_volume
    ```

    Use "-" as LOCAL_DESTINATION to write file contents to standard output.
    downloadr]   Nz$Finished downloading files to local!)
r   r   r   rJ   r   r=   re   r   rg   rh   )	rD   r[   rs   rt   r   destinationrT   r?   rg   r4   r4   r5   r=      s   
rmz6Delete a file or directory from a network file system.z-rz--recursivezDelete directory recursively)r   	recursivec                    s@   t | t| }|j||dI d H  t | d d S )N)rx   z was deleted successfully!)r   r   rJ   remove_filer   r=   rh   )rD   r[   rx   r   rT   r4   r4   r5   rw      s
   

deletez3Delete a named, persistent modal.NetworkFileSystem.zAName of the modal.NetworkFileSystem to be deleted. Case sensitivenfs_nameyesc                    sN   t j| |d I d H  |stjd|  dddd t j| |dI d H  d S )Nr   zIAre you sure you want to irrevocably delete the modal.NetworkFileSystem 'z'?FT)rC   abort)r   rJ   hydratetyperconfirmrz   )r{   r|   r   r4   r4   r5   rz      s   
)6rb   rL   pathlibr   typingr   r   clickr   rich.syntaxr   
rich.tabler   r   r   r:   modal._locationr	   modal._utils.async_utilsr
   modal._utils.time_utilsr   modal.cli._downloadr   modal.cli.utilsr   r   r   modal.clientr   modal.environmentsr   modal.network_file_systemr   modal.outputr   modal_protor   nfs_clicommandcreate_blockingstrboolr6   r7   r8   rA   rY   	Exceptionrk   r=   Optionrw   rz   r4   r4   r4   r5   <module>   s   $

