
    Ee!                        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	 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d ZdddZddZd Z e j8                  e       ddZddZy)    N)dirnameexists)RLock 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'   sw    }}dE&,,.$9:e 7 e#!%t!6v!6E#J7 SzuSz7 Szs   A((A9 )r   r   s   ` r   cachedr   &   s    
     c                  ,    t         j                          y r   )r   clearr   r   r   _cache_clearr    6   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   _debugr.   ;   s9    U 	/LL w{d"4 r   c                     t        j                  d      }t        j                  |      j	                  d      j                  d      }t        d| |       |S )N      =asciizGenerated %s token: %s)osurandombase64urlsafe_b64encodestripdecoder.   )whatdataresults      r   _random_tokenr<   J   sG    ::b>D%%d+11$7>>wGF
#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tokens      r   _final_attemptrC   Q   s&     %- 1 
E5z5%01r   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   r.   WRITE_DEFEROSErrorrG   EROFSr3   makedirsr   openwriteWRITE_SUCCESS	ExceptiongetattrEACCESEPERM
WRITE_FAIL)r@   rA   client_tokenemulate_failfpexcs         r   r?   r?   _   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c                    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        |      dk  r}t        |      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.
    r   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 tokenzReturning blank %s tokenzDeferring token write)r.   
capitalize
READ_CHAOSr   rL   joinread
splitlinesrO   lenr<   r?   WRITE_CHAOSrS   rH   r>   append)rA   r9   r@   rT   rV   rW   statuss          r   _saved_tokenrc   ~   sC    L
 159Aw*3T:		Qe C!wwrwwy';';'=bq'ABC+T<@ <2|q ,-$T*
E<aKAWXZ-t4{" *+OOZ=>%C C  	Q74PP	Qs0   D, 1D >D,  D)%D, ,	E5E		E)random)Fr   )atexitr5   rG   r3   r+   os.pathr   r   	threadingr   environgetr*   boolr&   r>   r   r   r[   r`   rN   rH   rS   r   r    r.   r<   rC   registerr?   rc   r   r   r   <module>rl      s       	 
 # 
**..;
<
BRZZ^^789DW 
 	w ZZ^^<=C
 jjnn>?E2
 
 ! 51  >!r   