ó
}Îº^c           @   sˆ  d  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 Z d d l Z d d l	 Z	 d d l
 m Z d Z e f Z e a e a d a i  a d a d a e a d a d a d a d a e a e a e ƒ  Z d	 „  Z  d
 „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d d d „  ƒ  YZ) d e j* f d „  ƒ  YZ+ d „  Z, e- d k r„e, ƒ  n  d S(   s|   
B{FastestMirror} is a Yum plugin which sorts each repository's mirrorlist
according to connection speed prior to download.
iÿÿÿÿN(   t	   TYPE_COREs   2.5i   t    i
   i   c         C   s  t  |  d ƒ r |  j d ƒ n  |  j d d d t ƒa |  j d d d t ƒa |  j d d d d ƒa |  j	 d d	 d d
 ƒa
 |  j d d d d ƒa |  j d d d d ƒa |  j	 d d d d ƒa |  j	 d d d d ƒa |  j	 d d d d ƒa |  j d d d t ƒa d S(   sD  
    This function initiliazes the variables required for running B{fastestmirror}
    module. The variables are initiliazed from the main section of the plugin file.

    There are no parameteres for this function. It uses global variables to
    communicate with other functions.

    This function refers:
        - L{get_hostfile_age}

    @param verbose : Verbosity of output.
    @type verbose : Boolean
    @param socket_timeout : The default timeout for a socket connection.
    @type socket_timeout : Integer
    @param hostfilepath : Absolute path to the plugin's cache file.
    @type hostfilepath : String
    @param maxhostfileage : Maximum age of the plugin's cache file.
    @type maxhostfileage : Integer
    @param loadcache : Fastest Mirrors to be loaded from plugin's cache or not.
    @type loadcache : Boolean

    t   registerPackageNames   yum-plugin-fastestmirrort   maint   verboset   defaultt   always_print_best_hostt   socket_timeouti   t   hostfilepatht
   timedhostst   maxhostfileagei
   t
   maxthreadst   excludet   include_onlyt   prefers   no.prefer.mirrort   downgrade_ftpN(   t   hasattrR   t   confBoolt   FalseR   t   TrueR   t   confIntR   t
   confStringR   R
   R   t   NoneR   R   R   R   (   t   conduit(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt	   init_hookH   s    		c         C   s”   t  r0 t  d d k r0 |  j j j d t  a  n  t j j t  ƒ r |  j d d ƒ y t j t  ƒ Wq t	 k
 rŒ } |  j d d | ƒ q Xn  d S(   s”   
    This function cleans the plugin cache file if exists. The function is called
    when C{yum [options] clean [plugins | all ]} is executed.
    i    t   /i   s#   Cleaning up list of fastest mirrorss   Cleanup failed: %sN(
   R   t   _baset   conft   cachedirt   ost   patht   existst   infot   unlinkt	   Exception(   R   t   e(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt
   clean_hookp   s    c         C   s   |  j  d ƒ d j  d ƒ d S(   NR   i   t   @iÿÿÿÿ(   t   split(   t   mirror(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   <lambda>€   s    c         C   sJ   t  j j |  ƒ s7 y t t d ƒ } t SWq7 t SXn  t  j |  t  j ƒ S(   Nt   w(	   R   R   R   t   fileR   R   R   t   accesst   W_OK(   t   fnamet   hostfile(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   _can_write_results‚   s    c         C   s:   d } x- |  D]% } | j  d ƒ r( q n  | d 7} q W| S(   s-    Count the number of urls, which aren't ftp. i    s   ftp:i   (   t
   startswith(   t   urlst   numt   url(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   _len_non_ftpŒ   s    c   
         s_  t  r0 t  d d k r0 ˆ  j j j d t  a  n  t j j t  ƒ rZ t ƒ  t k  rZ t	 a
 n  ˆ  j ƒ  \ } } ˆ  j j j sˆ t t  ƒ rŒ d St r¥ ˆ  j d d ƒ n0 t
 rÅ ˆ  j d d ƒ t ƒ  n ˆ  j d d ƒ i  } ˆ  j ƒ  } g  } xu | j ƒ  D]g } | j t k rqú n  t r6t | j ƒ d k r6qú n  t | j ƒ d k rQqú n  | j | j ƒ qú Wt | ƒ j ƒ  } x»| j ƒ  D]­} | j t k rŸq„n  t rØt | j ƒ d k rØt | j d	 t	 ƒ| _ q„n  t | j ƒ d k róq„n  t | ƒ | k r't | j ƒ j ƒ  | t | ƒ <n  t rb‡  f d
 †  } t | | t | ƒ ƒ | t | ƒ <n; t r‡  f d †  } t | | t | ƒ ƒ | t | ƒ <n  | t | ƒ | _ t | j ƒ rd }	 t  rÔd }	 n  ˆ  j |	 d t | ƒ t! | j d ƒ f ƒ n  d | _" | j# ƒ  | j$ ƒ  t j% | j ƒ q„Wt& rKt' j( d ƒ n  t
 s[t* ƒ  n  d S(   s}  
    This function is called after Yum has initiliazed all the repository information.

    If cache file exists, this function will load the mirror speeds from the file,
    else it will determine the fastest mirrors afresh and write them back to the cache
    file.

    There are no parameteres for this function. It uses global variables to
    communicate with other functions.

    This function refers:
        - L{read_timedhosts()}
        - L{FastestMirror.get_mirrorlist()}
        - L{write_timedhosts()}

    @param loadcache : Fastest Mirrors to be loaded from plugin's cache file or not.
    @type loadcache : Boolean
    i    R   Ni   s"   Checking for new repos for mirrorss*   Loading mirror speeds from cached hostfiles   Determining fastest mirrorsi   t   reversec            sL   t  ‡  f d †  t j d d ƒ j ƒ  ƒ rH ˆ j d d t ˆ  ƒ ƒ t St S(   Nc            s   t  j |  t ˆ  ƒ ƒ S(   N(   t   ret   searcht   host(   t   exp(   R'   (    s%   /usr/lib/yum-plugins/fastestmirror.pyR(   Ù   s    t   ,t    i   s   Including mirror: %s(   t   filterR   t   replaceR&   R    R8   R   R   (   R'   (   R   (   R'   s%   /usr/lib/yum-plugins/fastestmirror.pyt   includeCheckØ   s
    c            sL   t  ‡  f d †  t j d d ƒ j ƒ  ƒ rH ˆ j d d t ˆ  ƒ ƒ t St S(   Nc            s   t  j |  t ˆ  ƒ ƒ S(   N(   R6   R7   R8   (   R9   (   R'   (    s%   /usr/lib/yum-plugins/fastestmirror.pyR(   â   s    R:   R;   i   s   Excluding mirror: %s(   R<   R   R=   R&   R    R8   R   R   (   R'   (   R   (   R'   s%   /usr/lib/yum-plugins/fastestmirror.pyt   excludeChecká   s
    i   s	    * %s: %st   priority(+   R   R   R   R   R   R   R   t   get_hostfile_ageR
   R   t	   loadcachet
   getCmdLinet   cacheR/   t
   done_reposR    t   read_timedhostst   getRepost   listEnabledt   idR   R4   R1   t   lent   extendt   FastestMirrort   get_mirrorlistt   sortedt   strR   R<   R   R   R8   t   failovermethodt   checkt	   setupGrabt   addt   done_sock_timeoutt   sockett   setdefaulttimeoutR   t   write_timedhosts(
   R   t   optst   commandst   repomirrorst   repost   all_urlst   repoR>   R?   t   lvl(    (   R   s%   /usr/lib/yum-plugins/fastestmirror.pyt   prereposetup_hook•   sn    !	
"&&	0	

c          C   sk   yS t  t ƒ }  x6 |  j ƒ  D]( } | j ƒ  \ } } t | ƒ t | <q W|  j ƒ  Wn t k
 rf n Xd S(   sú  
    This function reads the time and hostname from the plugin's cache file and
    store them in C{timedhosts}.

    There are no parameteres for this function. It uses global variables to
    communicate with other functions.

    This function is referred by:
        - L{prereposetup_hook()}

    @param timedhosts : A list of time intervals to reach different hosts
    corresponding to the mirrors. The index of the list are hostnames.
    C{timedhosts[host] = time}.
    @type timedhosts : List
    N(   R*   R   t	   readlinesR&   t   floatR	   t   closet   IOError(   R.   t   lineR8   t   time(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyRF   ù   s    c          C   sa   yI t  t d ƒ }  x) t D]! } |  j d | t | f ƒ q W|  j ƒ  Wn t k
 r\ n Xd S(   së  
    This function writes the plugin's cache file with the entries in the
    C{timedhosts} list.

    There are no parameteres for this function. It uses global variables to
    communicate with other functions.

    This function is referred by:
        - L{prereposetup_hook()}

    @param timedhosts : A list of time intervals to reach different hosts
    corresponding to the mirrors. The index of the list are hostnames.
    C{timedhosts[host] = time}.
    @type timedhosts : List
    R)   s   %s %s
N(   R*   R   R	   t   writeRb   Rc   (   R.   R8   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyRW     s    c          C   s2   t  j  j t j j t ƒ ƒ }  t  j  j ƒ  |  j S(   s¤  
    This function returns the current age of the plugin's cache file.

    There are no parameteres for this function. It uses global variables to
    communicate with other functions.

    This function is referred by:
        - L{init_hook()}

    @param hostfilepath : Absolute path to the plugin's cache file.
    @type hostfilepath : String
    @rtype: Integer
    @return: The age of the plugin's cache file.
    (   t   datetimet   fromtimestampR   R   t   getmtimeR   t   nowt   days(   t	   timestamp(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyRA   ,  s    RL   c           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sÃ   
    This is the helper class of B{fastestmirror} module. This class does
    all the processing of the response time calculation for all the mirrors
    of all the enabled Yum repositories.
    c         C   s   | |  _  i  |  _ g  |  _ d S(   s¾   
        This is the initiliazer function of the B{L{FastestMirror}} class.

        @param mirrorlist : A list of mirrors for an enabled repository.
        @type mirrorlist : List
        N(   t
   mirrorlistt   resultst   threads(   t   selfRm   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   __init__F  s    		c         C   s8   t  |  d ƒ s4 t j ƒ  |  _ t a t j t ƒ n  d  S(   Nt   _results_lock(	   R   t	   threadingt   LockRr   R   RT   RU   RV   R   (   Rp   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt
   _init_lockR  s    c         C   s#   t  |  d ƒ r |  j j ƒ  n  d  S(   NRr   (   R   Rr   t   acquire(   Rp   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   _acquire_lockY  s    c         C   s#   t  |  d ƒ r |  j j ƒ  n  d  S(   NRr   (   R   Rr   t   release(   Rp   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   _release_lock\  s    c         C   s    |  j  ƒ  t sA g  |  j j ƒ  D] \ } } | | f ^ q  } n: g  |  j j ƒ  D]$ \ } } | j d ƒ | | f ^ qQ } | j ƒ  g  | D] } | d ^ qŒ S(   sû  
        This function pings/polls all the mirrors in the list
        C{FastestMirror.mirrorlist} and returns the sorted list of mirrors
        according to the increasing response time of the mirrors.

        This function refers:
            - L{FastestMirror._poll_mirrors()}

        This function is referred by:
            - L{prereposetup_hook()}
            - L{main()}

        @rtype: List
        @return: The list of mirrors sorted according to the increasing
        response time.
        t   ftpiÿÿÿÿ(   t   _poll_mirrorsR   Rn   t   itemsR0   t   sort(   Rp   t   kt   vt   mirrorst   x(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyRM   `  s    
17
c         C   sw  x#|  j  D]} t |  j ƒ t k rY |  j d j ƒ  rL |  j d j ƒ  n  |  j d =n  | j d ƒ rq d } n t | ƒ } | t k rÁ t | } t	 r« d | | f GHn  |  j
 | | | ƒ q
 | d d d t f k rï |  j
 | | d ƒ q
 |  j ƒ  t |  | ƒ } | j ƒ  |  j j | ƒ q
 WxJ t |  j ƒ d k rr|  j d j ƒ  re|  j d j ƒ  n  |  j d =q)Wd S(   s  
        This function uses L{PollThread} class to ping/poll individual mirror
        in parallel.

        This function refers:
            - L{PollThread.run()}

        This function is referred by:
            - L{FastestMirror.get_mirrorlist()}
        i    s   file:s	   127.0.0.1s   %s already timed: %ss   ::1t	   localhostN(   Rm   RJ   Ro   R   t   isAlivet   joinR0   R8   R	   R   t   _add_resultR   Ru   t
   PollThreadt   startt   append(   Rp   R'   t   mhostt   resultt
   pollThread(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyR{   {  s.    	


c         C   sG   |  j  ƒ  t r" d | | f GHn  | |  j | <| t | <|  j ƒ  d S(   s”  
        This function is called by L{PollThread.run()} to add details of a
        mirror in C{FastestMirror.results} dictionary.

        This function is referred by:
            - L{PollThread.run()}

        @param mirror : The mirror that was polled for response time.
        @type mirror : String
        @param host : The hostname of the mirror.
        @type host : String
        @param time : The response time of the mirror.
        @type time : Integer
        @param timedhosts : A list of time intervals to reach different hosts
        corresponding to the mirrors. The index of the list are hostnames.
        @type timedhosts : List
        s    * %s : %f secsN(   Rw   R   Rn   R	   Ry   (   Rp   R'   R8   Re   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyR…   ¤  s    
 
(
   t   __name__t
   __module__t   __doc__Rq   Ru   Rw   Ry   RM   R{   R…   (    (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyRL   ?  s   						)R†   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s•   
    B{PollThread} class implements C{threading.Thread} class. This class
    provides the functionalities to ping/poll the mirrors in parallel.
    c         C   s±   t  j j |  ƒ | |  _ | |  _ t | ƒ |  _ t j | ƒ d } | d k r\ d |  _ nQ | d k rt d |  _ n9 | d k rŒ d |  _ n! | d k r¤ d	 |  _ n	 d
 |  _ d S(   s1  
        It is initiliazer function for B{L{PollThread}} class. This function
        initiliazes the service ports for different webservices.

        @param parent : The parent class.
        @type parent : Class
        @param mirror : The mirror of a repository.
        @type mirror : String
        i    t   httpiP   t   httpsi»  Rz   i   R*   s	   127.0.0.1iþÿÿÿN(   Rs   t   ThreadRq   t   parentR'   R8   t   urlparset   port(   Rp   R’   R'   t   uService(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyRq   Ã  s    
		c         C   sb  y#|  j  t k r= t |  j  } t rd |  j  | f GHqnÉ |  j  d d d t f k ra d } n¥ t j t j t j ƒ } t j |  j  d ƒ } | d k rÇ t	 |  j  | d ƒ |  _
 |  j  |  |  _  n  t j ƒ  } | j |  j  |  j
 f ƒ t j ƒ  | } | j ƒ  |  j j |  j |  j  | ƒ Wn8 t r>d |  j  GHn  |  j j |  j |  j  d	 ƒ n Xd
 S(   sà  
        The C{threading.Thread.run()} function is being overridden here.
        This function pings/polls a mirror and add the details of that
        mirror to the C{FastestMirror.results} dictionary.

        The response time of any mirror is '99999999999' if any exception
        occurs during polling.

        This function refers:
            - L{FastestMirror._add_result()}

        This function is referred by:
            - L{FastestMirror._poll_mirrors()}
        s   %s already timed: %ss	   127.0.0.1s   ::1R‚   i    t   :i   s    * %s : deadl   ÿgí] N(   R8   R	   R   R   RU   t   AF_INETt   SOCK_STREAMt   stringt   findt   intR”   Re   t   connectRb   R’   R…   R'   (   Rp   RŠ   t   sockt   uPortt   time_before(    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   runÝ  s*    	
 (   RŒ   R   RŽ   Rq   R    (    (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyR†   ½  s   	c          C   sg   t  a t t j ƒ d k r; d t j d GHt j d ƒ n  t j d }  d t t |  ƒ j ƒ  ƒ GHd S(   sõ   
    This is the main function for B{fastestmirror} module.

    This function explains the usage of B{fastestmirror} module. Also parses
    the command line arguments.

    This function refers:
        - L{FastestMirror.get_mirrorlist()}
    i   s+   Usage: %s <mirror1> [mirror2] ... [mirrorN]i    iÿÿÿÿs   Result: N(	   R   R   RJ   t   syst   argvt   exitRO   RL   RM   (   Rm   (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyR     s    t   __main__(    (.   RŽ   R   R¡   Re   RU   R™   R“   Rg   Rs   R6   t   yum.pluginsR    t   requires_api_versiont   plugin_typeR   R   R   R   R   R	   R   R
   RB   R   R   R   R   R   R   RT   t   setRE   R   R$   R8   R/   R4   R_   RF   RW   RA   RL   R‘   R†   R   RŒ   (    (    (    s%   /usr/lib/yum-plugins/fastestmirror.pyt   <module>(   sP   			(			
			d			~G	