
    nVh(                         d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZmZ d dlZd dlZd Zd Zd Zd Z G d	 d
e      Z G d de      Z G d d      Z G d de      Z G d de      Zeadad ZdefdZdefdZdefdZd Zy)    N)ABCabstractmethod)Path)DictListOptionalc                  R    t        j                  dt        j                               S )NTRITON_HOME)osgetenvr   home     D/home/dcms/DCMS/lib/python3.12/site-packages/triton/runtime/cache.pyget_home_dirr      s    99]DIIK00r   c                  T    t         j                  j                  t               dd      S )N.tritoncacher   pathjoinr   r   r   r   default_cache_dirr      s    77<<	7;;r   c                  T    t         j                  j                  t               dd      S )Nr   overrider   r   r   r   default_override_dirr      s    77<<	:>>r   c                  T    t         j                  j                  t               dd      S )Nr   dumpr   r   r   r   default_dump_dirr      s    77<<	6::r   c                       e Zd Zd Zedee   fd       Zed
defd       Zededee	eef      fd       Z
edede	eef   fd       Zy	)CacheManagerc                      y Nr   selfkeys     r   __init__zCacheManager.__init__       r   returnc                      y r"   r   r$   filenames     r   get_filezCacheManager.get_file!       r   c                      y r"   r   r$   datar+   binarys       r   putzCacheManager.put%   r-   r   r+   c                      y r"   r   r*   s     r   	get_groupzCacheManager.get_group)   r-   r   groupc                      y r"   r   )r$   r+   r5   s      r   	put_groupzCacheManager.put_group-   r-   r   NT)__name__
__module____qualname__r&   r   r   strr,   r2   r   r4   r7   r   r   r   r    r       s     HSM   #   # (4S>*B   # d38n  r   r    c                       e Zd ZddZdefdZdefdZdee   fdZ	dedee
eef      fdZdede
eef   defd	Zddefd
Zy)FileCacheManagerc                 .   || _         d | _        |rt        j                  dd      j	                         xs
 t               | _        t        j                  j                  | j                  | j                         | _        t        j                  j                  | j                  d      | _        t        j                  | j                  d       y |rot        j                  dd      j	                         xs
 t               | _        t        j                  j                  | j                  | j                         | _        y t        j                  dd      j	                         xs
 t               | _        | j                  rt        j                  j                  | j                  | j                         | _        t        j                  j                  | j                  d      | _        t        j                  | j                  d       y t        d      )	NTRITON_DUMP_DIR lockTexist_okTRITON_OVERRIDE_DIRTRITON_CACHE_DIR$Could not create or locate cache dir)r%   	lock_pathr   r   stripr   	cache_dirr   r   makedirsr   r   RuntimeError)r$   r%   r   r   s       r   r&   zFileCacheManager.__init__4   sL   YY'8"=CCE[IYI[DNWW\\$..$((CDNWW\\$..&ADNKK6YY'<bAGGIcMaMcDNWW\\$..$((CDN  YY'92>DDF]J[J]DN~~!#dnndhh!G!#dnnf!EDNNT:"#IJJr   r(   c                 V    t         j                  j                  | j                  |      S r"   )r   r   r   rJ   r*   s     r   
_make_pathzFileCacheManager._make_pathI   s    ww||DNNH55r   c                     | j                   st        d      t        j                  j	                  | j                  |            S )NrG   )rJ   rL   r   r   existsrN   r*   s     r   has_filezFileCacheManager.has_fileL   s1    ~~EFFww~~dooh788r   c                 H    | j                  |      r| j                  |      S y r"   )rQ   rN   r*   s     r   r,   zFileCacheManager.get_fileQ   s     =="??8,,r   r+   c                 h   d| }| j                  |      sy | j                  |      }t        |      5 }t        j                  |      }d d d        j                  dd       }|y i }|j                         D ]*  \  }}	t        j                  j                  |	      s&|	||<   , |S # 1 sw Y   _xY wN__grp__child_paths)
rQ   rN   openjsonloadgetitemsr   r   rP   )
r$   r+   grp_filenamegrp_filepathfgrp_datarV   resultcps
             r   r4   zFileCacheManager.get_groupW   s     
