ó
Aa¢Rc           @   sB  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 Z d  d l m Z d  d l	 Z	 d  d l
 Z
 d  d l Z d e  j f d „  ƒ  YZ e d k r>e j j ƒ  Z e j e j ƒ  ƒ e j d ƒ Z e j ƒ  j ƒ  Z d e j j e j j d j e f Z e e j j j ƒ  e ƒ Z e j  ƒ  n  d S(	   iÿÿÿÿN(   t   Timert
   MirrorTestc           B   s‡   e  Z d  Z d e j 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 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 j d ƒ |  _ t	 j
 j |  ƒ 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.7/dist-packages/softwareproperties/MirrorTest.pyR      s    						c      	   C   sz  d  } xm|  j j ƒ  ru|  j j j ƒ  ruy™ |  j j t ƒ } | j } |  j j	 d | ƒ t
 j d | d t d t
 j d t
 j ƒj } x5 t rÂ | j ƒ  } | sª Pn  t j |  j | ƒ } qŽ WWn1 t j k
 rÝ } d  S|  j j	 d | ƒ n Xt j j ƒ  t j d 7_ |  j j t j t j |  j |  j ƒ | re|  j j t  | d ƒ | | g ƒ n  t j j! ƒ  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.7/dist-packages/softwareproperties/MirrorTest.pyt   run   s6    %		
&(   i    i   (   i    i    (   t   __name__t
   __module__t   __doc__R   R2   (    (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyR      s   	c         C   s›   t  j j |  ƒ d |  _ d |  _ | |  _ d  |  _ | |  _ g  |  _	 d t
 _ t  j ƒ  t
 _ t | ƒ t
 _ | |  _ | sŽ t  j ƒ  |  _ n	 | |  _ d  S(   Nt    i    g        (   i    i    g        (   R   R   R   t   actiont   progresst   eventR   t   bestt	   test_filet   threadsR   R&   t   LockR$   t   lenR(   t   mirrorst   EventR   (   R   R?   R;   R9   R   (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyR   7   s    								c         C   s&   | |  _  |  j r" |  j j ƒ  n  d  S(   N(   R7   R9   t   set(   R   t   text(    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyR   H   s    		i    id   c         C   s[   | | d | | d | d | d | d | | f |  _  |  j rW |  j j ƒ  n  d S(   sT   Subclasses should override this method to receive
           progress status updatesi    i   N(   R8   R9   RA   (   R   t   currentt   maxR   R   (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyR'   M   s
    (	c         C   s  |  j  d d d d d d ƒ } t |  j ƒ } | d k r– | j d d |  j t j d	 | d	 ƒ g ƒ | j d d |  j t j d	 | d	 ƒ g ƒ n  |  j t d
 „  | ƒ d d d t j	 t j	 f ƒ} x& | D] \ } } d | j
 | f GHqÐ W| sü d  S| d d	 j
 } d | GH| Sd  S(   NRD   i   R   i    g      à?R   i   i   i   c         S   s   |  d S(   Ni   (    (   t   r(    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyt   <lambda>^   s    s   mirror: %s - time: %ss   and the winner is: %s(   i    g      à?(   i    i   (   g      à?i   (   t   run_ping_testR>   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.7/dist-packages/softwareproperties/MirrorTest.pyt   run_full_testV   s     -0	i   c   
      C   sÊ   | d k r |  j } n  t j ƒ  } x | D] } | j | ƒ q+ Wg  } xL t d ƒ D]> } t j | | | |  | | ƒ }	 |  j j |	 ƒ |	 j	 ƒ  qU Wx |  j D] }	 |	 j
 ƒ  q¡ W| j ƒ  | 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   R<   R)   t   startt   joint   sort(
   R   R?   RD   R   R   R   t   mR   t   iRN   (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyRG   k   s    
c   	         s½   ‡  f d †  } | d k r' ˆ  j } n  g  } xx | D]p } ˆ  j j ƒ  sM Pn  | | ƒ } | d k r{ | j | | g ƒ n  ˆ  j | j | ƒ d t | ƒ d | ƒ q4 W| j ƒ  | 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 |  j  ƒ  d ˆ  j f } ˆ  j d | ƒ t j ƒ  } y0 t j | d d ƒj d ƒ } t j ƒ  | SWn d SXd  S(   Ns   %s/%si    s   Downloading %s...t   timeouti   i  (   t   get_repo_urlsR;   R   t   timet   urllib2t   urlopent   read(   R-   t   urlRT   t   data(   R   (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyt   test_download_speedˆ   s    i    i   g      à?N(   g      à?i   (	   R   R?   R   R   R)   R'   t   indexR>   RV   (	   R   R?   RD   R   R   Ra   R   RW   t   download_time(    (   R   sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyRJ   ‚   s    
-
c         C   s    |  j  ƒ  |  _ |  j j ƒ  d S(   s/   Complete test exercise, set self.best when doneN(   RQ   R:   R   t   clear(   R   (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyR2       s    N(   i    id   (   i    i    (   i    i   (   i    i    (   i    i   (   i    i    (   R3   R4   R5   R   R   R   R   R   R   R'   RQ   RG   RJ   R2   (    (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyR   	   s   ,				t   __main__s   dpkg --print-architectures!   dists/%s/%s/binary-%s/Packages.gzi    (!   R   R"   R[   R	   R   t   tempfilet
   aptsourcest   timeitR    R\   t   socketRH   R   R   R3   t   distrot
   get_distrot   get_sourcest   SourcesListt   ost   popent   pipeR^   t   stript   archt   source_templatet   namet
   componentsR;   t
   mirror_sett   valuest   appRQ   (    (    (    sA   /usr/lib/python2.7/dist-packages/softwareproperties/MirrorTest.pyt   <module>   s$   Hœ	
	