
    iGgs              
          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mZ d dlm	Z	 d dl
mZmZ ej                  j                  d      xs dZ eej                  j                  d            xs eZg ai a e	       Zej                  j                  d      xs dZej                  j                  d	      xs dZd Zd
ZdZdZed
z
  dz  d
z   Zd Zd ZdddZddZd Z  e jB                  e        ddZ"t$        fde#de#dedee#   fdZ$ddZ%y)    N)dirnameexists)RLock)ListOptional ANACONDA_ANON_USAGE_DEBUG_PREFIX ANACONDA_ANON_USAGE_DEBUGANACONDA_ANON_USAGE_READ_CHAOSANACONDA_ANON_USAGE_WRITE_CHAOS            c                       fd}|S )Nc                      j                   | t        |j                               f}|t        vr/t        5  |t        vr | i |t        |<   d d d        t        |   S t        |   S # 1 sw Y   t        |   S xY wN)__name__tupleitemsCACHELOCK)argskwargskeyfuncs      9lib/python3.12/site-packages/anaconda_anon_usage/utils.pycall_if_neededzcached.<locals>.call_if_needed.   sr    }}dE&,,.$9:e e#!%t!6v!6E#J  SzuSz  Szs   A((A9 )r   r   s   ` r   cachedr    -   s    
     c                  ,    t         j                          y r   )r   clearr   r!   r   _cache_clearr$   =   s    	KKMr!   Ferrorc                    |rt         sddlm} |j                   }|st         r&t	        t
        | z   |z  t        j                         y y )Nr   )context)file)DEBUGconda.base.contextr(   jsonprintDPREFIXsysstderr)sr&   r   r(   s       r   _debugr2   B   s9    U 	/LL w{d"4 r!   c                     t        j                  t        dz  dz
  dz  dz         }t        j                  |      j                  d      d t         }t        d| |       |S )Nr   r      asciizGenerated %s token: %s)osurandomTOKEN_LENGTHbase64urlsafe_b64encodedecoder2   )whatdataresults      r   _random_tokenr?   Q   sZ     ::|a'!+1A56D%%d+227;M\JF
#T62Mr!   c                  >    t         D ]  \  } }}}t        | ||        y)z
    Called upon the graceful exit from conda, this attempts to
    write an environment token that was deferred because the
    environment directory was not yet available.
    N)DEFERRED_write_attempt)
must_existfpathtokenr<   s       r   _final_attemptrF   Z   s#     +3&
E5$z5%0 +3r!   c                 (   | rt        |       st        d|        t        S 	 |rt        t        j
                  d      t        j                  t        |      d       t        |d      5 }|j                  |       ddd       t        d|       t        S # 1 sw Y   xY w# t        $ rg}t        |dd      t        j                  t        j                  t        j
                  fv rt        d	       nt        d
||d       t         cY d}~S d}~ww xY w)zj
    Attempt to write the token to the given location.
    Return True with success, False otherwise.
    zDirectory not ready: %szTesting permissions issuesT)exist_okwNzToken saved: %serrnoz(No write permissions; cannot write tokenz?Unexpected error writing token file:
  path: %s
  exception: %sr%   )r   r2   WRITE_DEFEROSErrorrJ   EROFSr6   makedirsr   openwriteWRITE_SUCCESS	ExceptiongetattrEACCESEPERM
WRITE_FAIL)rC   rD   client_tokenemulate_failfpexcs         r   rB   rB   h   s    
 &,(*5%++'CDD
GENT2%HH\"  %(    3&5<<ekk*RR=>S	 s7   AB! )B;B! BB! !	D*ADDDrD   r<   deferred_writesreturnc                 :    |D ]  \  }}}}|| k(  s||k(  s|c S  y)a  
    Check if the deferred token write exists in the DEFERRED write array.
    If the path must already exist, this helper function determines
    if the token will be written in the future.

    Args:
        fpath: The file path to check for.
        what: The type of token to check for.
        deferred_tokens: The list of deferred tokens to check.

    Returns:
        The token if it exists, otherwise None.
    Nr   )rD   r<   r[   _rY   rE   rI   s          r   _deferred_existsr_      s(      +2ua;19L +r!   c                    t        | |      }|rt        d||       |S d}t        d|j                         |        |d   t        v rt        d|       n]t	        |       rR	 t        |       5 }dj                  |j                         j                         dd       }ddd       t        d||       t        |      t        k  rt        |      dkD  rt        d|       t        |      }t        || ||d   t        v       }|t        k(  rt        d|       y|t         k(  r%t        d|       t"        j%                  || ||f       |S # 1 sw Y   xY w# t        $ r}t        d	| |d
       Y d}~d}~ww xY w)z
    Implements the saved token functionality. If the specified
    file exists, and contains a token with the right format,
    return it. Otherwise, generate a new one and save it in
    this location. If that fails, return an empty string.
    zReturning deferred %s token: %sr	   z%s token path: %sr   z"Pretending %s token is not presentNr   zRetrieved %s token: %sz!Unexpected error reading: %s
  %sTr%   zGenerating longer %s tokenzReturning blank %s tokenzDeferring %s token write)r_   r2   
capitalize
READ_CHAOSr   rO   joinread
splitlinesrR   lenr8   r?   rB   WRITE_CHAOSrV   rK   rA   append)rD   r<   rC   deferred_tokenrW   rY   rZ   statuss           r   _saved_tokenrk      s_    &eT2N0$GL
 159Aw*3T:		Qe!wwrwwy';';'=bq'AB +T<@ <<'|q /6$T*
E<aKAWXZ-t4{" -t4OOZdCD%   	Q74PP	Qs0   E *1EE E	E 	E2E--E2)random)Fr   )&atexitr9   rJ   r6   r/   os.pathr   r   	threadingr   typingr   r   environgetr.   boolr*   rA   r   r   rb   rg   rQ   rK   rV   MIN_ENTROPYr8   r    r$   r2   r?   rF   registerrB   strr_   rk   r   r!   r   <module>rw      s-      	 
 #  !
**..;
<
BRZZ^^789DW 
 	w ZZ^^<=C
 jjnn>?E2
  aA%) 
 ! 51  @ 4<,0c]*(r!   