+}}\*|4, 	$1yy|H	$ll=$7%%' 	DAqww~~a q		 	$ 	$s   B((B1r5   c                     | j                   st        d      t        j                  d|i      }d| }| j	                  ||d      S )NrG   rV   rU   Fr1   )rJ   rL   rX   dumpsr2   r$   r+   r5   grp_contentsr\   s        r   r7   zFileCacheManager.put_groupi   sI    ~~EFFzz=%"89 
+xxl5xAAr   c                    | j                   st        d      t        |t              }|st	        |      }| j
                  J | j                  |      }t	        t        j                               }t        j                         }t        j                  j                  | j                   d| d|       }t        j                  |d       t        j                  j                  ||      }|rdnd}	t        ||	      5 }
|
j                  |       d d d        t        j                   ||       t        j"                  |       |S # 1 sw Y   6xY w)NrG   ztmp.pid__TrC   wbw)rJ   rL   
isinstancebytesr<   rH   rN   uuiduuid4r   getpidr   r   rK   rW   writereplace
removedirs)r$   r0   r+   r1   filepathrnd_idpidtemp_dir	temp_pathmoder^   s              r   r2   zFileCacheManager.putp   s   ~~EFFD%(t9D~~)))??8,TZZ\"iik77<<(3%q0IJ
Ht,GGLL84	t3)T" 	aGGDM	 	

9h'
h	 	s   >EENFFr8   )r9   r:   r;   r&   r<   rN   boolrQ   r   r,   r   r4   r7   r2   r   r   r   r>   r>   2   s    K*6c 69D 9
HSM # (4S>*B $B# Bd38n B B# r   r>   c                   `    e Zd ZdZdefdZedee   deee	f   fd       Z
edede	fd	       Zy
)RemoteCacheBackendzL
    A backend implementation for accessing a remote/distributed cache.
    r%   c                      y r"   r   r#   s     r   r&   zRemoteCacheBackend.__init__   r'   r   	filenamesr(   c                      y r"   r   )r$   r   s     r   rZ   zRemoteCacheBackend.get   r-   r   r+   r0   c                      y r"   r   r$   r+   r0   s      r   r2   zRemoteCacheBackend.put   r-   r   N)r9   r:   r;   __doc__r<   r&   r   r   r   rm   rZ   r2   r   r   r   r}   r}      sd    C  T#Y 4U
+;   C u  r   r}   c                   `    e Zd Zd ZdedefdZdee   deeef   fdZdede	deee	f   fdZ
y	)
RedisRemoteCacheBackendc           	         dd l }|| _        t        j                  j	                  dd      | _        |j                  t        j                  j	                  dd      t        t        j                  j	                  dd                  | _        y )	Nr   TRITON_REDIS_KEY_FORMATztriton:{key}:{filename}TRITON_REDIS_HOST	localhostTRITON_REDIS_PORTi  )hostport)	redis_keyr   environrZ   _key_fmtRedisint_redis)r$   r%   r   s      r   r&   z RedisRemoteCacheBackend.__init__   sd    	

'@B[\kk 3[ARZZ^^$7>? " 
r   r+   r(   c                 P    | j                   j                  | j                  |      S )N)r%   r+   )r   formatr   r*   s     r   _get_keyz RedisRemoteCacheBackend._get_key   s     }}##		H#EEr   r   c                     | j                   j                  |D cg c]  }| j                  |       c}      }t        ||      D ci c]  \  }}|	|| c}}S c c}w c c}}w r"   )r   mgetr   zip)r$   r   r^   resultsr+   r`   s         r   rZ   zRedisRemoteCacheBackend.get   s[    ++""i#HDMM!$4#HI9<Y9Pg%5XvTZTf& gg $Igs   A
A!A!r0   c                 Z    | j                   j                  | j                  |      |       y r"   )r   setr   r   s      r   r2   zRedisRemoteCacheBackend.put   s    h/6r   N)r9   r:   r;   r&   r<   r   r   r   rZ   rm   r2   r   r   r   r   r      sb    
F F FhT#Y h4S> h7C 7u 7c5j1A 7r   r   c                       e Zd ZddZdedefdZdedee   fdZddedefdZ	dedee
eef      fdZded	e
eef   fd
Zy)RemoteCacheManagerc                     t         j                  d   }|j                  d      \  }}t        j                  |      }t        ||      } ||      | _        || _        || _        t        |||      | _
        y )NTRITON_REMOTE_CACHE_BACKEND:)r   r   )r   r   split	importlibimport_modulegetattr_backend	_override_dumpr>   _file_cache_manager)	r$   r%   r   r   remote_cache_managermodule_pathclz_nmemoduleremote_cache_clss	            r   r&   zRemoteCacheManager.__init__   sr    !zz*GH399#>W((5"673(-!
 $4C(QU#V r   r+   r0   c                 >    | j                   j                  ||d      S )NTrd   )r   r2   r   s      r   _materializezRemoteCacheManager._materialize   s     ''++D(4+HHr   r(   c                    | j                   s| j                  r| j                  j                  |      S | j                  j                  |g      }t        |      dk(  ry |j                         \  \  }}| j                  ||      S )Nr   )	r   r   r   r,   r   rZ   lenr[   r   )r$   r+   r   ri   r0   s        r   r,   zRemoteCacheManager.get_file   sq    ::++44X>>
 --##XJ/w<1]]_
	D  400r   c                    | j                   s| j                  r| j                  j                  |||      S t	        |t
              st        |      j                  d      }| j                  j                  ||       | j                  ||      S )Nrd   utf-8)
