
    DCe!                        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
                            d          pdZ eej
                            d                    peZg ai a e	            Zej
                            d          pdZej
                            d          pdZd Zd	Zd
Zd Zd ZdddZddZd Z e j        e           ddZddZdS )    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          |                                          f}|t          vr8t          5  |t          vr | i |t          |<   d d d            n# 1 swxY w Y   t          |         S N)__name__tupleitemsCACHELOCK)argskwargskeyfuncs      9lib/python3.11/site-packages/anaconda_anon_usage/utils.pycall_if_neededzcached.<locals>.call_if_needed'   s    }dE&,,..$9$9:e 7 7 e##!%t!6v!6!6E#J7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 Szs   A!!A%(A% )r   r   s   ` r   cachedr   &   s$    
 
 
 
 
     c                  8    t                                            d S r   )r   clearr   r   r   _cache_clearr    6   s    	KKMMMMMr   Ferrorc                    |rt           sddlm} |j         }|st           r(t	          t
          | z   |z  t          j                   d S d S )Nr   )context)file)DEBUGconda.base.contextr$   jsonprintDPREFIXsysstderr)sr"   r   r$   s       r   _debugr.   ;   sl     	!U 	! 	/.....L  5 5w{d"4444445 5r   randomc                     t          j        d          }t          j        |                              d                              d          }t          d| |           |S )N      =asciizGenerated %s token: %s)osurandombase64urlsafe_b64encodestripdecoder.   )whatdataresults      r   _random_tokenr=   J   sS    :b>>D%d++11$77>>wGGF
#T6222Mr   c                  D    t           D ]\  } }}t          | ||           dS )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_attemptrD   Q   s9     %- 1 1 
E5z5%00001 1r   c                 d   | r&t          |           st          d|            t          S 	 |rt          t          j        d          t          j        t          |          d           t          |d          5 }|
                    |           ddd           n# 1 swxY w Y   t          d|           t          S # t          $ rh}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OSErrorrH   EROFSr4   makedirsr   openwriteWRITE_SUCCESS	ExceptiongetattrEACCESEPERM
WRITE_FAIL)rA   rB   client_tokenemulate_failfpexcs         r   r@   r@   _   s   
  &,, (*555 	E%+'CDDD
GENNT2222% 	#HH\"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# %(((    3&&5<ek*RRR=>>>>S	    sC   AB= 9BB= BB= "B#B= =
D/AD*$D/*D/c                 f   d}t          d|                                |            |d         t          v rt          d|           nt          |           r	 t	          |           5 }d                    |                                                                dd                   }ddd           n# 1 swxY w Y   t          d||           n*# t          $ r}t          d| |d	
           Y d}~nd}~ww xY wt          |          dk     rt          |          dk    rt          d           t          |          }t          || ||d         t          v           }|t          k    rt          d|           dS |t          k    r,t          d           t                              || |f           |S )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   rM   joinread
splitlinesrP   lenr=   r@   WRITE_CHAOSrT   rI   r?   append)rB   r:   rA   rU   rW   rX   statuss          r   _saved_tokenrd   ~   s    L
 1 15999Aw*3T::::	 Q	Qe C!wwrwwyy';';'='=bqb'ABBC C C C C C C C C C C C C C C+T<@@@@ 	Q 	Q 	Q74PPPPPPPPP	Q
<2|q  ,---$T**
E<aKAWXXZ-t4442{"" *+++OOZ=>>>s=   C %AB3'C 3B77C :B7;C 
C7C22C7)r/   )Fr   )atexitr6   rH   r4   r+   os.pathr   r   	threadingr   environgetr*   boolr&   r?   r   r   r\   ra   rO   rI   rT   r   r    r.   r=   rD   registerr@   rd   r   r   r   <module>rl      s      				 



 # # # # # # # #      
*..;
<
<
BRZ^^78899DW 
 	uww Z^^<==C
 jnn>??E2
     
 ! 5 5 5 5 5   1 1 1       >! ! ! ! ! !r   