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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 m!Z! d dl"m#Z# eddddZ$de%de&fddZ'e$j(ddddeedddfde&dee& d ee% fd!dZ)e$j(d"d#d$ej*ed%d&efd'e&d(e&d)e&d*e+dee& f
d+d"Z,e$j(d,d-ddej*ed&fdee& d.e+fd/d0Z-e$j(d1d2d#dej*ed3d4d&efd'e&d5e&d.e+dee& fd6d1Z.e$j(d7d8d#dej*e ed3d4ed&d9d:d;d<efd'e&d=e&d(e&d*e+dee& f
d>d7Z/e$j(d?d@d#dej*ed&dAdBdCd<efd'e&d(e&dDe+dee& fdEd?Z0e$j(dFdGd#dej*ed&dAdBdHd<efd'e&dIe1e& dDe+dee& fdJdFZ2e$j(dKdLddej*edMd<fed&dNdOd<eedPde&dQe+dRe+dee& fdSdKZ3e$j(dTdUddej*eefdVe&dWe&dRe+dee& fdXdTZ4e$j(dYdZddej*ed[d<efd'e&dee& fd\dYZ5dS )]    N)Path)Optional)
UsageError)Syntax)ArgumentOptionTyper)synchronizer)open_url_and_display)timestamp_to_localized_str)_volume_download)
ENV_OPTION
YES_OPTIONdisplay_table)
ensure_env)OutputManager)#_AbstractVolumeUploadContextManager_Volume)api_pb2volumeTz
    Read and edit `modal.Volume` volumes.

    Note: users of `modal.NetworkFileSystem` should use the `modal nfs` command instead.
    )nameno_args_is_helphelpvaluereturnc                 C   sv   | dk rt dd}d}d}t| }||k r|ddS t|D ]\}}||d  }||k r0 nq ||| |  | S )	Nr   zvalue should be >= 0)z KiBz MiBz GiBz TiBz PiBz EiBz ZiBz%.1fi   z0.0fz B   )
ValueErrorfloat	enumerate)r   suffixformatbasebytes_isunit r&   D/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/volume.pyhumanize_filesize"   s   r(   createz(Create a named, persistent modal.Volume.
Management)r   r   rich_help_panelz VolumeFS version. (Experimental))defaultr   r   envversionc                 C   s`   t |}tjjj| ||d d|  d}t }|d|  d| d t|d}|| d S )N)environment_namer.   z;
@app.function(volumes={"/my_vol": modal.Volume.from_name("z0")})
def some_func():
    os.listdir("/my_vol")
zCreated Volume 'z' in environment 'z'. 

Code example:
python)	r   modalVolumeobjectsr)   r   getprintr   )r   r-   r.   env_name
usage_codeoutputusager&   r&   r'   r)   2   s   
r4   zFile operations)r   r+   .Fvolume_nameremote_pathlocal_destinationforcec           	         sz   t | t|}tj| |d}t }|d}t|||||jdI dH  W d   n1 s1w   Y  |	d dS )a  Download files from a modal.Volume object.

    If a folder is passed for REMOTE_PATH, the contents of the folder will be downloaded
    recursively, including all subdirectories.

    **Example**

    ```
    modal volume get <volume_name> logs/april-12-1.txt
    modal volume get <volume_name> / volume_data_dump
    ```

    Use "-" as LOCAL_DESTINATION to write file contents to standard output.
    r/   download)r   r<   r=   	overwriteprogress_cbNz$Finished downloading files to local!)
r   r   r   	from_namer   r4   transfer_progressr   progressstep_completed)	r;   r<   r=   r>   r-   destinationr   r8   rE   r&   r&   r'   r4   F   s   listz?List the details of all modal.Volume volumes in an Environment.jsonc                    sn   t | } tjj| dI d H }g }|D ]}| I d H }||jt|j	 ||j
f qtg d|| d S )Nr?   )Namez
Created atz
Created by)r   r   r3   rH   infoappendr   r   
created_at	timestamp
created_byr   )r-   rI   volumesrowsobjrK   r&   r&   r'   list_l   s   "rS   lsz4List files and directories in a modal.Volume volume./)r,   pathc                    s  t | tj| |d}||I d H }|s'tj s'|D ]}t|j qd S g }|D ]G}|j	t
jjjkr8d}n#|j	t
jjjkrCd}n|j	t
jjjkrNd}n|j	t
jjjkrYd}nd}||jdd|t|jd	t|jf q+g d
}	d| d|  d}
t|	|||
d d S )Nr?   dirlinkfifosocketfileunicode_escapezutf-8F)FilenameTypezCreated/ModifiedSizezDirectory listing of 'z' in '')title)r   r   rC   listdirsysstdoutisattyr5   rV   typer   	FileEntryFileType	DIRECTORYSYMLINKFIFOSOCKETrL   encodedecoder   mtimer(   sizer   )r;   rV   rI   r-   volentriesentryrQ   filetypecolumnsra   r&   r&   r'   rT   }   s:   
putzUpload a file or directory to a modal.Volume.

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.
z-fz--forcezOverwrite existing files.)r   
local_pathc           
         s  t | tj| |d I d H }|dr|tj| }t	 }t
| r|dL}z1tj|jj|j|j|j|d4 I d H }||| W d   I d H  n1 I d H sYw   Y  W n tyq }	 ztt|	d }	~	ww W d    n1 s|w   Y  |d| d| d d S d|v rtd	|dL}z1tj|jj|j|j|j|d4 I d H }||| W d   I d H  n1 I d H sw   Y  W n ty }	 ztt|	d }	~	ww W d    n1 sw   Y  |d
| d| d d S )Nr?   rU   upload)rB   r>   zUploaded directory 'z' to 'r`   *z(Glob uploads are currently not supportedzUploaded file ')r   r   rC   hydrateendswithosrV   basenamer   r4   r   is_dirrD   r   resolve	_metadatar.   	object_id_clientrE   put_directoryFileExistsErrorr   strrF   put_file)
r;   rw   r<   r>   r-   rq   r8   rE   batchexcr&   r&   r'   rv      sb   
((	rmz/Delete a file or directory from a modal.Volume.z-rz--recursivezDelete directory recursively	recursivec                    sD   t | tj| |d}|j||dI d H  t | d d S )Nr?   )r   z was deleted successfully!)r   r   rC   remove_filer   r4   rF   )r;   r<   r   r-   r   r&   r&   r'   r      s
   
