Ñò
ÆKc           @   sD  d  d k  Z  d  d k Z d  d k Z d  d k Z d  d k Z d  d k Z d  d k Z d  d k l Z d  d k	 Z	 d  d k
 Z
 d  d k Z d e  i f d „  ƒ  YZ e d j o’ e i i ƒ  Z e i e i ƒ  ƒ e i d ƒ Z e i ƒ  i ƒ  Z d e i i e i i d i e f Z e e i i i ƒ  e ƒ Z e i  ƒ  n d S(	   iÿÿÿÿN(   t   Timert
   MirrorTestc           B   s~   e  Z d  Z d e i f d „  ƒ  YZ d d „ Z d „  Z d d d „ Z	 d „  Z
 d d d d d
 „ Z d d d d d „ Z RS(   s@   Determines the best mirrors by perfoming ping and download test.t
   PingWorkerc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s¢   Use the command line command ping to determine the server's
           response time. Using multiple threads allows to run several
           test simultaneously.i    i   c         C   s\   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ t i d ƒ |  _ t	 i
 i |  ƒ d  S(   Ns   ^rtt .* = [\.\d]+/([\.\d]+)/.*(   t   borderst   modt   parentt   idt   jobst   resultst   ret   compilet   match_resultt	   threadingt   Threadt   __init__(   t   selfR   R   R   R   R   R   (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyR      s    						c         C   s…  d  } xx|  i i ƒ  of|  i i i ƒ  oSy |  i i t ƒ } | i } |  i i	 d | ƒ t
 i d | d t d t
 i d t
 i ƒi } x9 t o1 | i ƒ  } | p Pn t i |  i | ƒ } q WWn3 t i j
 o
 } d  S|  i i	 d | ƒ n Xt i i ƒ  t i d 7_ |  i i t i t i |  i |  i ƒ | o' |  i i t  | d ƒ | | g ƒ n t i i! ƒ  q	 Wd  S(	   Ns   Pinging %s...s   ping -q -c 2 -W 1 -i 0.5 %st   shellt   stdoutt   stderrs   Skipping %si   i    ("   t   NoneR   t   emptyR   t   runningt   isSett   gett   Falset   hostnamet   report_actiont
   subprocesst   Popent   Truet   PIPEt   STDOUTR   t   readlineR	   t   findallR   t   Queuet   EmptyR   t   completed_lockt   acquiret	   completedt   report_progresst   todoR   R   R   t   appendt   floatt   release(   R   t   resultt   mirrort   hostt   commandot   linet   e(    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyt   run   s:     $	 
'(   i    i   (   i    i    (   t   __name__t
   __module__t   __doc__R   R2   (    (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyR      s   	c         C   sy   t  i i |  ƒ | |  _ g  |  _ d t _ t  i ƒ  t _ t	 | ƒ t _
 | |  _ | p t  i ƒ  |  _ n
 | |  _ d  S(   Ni    (   R   R   R   t	   test_filet   threadsR   R&   t   LockR$   t   lenR(   t   mirrorst   EventR   (   R   R:   R6   R   (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyR   7   s    				c         C   s	   | GHd S(   sS   Subclasses should override this method to receive
           action message updatesN(    (   R   t   text(    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyR   D   s    i    id   c         C   s#   d | | d | | d f GHd S(   sT   Subclasses should override this method to receive
           progress status updatess   Completed %s of %si    i   N(    (   R   t   currentt   maxR   R   (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyR'   I   s    c         C   s"  |  i  d d d d d d ƒ } t |  i ƒ } | d j o^ | i d d |  i t i d	 | d	 ƒ g ƒ | i d d |  i t i d	 | d	 ƒ g ƒ n |  i t d
 „  | ƒ d d d t i	 t i	 f ƒ} x& | D] \ } } d | i
 | f GHqÒ W| p d  S| d d	 i
 } d | GH| Sd  S(   NR>   i   R   i    g      à?R   i   i   i   c         S   s   |  d  S(   i   (    (   t   r(    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyt   <lambda>V   s    s   mirror: %s - time: %ss   and the winner is: %s(   i    g      à?(   i    i   (   g      à?i   (   t   run_ping_testR9   R:   R)   t   randomt   randintt   run_download_testt   mapR   R(   R   R   (   R   t   results_pingt   sizeR   t   tt   ht   winner(    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyt   run_full_testN   s"    -1 	i   c   
   
   C   sÌ   | d j o |  i } n t i ƒ  } x | D] } | i | ƒ q- Wg  } xL t d ƒ D]> } t i | | | |  | | ƒ }	 |  i i |	 ƒ |	 i	 ƒ  qW Wx |  i D] }	 |	 i
 ƒ  q£ W| i ƒ  | d | !S(   sõ   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.i   i    N(   R   R:   R"   t   putt   rangeR   R   R7   R)   t   startt   joint   sort(
   R   R:   R>   R   R   R   t   mR   t   iRH   (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyRA   c   s"      
 
c   	         sÃ   ‡  f d †  } | d j o ˆ  i } n g  } x| | D]t } ˆ  i i ƒ  p Pn | | ƒ } | d j o | i | | g ƒ n ˆ  i | i | ƒ d t | ƒ d | ƒ q6 W| i ƒ  | d | !S(   sù   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            sy   d |  i  ƒ  d ˆ  i f } ˆ  i d | ƒ t i ƒ  } y0 t i | d d ƒi d ƒ } t i ƒ  | SWn d SXd  S(   Ns   %s/%si    s   Downloading %s...t   timeouti   i  (   t   get_repo_urlsR6   R   t   timet   urllib2t   urlopent   read(   R-   t   urlRN   t   data(   R   (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyt   test_download_speed€   s    i    i   g      à?N(   g      à?i   (	   R   R:   R   R   R)   R'   t   indexR9   RP   (	   R   R:   R>   R   R   R[   R   RQ   t   download_time(    (   R   sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyRD   z   s    
 -
N(   i    id   (   i    i    (   i    i   (   i    i    (   i    i   (   i    i    (   R3   R4   R5   R   R   R   R   R   R   R'   RK   RA   RD   (    (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyR   	   s   ,			t   __main__s   dpkg --print-architectures!   dists/%s/%s/binary-%s/Packages.gzi    (!   R   R"   RU   R	   R   t   tempfilet
   aptsourcest   timeitR    RV   t   socketRB   R   R   R3   t   distrot
   get_distrot   get_sourcest   SourcesListt   ost   popent   pipeRX   t   stript   archt   source_templatet   namet
   componentsR6   t
   mirror_sett   valuest   appRK   (    (    (    sA   /usr/lib/python2.6/dist-packages/softwareproperties/MirrorTest.pyt   <module>   s$   H	
	