r   r   r   r2   rl   rm   r<   encoder   r   r/   s       r   r2   zRemoteCacheManager.put   sr    ::++//hv/NN$&t9##G,D(D)  400r   c                    | j                   s| j                  r| j                  j                  |      S d| }| j	                  |      }|y t        |      5 }t        j                  |      }d d d        j                  dd       }d }|Hi }| j                  j                  |      j                         D ]  \  }}	| j                  ||	      ||<    |S # 1 sw Y   ixY wrT   )r   r   r   r4   r,   rW   rX   rY   rZ   r   r[   r   )
r$   r+   r\   r]   r^   r_   rV   r`   
child_pathr0   s
             r   r4   zRemoteCacheManager.get_group   s    ::++55h?? 
+}}\2, 	$1yy|H	$ll=$7 "F$(MM$5$5k$B$H$H$J I 
D%)%6%6z4%Hz"I 	$ 	$s   CCr5   c           	         | j                   s| j                  r| j                  j                  ||      S t	        j
                  dt        t        |j                                     i      }d| }| j                  ||      S )NrV   rU   )
r   r   r   r7   rX   re   sortedlistkeysr2   rf   s        r   r7   zRemoteCacheManager.put_group   sh    ::++55hFFzz=&ejjl9K2L"MN 
+xxl33r   Nrz   r8   )r9   r:   r;   r&   r<   rm   r   r   r,   r2   r   r4   r7   r   r   r   r   r      s    WIS I I1 1# 11# 1s 1# (4S>*B .4# 4d38n 4r   r   DEFAULTc                     t        j                  t        j                  |             j	                  d      j                  d      S )Nr   =)base64	b32encoderm   fromhexdecoderstripr%   s    r   _base32r     s2    EMM#./66w?FFsKKr   r(   c                     dd l }|j                  j                  dd       }|@|t        k7  r7|j	                  d      \  }}t        j                  |      }t        ||      a|at        t        |             S )Nr   TRITON_CACHE_MANAGERr   )
r   r   rZ   __cache_cls_nmer   r   r   r   __cache_clsr   )r%   r   user_cache_managerr   r   r   s         r   get_cache_managerr     sp    (>E %*<*O177<W((5fg.,ws|$$r   c                 .    t        t        |       d      S )NT)r   r   r   r   s    r   get_override_managerr     s    ws|d33r   c                 .    t        t        |       d      S )NT)r   r   r   s    r   get_dump_managerr     s    ws|$//r   c                 p   |j                         D ci c]  \  }}||d   dk(  rdn| }}}|  ddj                  |j                                d| d| }|D ]  }| d|j                  |       } t	        j
                  |j                  d            j                         }t        |      S c c}}w )Nr   *ptr-rA   r   )	r[   r   valuesrZ   hashlibsha256r   	hexdigestr   )	version_hash	signature	constantsidskwargskvr%   kws	            r   make_so_cache_keyr      s    <EOO<MNDAqQqTS[Ea/NINN!BGGI$4$4$678)AcU
KC (Qvzz"~&'(
..G,
-
7
7
9C3< Os   B2)r   rX   r   rn   abcr   r   pathlibr   typingr   r   r   r   r   r   r   r   r   r    r>   r}   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s      	  #  ' '  1<?;3 ,V| Vr "70 7,J4 J4Z L
%l % 4 40\ 0r   