
Uc           @   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 e f d     YZ d   Z 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 |  j |  j f   n  g  } g  } g  } g  } x4|  j  j D]&} | j r|  j | j  r| j r| j j r|  j | j j  r| j j  d  r| j! r|  j j" |  | j# | j$  q| j j  d  r| j! r|  j j" |  q| j% |  j& k r6| j! r6|  j j" |  | j# | j$  | j" | j  q| j% |  j& k rd| j! rd|  j j" |  q| j% |  j' k r| j! r|  j j" |  q| j% |  j' k r| j! r|  j j" |  qn  | j r.| j |  j j( k r.| j! r| j% |  j& k r|  j j" |  qT| j! rA| j% |  j' k rA|  j j" |  qT|  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    s7   Error: could not find a distribution template for %s/%ss   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   R   t   listt   invalidt   distt   templatet   officialt   urit
   startswitht   disabledt   appendt   extendt   compst   typeR   R   t   childrent   sett   get_mirrors(   R   R   R*   t   mediaR1   R   R   t   source(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   get_sources3   s    																	
	
	
	
	


c         C   s5  |  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 |  ryt d |  } t | d d  d k r | j | _ n  | j d  } x | D] } y | j d } Wn t k
 r | j d } n Xy | j d	 } Wn t k
 rR| 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 r1|  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   itert   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!   R/   t   lenR   R   R,   t	   countriest   ost   patht   existsR    t   getattrR   t   getiteratorR:   t   attribt   KeyErrorR   t   dgettextt   lowert   countryt   country_codet   getenvt   findR    (   R   t   mirror_templatet   mediumt   fnamet   ett   itt   elmt   descrt   codet   localet   at   zRR   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR5      sL    				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   RT   RF   RG   RC   t   rstrip(   R   t   serverRQ   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   R/   RC   t   TrueR    Rd   R   R   (   R   Rj   t   mirrorsRa   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 r | |  j k r |  j j |  j	 | | | | d | j
 d |  j j j |  d n  d S(   s3   
        Add distribution specific sources
        R9   t   posi   N(   R   R   R   R'   R   R   R   t   addR   R   R9   t   index(   R   R2   R,   R)   R1   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(   R4   R   R*   t
   componentsR%   t   parent_componentRp   t   _enable_component(   R   t   compR1   R7   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  } xm | D]e } | j |  j k rH | j | k r t   | | j <n  x( | j D] } | | j j |  q WqH qH Wxp |  j	 D]e } | j |  j
 k r | j | k r t   | | j <n  x( | j D] } | | j j |  q Wq q Wt |  j  d k  rS|  j d d   g  n? x | D] } | | |  qZWx |  j	 D] } | | |  q{W|  j 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)   R1   R/   Rp   (   R7   t   comps_per_dist(   Rx   (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   add_component_only_once3  s    i   R1   s   %sR2   (   R0   R   R   R2   R   R)   R4   R1   Rp   R   R   RF   Rt   R   (	   R   Rx   R|   t   sourcesR{   t   comps_per_sdistt   sRy   R7   (    (   Rx   s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyRw   1  s:    	#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(	   R0   R   R   R   R   R1   t   removeRF   R   (   R   Rx   R}   R7   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   disable_componento  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,   R1   R)   R   R/   RF   R   (   R7   R,   t   seenRx   (   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_sourceR7   (    (   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   Rk   R   (   R   R%   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR$     s    N(   R   R   R   R8   R   R5   Rd   Rn   Rt   Rz   Rw   R   R   R$   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR   &   s   		]9		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
   Rk   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   RT   RF   RG   RC   R   RO   R`   (   R   Ra   RQ   Rb   Rc   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyRd     s    c         C   s   t  j |  d d d  S(   NRU   s    http://ftp.%s.debian.org/debian/(   R   R5   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR5     s    (   R   R   t   __doc__R$   Rd   R5   (    (    (    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(   NRU   s$   http://%s.archive.ubuntu.com/ubuntu/(   R   R5   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR5     s    (   R   R   R   R5   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR     s   t   UbuntuRTMDistributionc           B   s   e  Z d  Z d   Z RS(   s/    Class to support specific Ubuntu RTM features c         C   s   |  j  j |  _ d  S(   N(   R   RE   R   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyR5     s    (   R   R   R   R5   (    (    (    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   .0Rc   (    (    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   warning(   R   R   R   t   resultt   outt   exc(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   _lsb_release  s    
"$c          C   s   d d l  m }  m } d d l } yf |  d d g d | d t j   d } x7 | j   D]) } | j d	  rZ | j d
 d  d SqZ WWn9 t	 k
 r } | j | j
 k r t j d |  q n Xd S(   s=   Get the current channel from system-image-cli -i if possible.i(   R   R   Ns   system-image-clis   -iR   t   universal_newlinesi    s	   channel: s   : i   s+   system-image-cli failed, using defaults: %s(   R   R   R   R   Rk   R   t
   splitlinesR-   R   R   R   R   R   R   (   R   R   R   R   Rc   R   (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   _system_image_channel  s    c         C   s+  |  o | o | o | s t    } | d }  | d } | d } | d } |  d k r t   } | d k	 r d | k r d }  | j d d	  d	 j d
 d	  d } | } | } q q n  |  d k r t |  | | |  S|  d k r 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   Ubuntus   ubuntu-rtm/s
   Ubuntu-RTMt   /i   t   -i    R   N(	   R   R   R   t   rsplitR   R   R   R   R   (   R   R   R	   R
   R   t   channel(    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt
   get_distro  s(    		



	&(   R   R   Re   RH   t   xml.etree.ElementTreeR    t   apt_pkgRC   t	   ExceptionR   t   objectR   R   R   R   R   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/aptsources/distro.pyt   <module>   s    		