
    uj                     t    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 Z G d d	e          ZdS )
    N)ArgumentParser)Path)
CLICommand)TEMPORARY_FOLDER_NAME)get_model_masked_directoryc                      t          |           S )z> Function which will be called for a specific sub parser.
    )ClearCacheCMD)argss    i/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/cli/clearcache.pysubparser_funcr      s         c                   F    e Zd ZdZd Zedefd            Zd Zd Z	d Z
dS )	r	   zclear-cachec                     || _         t          j        dt          j                                        dd                    | _        d S )NMODELSCOPE_CACHEz.cache
modelscope)r
   osgetenvr   homejoinpath	cache_dir)selfr
   s     r   __init__zClearCacheCMD.__init__   s<    	IKK  <88: :r   parsersc                    |                      t          j                  }|                                }|                    dt
          d           |                    dt
          d           |                    t                     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groups      r   define_argszClearCacheCMD.define_args   s     ##M$6773355X	 	 	
 	
 	
 	X	 	 	
 	
 	
 	00000r   c                 .    |                                   d S )N)_execute_with_confirmation)r   s    r   executezClearCacheCMD.execute2   s    '')))))r   c                 P   d}d}d}| j         j        s| j         j        r:| j         j        r| j         j        }d}|d| dz   }n&| j         j        }|d| dz   }n|d| j         dz   }d}t	          |d	z                                                                             }|d
k    r|r+|                     | j                   t          d           d S t          j
                            | j        |rdnd|          }t          j
                            | j        |rdndt          |          }|                     |          }|                     |          }	|s.|	s,|rt          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.hubdatasetszCache for Model z not found. Nothing to do.zCache for Dataset zOperation aborted.)r
   modeldatasetr   inputstripupper_remove_directoryprintr   pathjoinr   )
r   allsingle_modelpromptid
user_inputentity_directorytemp_directoryentity_removedtemp_removeds
             r   r(   z(ClearCacheCMD._execute_with_confirmation5   s&   -9? 
	di/ 
	y DY_#"A2"A"A"AAY&"CR"C"C"CCqDNqqqqFCKL
 
 %''%%'' 	
  ,&&t~666&'''''#%7<<N\$IEEz2$O $O !#N\$IEEz)2"/ "/ "&!7!78H!I!I#55nEE& 	, 	,# MrMMMO O O O O OOOO     *+++++&'''Fr   c                    t           j                            |          r	 t           j                            |          rNt	          j        t          j        |                     t          j        |           t          d| d           n't	          j        |           t          d| d           dS # t          $ r}t          d| d|            Y d }~nd }~ww xY wdS d S )NzCache and link for z	 removed.zCache folder Tz*An error occurred while clearing cache at z: F)
r   r5   existsislinkshutilrmtreereadlinkremover4   	Exception)r   r5   es      r   r3   zClearCacheCMD._remove_directoryd   s   7>>$ 	
P7>>$'' ;M"+d"3"3444IdOOO????@@@@M$'''9$999:::t P P PN4NN1NNOOOOOOOOP5	 	s   BB7 7
C CC N)__name__
__module____qualname__r   r   staticmethodr   r&   r)   r(   r3    r   r   r	   r	      s}        D: : : 1^ 1 1 1 \1,* * *- - -^    r   r	   )r   rC   argparser   pathlibr   modelscope.cli.baser   modelscope.hub.constantsr   modelscope.hub.utils.utilsr   r   r	   rM   r   r   <module>rS      s    				  # # # # # #       * * * * * * : : : : : : A A A A A A  _ _ _ _ _J _ _ _ _ _r   