
    c                     6   d dl Z 	 d dlmZmZ d dlZ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 d dlZ G d de j                         Zedk(  r(e
j&                  j)                         Zej+                  e
j,                  j/                                 ej0                  d      Zej5                         j7                         Z e j:                         Zej?                          dej@                  jB                  d	ej@                  jD                  d    jB                  d
edZ# e e$ej@                  jJ                  jM                               e# e j:                         e      Z'e'jQ                          yy# e$ r d dlmZmZ Y ww xY w# e$ r
 d dlmZ Y jw xY w)    N)EmptyQueue)urlopenc                   p    e Zd ZdZ G d dej
                        ZddZd ZddZ	d Z
dd	Z	 	 dd
Zd Zy)
MirrorTestz@Determines the best mirrors by perfoming ping and download test.c                       e Zd ZdZddZd Zy)MirrorTest.PingWorkerzUse the command line command ping to determine the server's
           response time. Using multiple threads allows to run several
           test simultaneously.c                     || _         || _        || _        || _        || _        || _        t        j                  d      | _        t        j                  j                  |        y )Nz^rtt .* = [\.\d]+/([\.\d]+)/.*)bordersmodparentidjobsresultsrecompilematch_result	threadingThread__init__)selfr   r   r   r   r   r   s          ?/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.pyr   zMirrorTest.PingWorker.__init__   sR    "DLDH DKDGDI"DL "

+L MD%%d+    c                    d }| j                   j                         s| j                  j                  j	                         r	 | j                   j                  d      }|j                  }| j                  j                  d|z         t        j                  ddddd|gt        j                  t        j                  d	      j                  }	 |j                         }|sn!t        j                  | j                   |      }4	 t$        j&                  j)                          t$        xj*                  dz  c_        | j                  j-                  t$        j*                  t$        j.                  | j0                  | j2                         |r*| j4                  j7                  t9        |d         |g       t$        j&                  j;                          | j                   j                         s'| j                  j                  j	                         ry y y y # t"        $ r Y y  | j                  j                  d
z         Y @xY w)NFzPinging %s...pingz-qz-c 2z-W 1z-i 0.5T)stdoutstderruniversal_newlineszSkipping %s   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lines         r   runzMirrorTest.PingWorker.run#   s   Fiioo'DKK,?,?,E,E,GD!YY]]51F!??DKK--o.DE)//vvxY]0^7A7A7H7HCG I JP  '002#!!#D,=,=t!D	  " ))113$$)$++J,@,@,6OO,0LL,0HH6 LL''vay)94(HI))1139 iioo'DKK,?,?,E,E,G',G'  DKK--md.BCs   B?H 	I"IN)r   r   r   r   )__name__
__module____qualname____doc__r   r9    r   r   
PingWorkerr	      s    	#	,	4r   rA   Nc                 b   t         j                  j                  |        d| _        d| _        || _        d | _        || _        g | _        dt        _
        t        j                         t        _        t        |      t        _        || _        |st        j                          | _        y || _        y )N )r   r   g        r   )r   r   r   actionprogresseventbest	test_filethreadsr   r.   Lockr,   lenr0   mirrorsEventr!   )r   rL   rH   rF   r!   s        r   r   zMirrorTest.__init__C   s    !!$'#
	" 
$-NN$4
!g,
$??,DL"DLr   c                 `    || _         | j                  r| j                  j                          y y N)rD   rF   set)r   texts     r   r%   zMirrorTest.report_actionT   s#    ::JJNN r   c                     ||d   z   ||d   z   |d   |d   |d   z
  |z  |z  z   f| _         | j                  r| j                  j                          yy)zTSubclasses should override this method to receive
           progress status updatesr   r   N)rE   rF   rP   )r   currentmaxr   r   s        r   r/   zMirrorTest.report_progressY   sd     !3q6)s1v wqzGAJ'>#&E&OOQ ::JJNN r   c           
      b   | j                  ddd      }t        | j                        }|dkD  rp|j                  dd| j                  t	        j
                  d|dz
           g       |j                  dd| j                  t	        j
                  d|dz
           g       | j                  |D cg c]  }|d   	 c}dt        j                  t        j                  f	      }|D ]   \  }}t        d
|j                  d|       " |sy |d   d   j                  }t        d|z         |S c c}w )N   )r         ?)r      )rT   r   r      r   r   rW   r   )r   r   zmirror: z	 - time: zand the winner is: %s)run_ping_testrK   rL   r1   randomrandintrun_download_testr   r0   printr$   )r   results_pingsizerr   thwinners           r   run_full_testzMirrorTest.run_full_testb   s   ))au)M4<< !8At||FNN1d1f4M'N OPAt||FNN1d1f4M'N OP(()E1!A$)E19.8oo.8oo.? ) @  	=FQQZZ;<	=QZ]++F)F23M *Fs   .D,c           	         |dk(  r| j                   }t               }|D ]  }|j                  |        g }t        d      D ]G  }t        j                  |||| ||      }	| j                  j                  |	       |	j                          I | j                  D ]  }	|	j                           |j                          |d| S )zPerforms 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   )rL   r   putranger   rA   rI   r1   startjoinsort)
r   rL   rT   r   r   r   mr   irc   s
             r   r[   zMirrorTest.run_ping_testw   s    
 d?llGw 	AHHQK	r 	A%%dGQgsKALL"GGI	
  	AFFH	 	q~r   c                 B     fd}|dk(  r j                   }g }|D ]m  } j                  j                         s nQ ||      }|dkD  r|j                  ||g        j	                  |j                  |      dz   t        |      d|       o |j                          |d| S )zPerforms 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                    | j                         d   dj                  }j                  d|z         t        j                         }	 t	        |d      j                  d       t        j                         |z
  S #  Y yxY w)Nr   /zDownloading %s...rY   )timeouti  )get_repo_urlsrH   r%   timer   read)r5   urlrk   r   s      r   test_download_speedz9MirrorTest.run_download_test.<locals>.test_download_speed   sv    #113A6!^^-C2S89IIKEQ',,V4yy{U**s   2B   BNr   r   rZ   )rL   r!   r"   r1   r/   indexrK   rm   )	r   rL   rT   r   r   rx   r   rn   download_times	   `        r   r^   zMirrorTest.run_download_test   s    		 d?llG 	TA<<%%'/2Mq q12  q!1A!5s7|XsS	T 	q~r   c                 b    | j                         | _        | j                  j                          y)z/Complete test exercise, set self.best when doneN)rf   rG   r!   clear)r   s    r   r9   zMirrorTest.run   s"    &&(	r   rO   ))r   d   r;   )NNr:   r;   )r<   r=   r>   r?   r   r   rA   r   r%   r/   rf   r[   r^   r9   r@   r   r   r   r      sE    J+4Y%% +4Z#"
*. AF#<r   r   __main__zdpkg --print-architecturezdists/rr   z/binary-z/Packages.gz))r   queuer   r   ImportErrorru   r   r&   osaptsources.distro
aptsourcesaptsources.sourceslisturllib.requestr   urllib2r\   r   r   r<   distro
get_distroget_sourcessourceslistSourcesListpopenpiperv   striparchrM   r!   rP   source_templatename
componentsrH   list
mirror_setvaluesapprf   r@   r   r   <module>r      si   #"  	  	   & [!! [z z))+F
z--99;<288/0D99;DiooGKKM'',,''2215::I T&00;;BBDE	 17<C W  #""#    s"   E5 F	 5FF	FF