
WRc           @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m  Z d e f d     YZ	 d e
 f d     YZ d e f d	     YZ d
 e f d     YZ d   Z e e e e d  Z d S(   iN(   t   ElementTree(   t   gettextt   NoDistroTemplateExceptionc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR   "   s   t   Distributionc           B   s}   e  Z d    Z d   Z d d  Z d   Z d   Z d d d d d d  Z d   Z	 d   Z
 d	   Z d
   Z d   Z RS(   c         C   s:   | |  _  | |  _ | |  _ | |  _ d |  _ d |  _ d S(   s2    Container for distribution specific informations t   debs   deb-srcN(   t   idt   codenamet   descriptiont   releaset   binary_typet   source_type(   t   selfR   R   R	   R
   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   __init__(   s    					c         C   s  | |  _  d |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _	 g  |  _
 t |  _ g  |  _ d |  _ d |  _ d |  _ g  |  _ xH |  j  j j D]7 } |  j | j  r | j |  j k r | |  _ Pq q W|  j d k r t d   n  g  } g  } g  } g  } xo|  j  j D]a} | j t k r|  j | j  r| j r| j j t k r|  j | j j  r| j j  d  r| j! t k r|  j j" |  | j# | j$  q| j j  d  r| j! t k r|  j j" |  q| j% |  j& k rA| j! t k rA|  j j" |  | j# | j$  | j" | j  q| j% |  j& k ru| j! t k ru|  j j" |  q| j% |  j' k r| j! t k r|  j j" |  q| j% |  j' k r| j! t k r|  j j" |  qn  | j t k r| j |  j j( k r| j! t k r8| j% |  j& k r8|  j j" |  q| j! t k rl| j% |  j' k rl|  j j" |  q|  j j" |  qqWt) |  |  _ t) |  |  _	 | j# |  | j# |  t) |  |  _ t) |  |  _
 |  j*   d S(   sf   
        Find the corresponding template, main and child sources
        for the distribution
        t    s-   Error: could not find a distribution templates   cdrom:N(+   t   sourceslistt   Nonet   source_templatet   child_sourcest   main_sourcest   disabled_sourcest   cdrom_sourcest   download_compst   enabled_compst   cdrom_compst
   used_mediat   Falset   get_source_codet   source_code_sourcest   default_servert   main_servert   nearest_servert   used_serverst   matchert	   templatest   is_codenamet   namet   distributionR   R   t   listt   invalidt   distt   templatet   officialt   Truet   urit
   startswitht   disabledt   appendt   extendt   compst   typeR   R   t   childrent   sett   get_mirrors(   R   R   R*   t   mediaR2   R   R   t   source(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   get_sources3   s    																		c         C   s  |  j  j |  _ x3 |  j D]( } | j d  s |  j j |  q q Wt |  j  d k ri |  j |  _	 n |  j d j
 |  _	 i  |  _ d } t j j |  rRt d |  } | j d  } x | D] } y | j d } Wn t k
 r | j d } n Xy | j d } Wn t k
 r+| j d	 } n Xt j d
 |  |  j | j   <q Wn  d |  _ d |  _ t j d d d }	 |	 j d  }
 |	 j d  } | d k rt |	  } n  |	 |
 d | !j   } | r| | |  _ n  | |  j k r
