
Nc           @   s   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   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   ElementTreec         C   s   t  j d |   S(   Ns
   python-apt(   t   gettextt   dgettext(   t   s(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   _"   s    t   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  } 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 rG| j! t k rG|  j j" |  | j# | j$  | 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| j% |  j' k r| j! t k r|  j j" |  qn  | j t k r$| j |  j j( k r$| j! t k r>| j% |  j& k r>|  j j" |  q| j! t k rr| j% |  j' k rr|  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   mediaR5   R   R   t   source_codet   source(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   get_sources7   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_UKR   t   .ii   N(   R   t   base_uriR"   R   R1   R$   R3   t   lenR   R!   R0   t	   countriest   ost   patht   existsR    t   getiteratort   attribt   KeyErrorR   R   t   lowerR   t   countryt   country_codet   getenvt   findR#   (   R   t   mirror_templatet   mediumt   fnamet   ett   itt   elmt   descrt   codet   localet   at   zRS   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR9      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   RU   RI   RJ   R   t   rstrip(   R   t   serverRR   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   stripRa   (   t   mir1t   mir2(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   compare_mirrors   s    i   i    s   Main servers   Custom serversN(
   RI   R$   R"   R3   R   R/   R#   Re   R   R   (   R   Rk   t   mirrorsRb   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   R6   R0   R,   R5   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(   R8   R   R-   t
   componentsR(   t   parent_componentRp   t   _enable_component(   R   t   compR5   R<   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,   R5   R3   Rp   (   R<   t   comps_per_dist(   Rx   (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   add_component_only_once1  s    i   R5   s   %sR6   (   R4   R   R   R6   R   R,   R8   t   mapRp   R5   R    R   RI   Rt   R   R/   (   R   Rx   R|   t   sourcesR{   t   comps_per_sdistR   R<   (    (   Rx   s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyRw   /  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(	   R4   R   R   R    R   R5   t   removeRI   R   (   R   Rx   R~   R<   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   disable_componentk  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   (   R0   R5   R,   R   R3   RI   R   (   R<   R0   t   seenRx   (   R   (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   change_server_of_source  s    	 N(   R!   R   R   R-   RH   R   R0   R    (   R   R0   R   t   seen_binaryt   seen_sourceR<   (    (   R   s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   change_server}  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   R=   R   R9   Re   Rn   Rt   Rz   Rw   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 %sRC   s   %ss   / N(   R   RU   RI   RJ   R   R   R   Ra   (   R   Rb   RR   Rc   Rd   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyRe     s    c         C   s   t  j |  d d d  S(   NRV   s    http://ftp.%s.debian.org/debian/(   R   R9   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR9     s    (   R   R   t   __doc__R'   Re   R9   (    (    (    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(   NRV   s$   http://%s.archive.ubuntu.com/ubuntu/(   R   R9   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR9     s    (   R   R   R   R9   (    (    (    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   .0Rd   (    (    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   Rf   RK   t   syst   xml.etree.ElementTreeR    R   t	   ExceptionR   t   objectR   R   R   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   <module>   s   	 x	