
    7.i                         S r SSKrSSKrSSKrSSKrSSKJr  SSKJ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  S
SKJr  SqSqSrSrSr\\\4rSrSrSrSS.S jr S r!SS jr"S r#\$S:X  a  \#" 5         gg)a0  
This module implements a heartbeat function that sends a simple
HEAD request to an upstream repository. It can be configured to
trigger upon environment activation, but it is off by default.
The intended use case is for organizations to enable it through
system configuration for better usage tracking.
    N)Thread)urljoin)Contextcontextlocate_prefix_by_name)get_session)Channel)RequestException)	HTTPError   )utilsFzhttps://repo.anaconda.cloud/zhttps://conda.anaconda.org/zhttps://repo.anaconda.com/pkgs/znoarch/activate-0.0.0-0.condag      ?   errorc                    [         (       d  [        R                  (       d  U(       d  g [        (       a,  U(       d%  [        R                  (       d  [        R
                  O[        R                  n[        X-  US9  g )N)file)VERBOSEr   DEBUG
STANDALONEsysstdoutstderrprint)msgr   argsofiles       =lib/python3.13/site-packages/anaconda_anon_usage/heartbeat.py_printr   )   s=     Gu{{e %*eu{{CJJE	#*5!    c                     [         R                  " 5       nU R                  XR                  US9n[         R                  " 5       U-
  n[	        SXTR
                  5        g ! [         aw  n[         R                  " 5       W-
  n[        U[        [        [        [        45      (       a%  [	        SU[        U5      R                  5         S nAg [	        SXVSS9   S nAg S nAff = f)N)proxiestimeoutz*Success after %.3fs; code (expect 404): %dz Heartbeat failed after %.3fs: %sz*Unexpected heartbeat error after %.3fs: %sTr   )timeperf_counterheadr!   r   status_code	Exception
isinstancer
   r   OSErrorTimeoutErrortype__name__)sessionurlr"   
start_timeresponsedeltaexcs          r   _pingr3   5   s    Y &&(
<<__g<N!!#j08%AUAU	
  
Y!!#j0
 c,i,OPP5ud3i>P>PQQ ?SWX
Ys   AA   
C!*ACCC!c                 
  ^ [        [        S5      (       d  SSKJn  UR	                  5         U(       a	  SU;   a  UnO[        R
                  (       d  S/[        l        [        R                   VVs/ s H%  n[        U5      R                  5         H  owPM     M'     nnnUR                  S [        R                  R                  5        5       5        [         H  m[        U4S jU 5       5      (       d  M    O   [        S5        g [        TU=(       d    S	5      S-   nUc  [        n[        XS5      n[        S
U5        U (       a  U [         l        [        SU 5        [        S[        R$                  5        U(       a  [        S5        g [&        [(        -  n	[(        S-
  [        l        S[        l        [/        U5      n
[1        [2        XU	4SS9nUR5                  5         [6        (       a  UR9                  5         g g s  snnf )N_aau_initializedr   )patch/defaultsc              3   B   #    U  H  oR                  S 5      v   M     g7f)r7   N)rstrip).0us     r   	<genexpr>$attempt_heartbeat.<locals>.<genexpr>Y   s     H+GaHHSMM+Gs   c              3   D   >#    U  H  oR                  T5      v   M     g 7fN)
startswith)r;   r<   bases     r   r=   r>   [   s     4t!<<%%ts    zNo valid heartbeat channelmainzHeartbeat url: %sz
Prefix: %szUser agent: %sz(Dry run selected, not sending heartbeat.r   F)targetr   daemon)hasattrr    r6   rC   	_channelschannelsr	   urlsextendchannel_aliasREPOSanyr   r   HEARTBEAT_PATHr   checked_prefix
user_agentTIMEOUTATTEMPTSremote_max_retriesremote_backoff_factorr   r   r3   startr   join)prefixdry_runchannelpathr6   r.   cr<   rJ   r"   r-   trB   s               @r   attempt_heartbeatr^   L   s   7.//

3'>   !+G"++G+aWQZ__5F5F+GH7+@+@+E+E+GHHD4t444  /0dG-v.4|
#
C
$!'|V$
W//09:  G!)AG$%G!#G
 	e7"9%HAGGIz	 G Hs   1,G?c                     Sq S[        R                  ;  =(       a    S[        R                  ;  qSn [	        U 5        [	        S5        [	        U 5        S nS n[
        R                  " 5       nUR                  5       nUR                  SS	US S
S9  UR                  SSUS SS9  UR                  SSSSS9  UR                  SSSSS9  UR                  SS SS9  UR                  SS SS9   UR                  5       n[        UR                  =(       d    UR                  UR                  UR                  UR                  S9  [	        U 5        g ! [	        U 5        f = f)NTz--quietz-qz-----------------------------zanaconda-anon-usage heartbeatc                 R    [         R                  R                  U 5      (       d   eU $ r@   )osr[   isdirss    r   environment_pathmain.<locals>.environment_path   s     ww}}Qr   c                     [        U 5      $ r@   )r   rc   s    r   environment_namemain.<locals>.environment_name   s    $Q''r   z-nz--namez6Environment name; defaults to the current environment.)r+   defaulthelpz-pz--prefixz8Environment prefix; defaults to the current environment.z-dz	--dry-run
store_truez/Do not send the heartbeat; just show the steps.)actionrk   zSuppress console logs.z	--channelz(advanced) The full URL to a custom repository channel. By default, an Anaconda-hosted channel listed in the user's channel configuration is used.)rj   rk   z--pathz6(advanced) A custom path to append to the channel URL.)rX   rY   rZ   r[   )r   r   argvr   r   argparseArgumentParseradd_mutually_exclusive_groupadd_argument
parse_argsr^   rX   namerY   rZ   r[   )linere   rh   pgr   s         r   rC   rC      sh    Jsxx'@D,@G*D
4L
*+
4L( 	!A	&&(ANNE   NNG   NN>	   NN4<>VNWNNV   NNE  	||~;;+$))LLLL		
 	tts   +AE E__main__)NFNN)%__doc__ro   ra   r   r#   	threadingr   urllib.parser   conda.base.contextr   r   r   !conda.gateways.connection.sessionr   conda.models.channelr	   requests.exceptionsr
   urllib3.exceptionsr   rG   r   r   r   CLD_REPOORG_REPOCOM_REPOrM   rO   rR   rS   r   r3   r^   rC   r,    r   r   <module>r      s     	 
     F F 9 ( 0 ( 

)(,	8X&0  # 	"Y.0f>B zF r   