
    j                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlZd dl	Z	d dl
mZ d dlZ ej         ej        ej                            d          d                    Zej                            de          Z ej        ed          Z ej        ed          Z ej        ed          Zd	 Zd
ededdfdZd Zd ZddZ G d d          Z e            ZdS )    N)Path~z.paddlexPADDLE_PDX_CACHE_HOMEfunc_retlockstempc                  r    t          j        t          d           t          j        t          d           dS )zFEnsure cache dirs exist. Safe for concurrent creation (exist_ok=True).Texist_okN)osmakedirsFUNC_CACHE_DIRFILE_LOCK_DIR     c/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddlex/utils/cache.py_ensure_persist_dirsr   #   s2    K....K------r   	dest_path	cache_dirreturnc                 6   t          j        dd|dd          5 }|j        }t          j        | |           ddd           n# 1 swxY w Y   	 t          j        ||           dS # t          $ r( 	 t          j        |           n# t          $ r Y nw xY w w xY w)zIWrite pickle atomically (temp file + os.replace) under an exclusive lock.wbFpersist_z.tmp)modedeletedirprefixsuffixN)	tempfileNamedTemporaryFilenamepickledumpr   replaceOSErrorunlink)objr   r   tftmp_paths        r   _atomic_write_pickler*   )   s
   		$
 
 
  
7C              

8Y'''''   	Ih 	 	 	D	sA   AA
AA& &
B1BB
BBBBc                      t          j        t          d           t          j        t          d           t          j        t          d           dS )zcreate cache dirTr
   N)r   r   	CACHE_DIRr   r   argskwargss     r   create_cache_dirr0   >   sI     K	D))))K....K------r   c                      t           S )zget cache dir)r,   r-   s     r   get_cache_dirr2   G   s
     r   c                 f     dt           t          t          f d  d fd fd}|S )persistzpersist_{key}.pklNc                 
    | d uS Nr   )rets    r   <lambda>zpersist.<locals>.<lambda>V   s
    3d? r   c                 F    t          |                               d          S )Nzutf-8)strencode)r'   s    r   	_to_byteszpersist.<locals>._to_bytesX   s    3xxw'''r   c           
      Z   t          j                    }|                     t          j        t          j        |                                          |                     | j                             |j        	                                D ]\  }}t          |          s2t          t          |           d| dt          |           d          |                     |                     |                     |                     |                                }|S )Nz: z, z' is unhashable or not a supported type.)hashlibmd5updateosprealpathinspectgetsourcefile__name__	argumentsitems
isinstance	TypeErrorreprtype	hexdigest)funcbnd_argsmkvhash_SUPPORTED_ARG_TYPESr<   s         r   	_make_keyzpersist.<locals>._make_key[   s    KMM	3<(=d(C(CDDEEFFF	4=))***&,,.. 	# 	#DAqa!455 AwwWW!WWtAwwWWW   HHYYq\\"""HHYYq\\""""r   c                 N     t          j                    fd            }|S )Nc                  P   t                       t          j                  } |j        | i |}|                                  |          }t          j        t          
                    t          |                              }t          j        t          j        t          | d                    }|5  d }d}t          j        |          r	 t          |d          5 }	t          j        |	          }d d d            n# 1 swxY w Y   d}nN# t          j        t$          t&          t(          f$ r) 	 t+          j        |           n# t(          $ r Y nw xY wY nw xY w|s) | i |} |          rt/          ||t                     d d d            n# 1 swxY w Y   |S )N)keyz.lockFrbT)r   rC   	signaturebindapply_defaultsrA   joinr   formatr:   filelockFileLockr   existsopenr"   loadUnpicklingErrorEOFError
ValueErrorr%   r   remover*   )r.   r/   sigrN   rW   cache_file_pathlockr7   loadedfFILENAME_PATTERNrT   condrM   s             r   _wrapperz(persist.<locals>._deco.<locals>._wrapperm   sW    """#D))Csx000H##%%%)D(++C!h 0 7 7CHH 7 E E O $SXm]]]%K%KLLD S S:o.. 	!!!/488 1A"(+a..C1 1 1 1 1 1 1 1 1 1 1 1 1 1 1!%"2Hj'R ! ! !!Io6666& ! ! ! D!!
  S$///CtCyy S,S/>RRR!S S S S S S S S S S S S S S S" Js~   =FD'D<DD	DD	DF"E!9EE!
EE!EE!F E!!.FF"F)	functoolswraps)rM   rn   rl   rT   rm   s   ` r   _decozpersist.<locals>._decol   sH    				 	 	 	 	 	 	 
		: r   )r:   intfloat)rm   rq   rl   rS   rT   r<   s   ` @@@@r   r4   r4   M   s    
 +U+|**( ( (     "      B Lr   c                   @    e Zd Zd Zd Zd Z G d d          Zd ZdS )TempFileManagerc                     g | _         t          t                                        dd           t	          j        | j                   d S )NT)parentsr   )
temp_filesr   TEMP_DIRmkdiratexitregistercleanupselfs    r   __init__zTempFileManager.__init__   s@    XTD999%%%%%r   c                 l    t          j        ddt          d|}| j                            |           |S )NF)r   r   r   )r   r    ry   rx   append)r   r/   	temp_files      r   create_temp_filez TempFileManager.create_temp_file   s;    /Uu(UUfUU	y)))r   c                     | j         D ]@}	 |                                 t          j        |j                   1# t
          $ r Y =w xY wg | _         d S r6   )rx   closer   rf   r!   FileNotFoundError)r   r   s     r   r}   zTempFileManager.cleanup   sh     	 	I!!!	).))))$   s   -9
AAc                        e Zd Zd Zd Zd ZdS )&TempFileManager.TempFileContextManagerc                 0    || _         || _        d | _        d S r6   )managerr/   r   )r   r   r/   s      r   r   z/TempFileManager.TempFileContextManager.__init__   s    "DL DK!DNNNr   c                 H     | j         j        di | j        | _        | j        S )Nr   )r   r   r/   r   r~   s    r   	__enter__z0TempFileManager.TempFileContextManager.__enter__   s(    :T\:IIT[IIDN>!r   c                 J    | j         r| j                                          d S d S r6   )r   r   )r   exc_type	exc_value	tracebacks       r   __exit__z/TempFileManager.TempFileContextManager.__exit__   s0    ~ '$$&&&&&' 'r   N)rE   
__module____qualname__r   r   r   r   r   r   TempFileContextManagerr      sA        	" 	" 	"
	" 	" 	"	' 	' 	' 	' 	'r   r   c                      | j         | fi |S r6   )r   )r   r/   s     r   temp_file_contextz!TempFileManager.temp_file_context   s    *t*4::6:::r   N)rE   r   r   r   r   r}   r   r   r   r   r   ru   ru      sx        & & &
  
  ' ' ' ' ' ' ' '; ; ; ; ;r   ru   r6   )r{   ro   r>   rC   r   os.pathpathrA   r"   r   pathlibr   r^   abspathr\   
expanduserDEFAULT_CACHE_DIRenvirongetr,   r   r   ry   r   r:   r*   r0   r2   r4   ru   temp_file_managerr   r   r   <module>r      s           				               CK););C)@)@* M MNN JNN24EFF	)Z00G,,38Iv&&. . .      *. . .  @ @ @ @F#; #; #; #; #; #; #; #;L $O%%   r   