o
    UÀ7e  ã                   @   sL  d dl Z z
d dlmZmZ W n ey   d dlmZmZ Y nw d dlZd dlZd dlZd dlZd dl	Z
d dlZ
zd dlmZ W n eyO   d dlmZ Y nw d dlZG dd„ de jƒZedkr¤e
j ¡ Ze e
j ¡ ¡ e d¡Ze ¡  ¡ Ze  ¡ Ze ¡  dej j!ej j"d  j!ef Z#ee$ej j% &¡ ƒe#e  ¡ eƒZ'e' (¡  dS dS )	é    N)ÚEmptyÚQueue)Úurlopenc                   @   sf   e Zd ZdZG dd„ dejƒZddd„Zdd„ Zddd„Z	dd„ Z
ddd„Z		
ddd„Zdd„ ZdS )Ú
MirrorTestz@Determines the best mirrors by perfoming ping and download test.c                   @   s"   e Zd ZdZd	dd„Zdd„ ZdS )
zMirrorTest.PingWorkerz¢Use the command line command ping to determine the server's
           response time. Using multiple threads allows to run several
           test simultaneously.©r   é   ©r   r   c                 C   s@   || _ || _|| _|| _|| _|| _t d¡| _t	j
 | ¡ d S )Nz^rtt .* = [\.\d]+/([\.\d]+)/.*)ÚbordersÚmodÚparentÚidÚjobsÚresultsÚreÚcompileÚmatch_resultÚ	threadingÚThreadÚ__init__)Úselfr   r   r   r   r	   r
   © r   ú?/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.pyr      s   zMirrorTest.PingWorker.__init__c                 C   s0  d }| j  ¡ s’| jj ¡ r”z5| j  d¡}|j}| j d| ¡ tj	ddddd|gtj
tjdd	j}	 | ¡ }|s9nt | j|¡}q2W n tyL   Y d S    | j d
| ¡ Y tj ¡  t jd7  _| j tjtj| j| j¡ |r€| j t|d ƒ||g¡ tj ¡  | j  ¡ s–| jj ¡ sd S d S d S d S )NFzPinging %s...Úpingz-qz-c 2z-W 1z-i 0.5T)ÚstdoutÚstderrÚuniversal_newlineszSkipping %sr   r   )r   Úemptyr   ÚrunningÚisSetÚgetÚhostnameÚreport_actionÚ
subprocessÚPopenÚPIPEÚSTDOUTr   Úreadliner   Úfindallr   r   r   Úcompleted_lockÚacquireÚ	completedÚreport_progressÚtodor	   r
   r   ÚappendÚfloatÚrelease)r   ÚresultÚmirrorÚhostÚcommandoÚliner   r   r   Úrun#   sF   ýýü

ý
&äzMirrorTest.PingWorker.runN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r5   r   r   r   r   Ú
PingWorker   s    
	r:   Nc                 C   sl   t j | ¡ d| _d| _|| _d | _|| _g | _dt	_
t  ¡ t	_t|ƒt	_|| _|s1t  ¡ | _d S || _d S )NÚ )r   r   g        r   )r   r   r   ÚactionÚprogressÚeventÚbestÚ	test_fileÚthreadsr   r*   ÚLockr(   Úlenr,   ÚmirrorsÚEventr   )r   rD   r@   r>   r   r   r   r   r   C   s   


zMirrorTest.__init__c                 C   s   || _ | jr| j ¡  d S d S ©N)r<   r>   Úset)r   Útextr   r   r   r!   T   s   ÿzMirrorTest.report_action©r   éd   r   c                 C   sP   ||d  ||d  |d |d |d  | |  f| _ | jr&| j ¡  dS dS )zTSubclasses should override this method to receive
           progress status updatesr   r   N)r=   r>   rG   )r   ÚcurrentÚmaxr	   r
   r   r   r   r+   Y   s   

þÿzMirrorTest.report_progressc              
   C   sÎ   | j dddd}t| jƒ}|dkr3| dd| jt d|d ¡ g¡ | dd| jt d|d ¡ g¡ | jdd	„ |D ƒd
tjtjfd}|D ]\}}t	d|j
|f ƒ qF|sXd S |d d j
}t	d| ƒ |S )Né   )r   ç      à?)r   é   )rL   r	   r
   é   r   r   c                 S   s   g | ]}|d  ‘qS )rP   r   )Ú.0Úrr   r   r   Ú
<listcomp>j   s    z,MirrorTest.run_full_test.<locals>.<listcomp>©rN   r   )r	   r
   zmirror: %s - time: %szand the winner is: %s)Úrun_ping_testrC   rD   r-   ÚrandomÚrandintÚrun_download_testr   r,   Úprintr    )r   Úresults_pingÚsizer   ÚtÚhÚwinnerr   r   r   Úrun_full_testb   s$   
""ÿþzMirrorTest.run_full_testr   c           
   	   C   sŠ   |dkr| j }tƒ }|D ]}| |¡ qg }tdƒD ]}t |||| ||¡}	| j |	¡ |	 ¡  q| jD ]}	|	 	¡  q4| 
¡  |d|… S )zõPerforms ping tests of the given mirrors and returns the
           best results (specified by max).
           Mod and borders could be used to tweak the reported result if
           the download test is only a part of a whole series of tests.Né   r   )rD   r   ÚputÚranger   r:   rA   r-   ÚstartÚjoinÚsort)
r   rD   rL   r	   r
   r   Úmr   Úir\   r   r   r   rU   w   s   


zMirrorTest.run_ping_testc           	         s†   ‡ fdd„}|dkrˆ j }g }|D ]'}ˆ j ¡ s n||ƒ}|dkr)| ||g¡ ˆ  | |¡d t|ƒd|¡ q| ¡  |d|… S )zùPerforms download tests of the given mirrors and returns the
           best results (specified by max).
           Mod and borders could be used to tweak the reported result if
           the download test is only a part of a whole series of tests.c                    sZ   d|   ¡ d ˆ jf }ˆ  d| ¡ t ¡ }zt|dd d¡ t ¡ | W S    Y dS )Nz%s/%sr   zDownloading %s...rP   )Útimeouti  )Úget_repo_urlsr@   r!   Útimer   Úread)r1   Úurlrc   ©r   r   r   Útest_download_speed”   s   ÿz9MirrorTest.run_download_test.<locals>.test_download_speedNr   r   rT   )rD   r   r   r-   r+   ÚindexrC   re   )	r   rD   rL   r	   r
   rn   r   rf   Údownload_timer   rm   r   rX   Ž   s   

 zMirrorTest.run_download_testc                 C   s   |   ¡ | _| j ¡  dS )z/Complete test exercise, set self.best when doneN)r_   r?   r   Úclearrm   r   r   r   r5   ¬   s   
zMirrorTest.runrF   )rI   r   )NNr   r   )r6   r7   r8   r9   r   r   r:   r   r!   r+   r_   rU   rX   r5   r   r   r   r   r      s    
-
	

ÿr   Ú__main__zdpkg --print-architecturez!dists/%s/%s/binary-%s/Packages.gz))r   Úqueuer   r   ÚImportErrorrj   r   r"   ÚosÚaptsources.distroÚ
aptsourcesÚaptsources.sourceslistÚurllib.requestr   Úurllib2rV   r   r   r6   ÚdistroÚ
get_distroÚget_sourcesÚsourceslistÚSourcesListÚpopenÚpiperk   ÚstripÚarchrE   r   rG   Úsource_templateÚnameÚ
componentsr@   ÚlistÚ
mirror_setÚvaluesÚappr_   r   r   r   r   Ú<module>   sL   ÿÿ 

þÿ
ÿó