cpzsCopy within a modal.Volume. Copy source file to destination file or multiple source files to destination directory.zCopy directories recursivelypathsc                    s:   t | tj| |d}|^ }}||||I d H  d S )Nr?   )r   r   rC   
copy_files)r;   r   r   r-   r   	src_pathsdst_pathr&   r&   r'   r      s
   
deletez*Delete a named Volume and all of its data.z6Name of the modal.Volume to be deleted. Case sensitivez--allow-missingz(Don't error if the Volume doesn't exist.)allow_missingyesr-   r   r   c                   sB   t |}|stjd|  dddd tjj| ||dI d H  d S )Nz>Are you sure you want to irrevocably delete the modal.Volume 'z'?FTr,   abort)r/   r   )r   typerconfirmr   r3   r   )r   r   r   r-   r&   r&   r'   r     s   
renamezRename a modal.Volume.old_namenew_namec                    s8   |st jd|  dddd tj| ||dI d H  d S )Nz/Are you sure you want rename the modal.Volume 'z.'? This may break any Apps currently using it.FTr   r?   )r   r   r   r   )r   r   r   r-   r&   r&   r'   r   #  s   
	dashboardz5Open the Volume's dashboard page in your web browser.z,Name of the Volume to view in the dashboard.c                    s<   t |}tj| |d I dH }d|j }t|d dS )zOpen a Volume's dashboard page in your web browser.

    **Example:**

    ```
    modal volume dashboard my-volume
    ```
    r?   Nzhttps://modal.com/id/zVolume dashboard)r   r   rC   rz   r   r
   )r;   r-   r   urlr&   r&   r'   r   9  s
   )6r|   rc   pathlibr   typingr   r   clickr   rich.syntaxr   r   r   r   r1   modal._utils.async_utilsr	   modal._utils.browser_utilsr
   modal._utils.time_utilsr   modal.cli._downloadr   modal.cli.utilsr   r   r   modal.environmentsr   modal.outputr   modal.volumer   r   modal_protor   
volume_cliintr   r(   commandr)   create_blockingboolr4   rS   rT   rv   r   rH   r   r   r   r   r&   r&   r&   r'   <module>   sd  
$ (/