
    5pjn                        S r SSKrSSKrSSKrSSKJrJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJrJr  \R"                  " \5      rSrS	r\ " S
 S5      5       r\ " S S5      5       rS\	4S jrS\S\	4S jrS\S\S-  4S jrSS.S\S\S\S\S\S-  S\S-  S\S-  S\\   S\\   S-  SS4S jjrS\SS4S jrS\	S\SS4S  jr g)!z?Best-effort local cache for SandboxPool hosts (host/pool mode).    N)asdict	dataclassfield)Path)List   )	constants)WeakFileLockloggingg      @c                   d    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \\S'   S	r\	\S
'   Sr
g)
CachedHost&   a3  A single host Job of a pool, as last seen by some process.

`base_url` + `nonce` are everything needed to rebuild the in-job server transport
(`_SandboxServer`) without an `inspect_job` round-trip: the per-sandbox auth token is
re-derived from the user's HF token and `nonce` (see `_derive_sandbox_token`).
job_idownerbase_urlnoncecapacitylive        
updated_at N)__name__
__module____qualname____firstlineno____doc__str__annotations__intr   float__static_attributes__r       Z/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/huggingface_hub/_sandbox_cache.pyr   r   &   s1     KJMJM
IJr"   r   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \S-  \S'   \S-  \S	'   \S-  \S
'   \" \	S9r
\\   \S'   \r\\S'   Sr\\S'   Srg)	PoolCache8   zJCached view of one pool: its config (to boot new hosts) + its known hosts.pool_idimageflavorsandboxes_per_hostN	max_hostsidle_timeout	namespace)default_factoryhostsversionr   r   r   )r   r   r   r   r   r   r   r   r   listr/   r   r   _CACHE_VERSIONr0   r   r    r!   r   r"   r#   r%   r%   8   s`    TLJKTz*Tz#D9E4
9!GS!Jr"   r%   returnc                  @    [        [        R                  5      S-  S-  $ )Nsandboxpools)r   r	   HF_HOMEr   r"   r#   
_pools_dirr8   H   s    	!!"Y.88r"   r'   c                    ^  [        U 4S jS 5       5      (       d  T S;   a  [        ST < 35      e[        5       T  S3-  $ )z.Path of the cache file for `pool_id` (no I/O).c              3   ,   >#    U  H	  oT;   v   M     g 7f)Nr   ).0cr'   s     r#   	<genexpr>"pool_cache_path.<locals>.<genexpr>N   s     
5!4A<!4s   )/\ ).z..zInvalid pool id: z.json)any
ValueErrorr8   )r'   s   `r#   pool_cache_pathrE   L   sC    

5!4
555K9O,WK899<WIU+++r"   c                     [        U 5      nUR                  SSS9 n[        R                  " U5      nSSS5        WR	                  S5      [
        :w  a  gUR                  S/ 5       Vs/ s H  n[        S	0 UD6PM     nn[        S	0 UDSU0D6$ ! , (       d  f       Nb= fs  snf ! [         a     g[         a&  n[        R                  SU < SU 35         SnAgSnAff = f)
zOReturn the cached view of `pool_id`, or `None` if missing/corrupt/incompatible.rutf-8encodingNr0   r/   z+Ignoring unreadable sandbox pool cache for : r   )rE   openjsonloadgetr2   popr   r%   FileNotFoundError	Exceptionloggerdebug)r'   pathfdatahr/   es          r#   read_pool_cacherZ   S   s    w'YYsWY-99Q<D .88I.0*.((7B*?@*?Qa*?@-4-u-- .- A  B7+RPQsSTsE   B& B!B& B& *B!?B& 
BB& &
C!2	C!;CC!)dead_host_idsr(   r)   r*   r+   r,   r-   r/   r[   c                   U=(       d
    [        5       n	 [        U 5      n
U
R                  R                  SSS9  [	        [        U
5      S-   [        S9   [        U 5      nU(       a  UR                  O/  Vs0 s H  oR                  U_M     nnU H  nXUR                  '   M     U	 H  nUR                  US5        M     [        U UUUUUU[        UR                  5       5      [        R                  " 5       S9	n[        U
U5        SSS5        gs  snf ! , (       d  f       g= f! [          a&  n["        R%                  SU < SU 35         SnAgSnAff = f)	a  Merge `hosts` into the cache for `pool_id` (best-effort, never raises).

Concurrency-safe: under a file lock, the on-disk hosts are read, then `hosts` are
upserted by `job_id` and `dead_host_ids` removed, so a process only adds/updates what
it learned and never drops hosts another process discovered. The result is written
atomically. The pool config is refreshed from the arguments.
T)parentsexist_okz.lock)timeoutN)	r'   r(   r)   r*   r+   r,   r-   r/   r   z'Could not write sandbox pool cache for rK   )setrE   parentmkdirr
   r   _LOCK_TIMEOUTrZ   r/   r   rP   r%   r1   valuestime_atomic_writerR   rS   rT   )r'   r(   r)   r*   r+   r,   r-   r/   r[   deadrU   existingrX   mergedhostr   cacherY   s                     r#   save_pool_cacherl   d   s1   & !CEDQw'$6#d)g-}E&w/H>FHNNB,NP,Nahhk,NFP&*t{{# 

64( #5#)#6==?+99;
E $&% FEP FE&  Q>wkA3OPPQsH   ?D' #D7DA;DD' D
D$ D' $D' '
E1EEc                      [        U 5      R                  SS9  g! [         a%  n[        R	                  SU  SU 35         SnAgSnAff = f)z@Remove the cache file for `pool_id` (best-effort, never raises).T)
missing_okz(Could not delete sandbox pool cache for rK   N)rE   unlinkrR   rS   rT   )r'   rY   s     r#   delete_pool_cacherp      sM    P ''4'8 P?y1#NOOPs    
A
AA
rU   rk   c                 &   U R                   U R                   S[        R                  " 5        S3-  nUR	                  SSS9 n[
        R                  " [        U5      USS9  SSS5        [        R                  " X 5        g! , (       d  f       N%= f)	zSWrite the cache via a temp file + `os.replace` so readers never see a partial file.rB   z.tmpwrH   rI      )indentN)	ra   nameosgetpidrL   rM   dumpr   replace)rU   rk   tmprV   s       r#   rf   rf      sg    
++499+Qryy{m48
8C	#	(A		&-1- 
)JJs 
)	(s    B
B)!r   rM   rv   re   dataclassesr   r   r   pathlibr   typingr    r	   utilsr
   r   
get_loggerr   rS   r2   rc   r   r%   r8   r   rE   rZ   r   r`   rl   rp   rf   r   r"   r#   <module>r      sp   F  	  0 0    ( 
		H	%     "   9D 9,S ,T ,S Y%5 6 &*+Q+Q +Q 	+Q
 +Q Tz+Q *+Q Tz+Q 
+Q s8d?+Q 
+Q\Ps Pt P Y 4 r"   