|  j | |  _ | |  _ n  d S(   sR   
        Provide a set of mirrors where you can get the distribution from
        s   cdrom:i    s%   /usr/share/xml/iso-codes/iso_3166.xmlt   filet   iso_3166_entryt   common_nameR%   t   alpha_2_codet   alpha_3_codet   iso_3166t   LANGt   defaultt   en_UKt   _t   .ii   N(   R   t   base_uriR   R   R.   R!   R0   t   lenR   R   R-   t	   countriest   ost   patht   existsR    t   getiteratort   attribt   KeyErrorR   t   dgettextt   lowerR   t   countryt   country_codet   getenvt   findR    (   R   t   mirror_templatet   mediumt   fnamet   ett   itt   elmt   descrt   codet   localet   at   zRQ   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR6      sH    				c         C   s   d } | j d  } | j d  } | d k rV | d k rV | | t d  | !} n  | |  j k rz t d  |  j | Sd | j d  Sd S(   su    Try to get a human readable name for the main mirror of a country
            Customize for different distributions s   ://s   .archive.ubuntu.comis   Server for %ss   %ss   / N(   R   RS   RF   RG   RC   t   rstrip(   R   t   serverRP   t   it   l(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   _get_mirror_name   s    c         C   sk  d   } g  } t  |  j  d k  sR t  |  j  d k r | |  j d |  j  r | j t d  |  j t g  |  j rg| j |  j |  j  |  j t g  qgnt  |  j  d k rq| |  j d |  j  rq| j t d  |  j t g  |  j d } |  j rF| | |  j  sF| j |  j |  j  |  j t g  qFn  | rg| j |  j |  | t g  qgn t  |  j  d k rg| j t d  |  j t g  |  j r| j |  j |  j  |  j t g  n  | j t d  d t g  xr |  j D]d } |  j |  | t g } | | |  j  s| | |  j  rDqq| | k r| j |  qqWn  | S(   s-    Return a list of used and suggested servers c         S   s"   t  j |  j d  | j d   S(   sl    Helper function that handles comaprision of mirror urls
                that could contain trailing slashess   / (   t   ret   matcht   stripR_   (   t   mir1t   mir2(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   compare_mirrors   s    i   i    s   Main servers   Custom serversN(
   RF   R!   R   R0   RC   R,   R    Rc   R   R   (   R   Ri   t   mirrorsR`   t   mirror_entry(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   get_server_list   sB    			%	R   c         C   s   | d k r |  j } n  | d k r0 |  j } n  | d k rN t |  j  } n  | d k rf |  j } n  |  j j | | | | |  } |  j t	 k r | |  j k r |  j j |  j
 | | | | d | j d |  j j j |  d n  d S(   s3   
        Add distribution specific sources
        R:   t   posi   N(   R   R   R   R'   R   R   R   t   addR   R,   R   R:   t   index(   R   R3   R-   R)   R2   t   commentt
   new_source(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt
   add_source  s    	c         C   s   t  | g  } xS |  j D]H } x? | j j D]1 } | j | k r, | j r, | j | j  q, q, Wq Wx | D] } |  j |  ql Wd S(   s   
        Enable a component in all main, child and source code sources
        (excluding cdrom based sources)

        comp:         the component that should be enabled
        N(   R5   R   R*   t
   componentsR%   t   parent_componentRn   t   _enable_component(   R   t   compR2   R8   t   c(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   enable_component  s    c            s    f d   } g  } | j  |  j  | j  |  j  i  } i  } x_ | D]W } | j |  j k rH | j | k r t   | | j <n  t | | j j | j	  qH qH Wxb |  j
 D]W } | j |  j k r | j | k r t   | | j <n  t | | j j | j	  q q Wt |  j  d k  r7|  j d d   g  n? x | D] } | | |  q>Wx |  j
 D] } | | |  q_W|  j t k rt |  j
  d k  r|  j d |  j d d   g  qx! |  j
 D] } | | |  qWn  d  S(   Nc            sR   |  j  | k r d S  | |  j  k r* d S|  j j    | |  j  j    d S(   s   
            Check if we already added the component to the repository, since
            a repository could be splitted into different apt lines. If not
            add the component
            N(   R)   R2   R0   Rn   (   R8   t   comps_per_dist(   Rv   (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   add_component_only_once-  s    i   R2   s   %sR3   (   R1   R   R   R3   R   R)   R5   t   mapRn   R2   R   R   RF   Rr   R   R,   (   R   Rv   Rz   t   sourcesRy   t   comps_per_sdistt   sR8   (    (   Rv   s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyRu   +  s6    !!#c         C   s   g  } | j  |  j  | j  |  j  | j  |  j  | |  j k r^ g  } | j  |  j  n  xX | D]P } | | j k re | j j |  t | j  d k  r |  j j |  q qe qe Wd S(   sx   
        Disable a component in all main, child and source code sources
        (excluding cdrom based sources)
        i   N(	   R1   R   R   R   R   R2   t   removeRF   R   (   R   Rv   R|   R8   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   disable_componentg  s    c            s     f d   } g  } g  } |   _  x!   j D] } | | | |  q. WxK   j D]@ } | j j d k s | j j | j k rR | | | |  qR qR Wx!   j D] } | | | |  q Wd S(   sN    Change the server of all distro specific sources to
            a given host c            s   | |  _  x[ |  j D]P } |  j  |  j | g | k rG |  j j |  q | j |  j  |  j | g  q Wt |  j  d k  r   j j |   n  d  S(   Ni   (   R-   R2   R)   R   R0   RF   R   (   R8   R-   t   seenRv   (   R   (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   change_server_of_source}  s    	 N(   R   R   R   R*   RE   R   R-   R   (   R   R-   R   t   seen_binaryt   seen_sourceR8   (    (   R   s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   change_servery  s    	c         C   s   | |  j  k r t St Sd S(   s1    Compare a given name with the release codename. N(   R   R,   R   (   R   R%   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR$     s    N(   R   R   R   R9   R   R6   Rc   Rl   Rr   Rx   Ru   R   R   R$   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR   &   s   		\5		3		<		t   DebianDistributionc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s+    Class to support specific Debian features c         C   s*   | |  j  k s |  j d k r" t St Sd S(   s    Compare a given name with the release codename and check if
            if it can be used as a synonym for a development releases t   testingt   unstableN(   R   R   (   R   R
   R,   R   (   R   R%   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR$     s    c         C   s   d } | j d  } | j d  } | d k rV | d k rV | | t d  | !} n  | |  j k r t d  t j d |  j | j    j   Sd | j d  Sd S(	   s_    Try to get a human readable name for the main mirror of a country
            Debian specific s   ://ftp.s   .debian.orgis   Server for %sR?   s   %ss   / N(   R   RS   RF   RG   RC   R   RN   R_   (   R   R`   RP   Ra   Rb   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyRc     s    c         C   s   t  j |  d d d  S(   NRT   s    http://ftp.%s.debian.org/debian/(   R   R6   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR6     s    (   R   R   t   __doc__R$   Rc   R6   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR     s   		t   UbuntuDistributionc           B   s   e  Z d  Z d   Z RS(   s+    Class to support specific Ubuntu features c         C   s   t  j |  d d d  S(   NRT   s$   http://%s.archive.ubuntu.com/ubuntu/(   R   R6   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR6     s    (   R   R   R   R6   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR     s   c          C   s   d d l  m }  m } d d l } i d d 6d d 6d d	 6d
 d 6} yU |  d d g d | j   d } | j d  } | j d   | j d  D  Wn9 t k
 r } | j | j	 k r t
 j d |  q n X| S(   s-   Call lsb_release --idrc and return a mapping.i(   t   Popent   PIPENt   sidt   Codenamet   Debians   Distributor IDs   Debian GNU/Linux unstable (sid)t   DescriptionR   t   Releaset   lsb_releases   -idrct   stdouti    s   utf-8c         s   s*   |  ]  } d  | k r | j  d   Vq d S(   s   :	N(   t   split(   t   .0Rb   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pys	   <genexpr>  s    s   
s#   lsb_release failed, using defaults:(   t
   subprocessR   R   t   errnot   communicatet   decodet   updateR   t   OSErrort   ENOENTt   loggingt   warn(   R   R   R   t   resultt   outt   exc(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   _lsb_release  s    
"$c         C   s   |  o | o | o | sL t    } | d }  | d } | d } | d } n  |  d k rk t |  | | |  S|  d k r t |  | | |  St |  | | |  Sd S(   s   
    Check the currently used distribution and return the corresponding
    distriubtion class that supports distro specific features.

    If no paramter are given the distro will be auto detected via
    a call to lsb-release
    s   Distributor IDR   R   R   t   UbuntuR   N(   R   R   R   R   (   R   R   R	   R
   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt
   get_distro  s    		


(   R   R   Rd   RH   t   xml.etree.ElementTreeR    t   apt_pkgRC   t	   ExceptionR   t   objectR   R   R   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   <module>   s    x	