o
    rqi                     @   sh   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 ZG d	d
 d
eZdS )    N)ArgumentParser)Path)
CLICommand)TEMPORARY_FOLDER_NAME)get_model_masked_directoryc                 C   s   t | S )z> Function which will be called for a specific sub parser.
    )ClearCacheCMD)args r	   `/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/modelscope/cli/clearcache.pysubparser_func   s   r   c                   @   sB   e Zd ZdZdd ZedefddZdd Zd	d
 Z	dd Z
dS )r   zclear-cachec                 C   s$   || _ tdt dd| _d S )NZMODELSCOPE_CACHEz.cacheZ
modelscope)r   osgetenvr   homejoinpath	cache_dir)selfr   r	   r	   r
   __init__   s
   
zClearCacheCMD.__init__parsersc                 C   sD   |  tj}| }|jdtdd |jdtdd |jtd dS )z. define args for clear-cache command.
        z--modelzThe id of the model whose cache will be cleared. For clear-cache, if neither model or dataset id is provided, entire cache will be cleared.)typehelpz	--datasetzThe id of the dataset whose cache will be cleared. For clear-cache, if neither model or dataset id is provided, entire cache will be cleared.)funcN)
add_parserr   nameadd_mutually_exclusive_groupadd_argumentstrset_defaultsr   )r   parsergroupr	   r	   r
   define_args   s   zClearCacheCMD.define_argsc                 C   s   |    d S )N)_execute_with_confirmation)r   r	   r	   r
   execute2   s   zClearCacheCMD.executec           
      C   s@  d}d}d}| j js| j jr.| j jr!| j j}d}|d| d }n| j j}|d| d }n|d| j d }d}t|d	   }|d
kr|rU| | j td d S t	j
| j|r^dnd|}t	j
| j|rkdndt|}| |}| |}	|s|	s|rtd| d d S td| d d S td d S td d S )NFz
You are about to delete Tzlocal cache for model z. zlocal cache for dataset zentire ModelScope cache at z$, including ALL models and dataset.
z9
Please press Y or y to proceed, any other key to abort.
YzCache cleared.ZhubZdatasetszCache for Model z not found. Nothing to do.zCache for Dataset zOperation aborted.)r   modelZdatasetr   inputstripupper_remove_directoryprintr   pathjoinr   )
r   allZsingle_modelpromptidZ
user_inputZentity_directoryZtemp_directoryZentity_removedZtemp_removedr	   r	   r
   r    5   sV   



z(ClearCacheCMD._execute_with_confirmationc              
   C   s   t j|rRz.t j|r%tt | t | td| d W dS t| td| d W dS  t	yQ } ztd| d|  W Y d }~dS d }~ww d S )NzCache and link for z	 removed.zCache folder Tz*An error occurred while clearing cache at z: F)
r   r)   existsislinkshutilrmtreereadlinkremover(   	Exception)r   r)   er	   r	   r
   r'   d   s    

zClearCacheCMD._remove_directoryN)__name__
__module____qualname__r   r   staticmethodr   r   r!   r    r'   r	   r	   r	   r
   r      s    /r   )r   r0   argparser   pathlibr   Zmodelscope.cli.baser   Zmodelscope.hub.constantsr   Zmodelscope.hub.utils.utilsr   r   r   r	   r	   r	   r
   <module>   s   