

`Q                 @   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 Gd d   d e  Z	 Gd d   d e
  Z Gd d	   d	 e  Z Gd
 d   d e  Z Gd d   d e  Z d d   Z d d   Z d d d d d d  Z d S)    N)ElementTree)gettextc               @   s   e  Z d  Z d S)NoDistroTemplateExceptionN)__name__
__module____qualname__ r   r   3/usr/lib/python3/dist-packages/aptsources/distro.pyr   "   s   r   c               @   s   e  Z d  Z d d   Z d d   Z d d d  Z d d	   Z d
 d   Z d d d d d d d  Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d S)Distributionc             C   s:   | |  _  | |  _ | |  _ | |  _ d |  _ d |  _ d S)z2 Container for distribution specific informations Zdebzdeb-srcN)idcodenamedescriptionreleasebinary_typesource_type)selfr   r   r   r   r   r   r	   __init__(   s    					zDistribution.__init__c             C   s  | |  _  d |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _	 d |  _
 g  |  _ d |  _ d |  _ d |  _ g  |  _ xE |  j  j j D]4 } |  j | j  r | j |  j k r | |  _ Pq W|  j d k rt d |  j |  j f   g  } g  } g  } g  } x+|  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"  n| j j d  r| j r|  j j  |  n | j# |  j$ k r0| j r0|  j j  |  | j! | j"  | j  | j  n | j# |  j$ k r^| j r^|  j j  |  nZ | j# |  j% k r| j r|  j j  |  n+ | j# |  j% k r| j r|  j j  |  | j r(| j |  j j& k r(| j r| j# |  j$ k r|  j j  |  q(| j r5| j# |  j% k r5|  j j  |  q(|  j j  |  q(Wt' |  |  _ t' |  |  _ | j! |  | j! |  t' |  |  _ t' |  |  _	 |  j(   d S)zf
        Find the corresponding template, main and child sources
        for the distribution
        NF z7Error: could not find a distribution template for %s/%szcdrom:))sourceslistsource_templatechild_sourcesmain_sourcesZdisabled_sourcesZcdrom_sourcesZdownload_compsenabled_compscdrom_comps
used_mediaget_source_codesource_code_sourcesdefault_servermain_servernearest_serverused_serversZmatcherZ	templatesis_codenamenameZdistributionr   r   r   listZinvaliddisttemplateZofficialuri
startswithZdisabledappendextendcompstyper   r   Zchildrensetget_mirrors)r   r   r%   Zmediar*   r   r   sourcer   r   r	   get_sources3   s    																	
	
	
	
	


zDistribution.get_sourcesNc             C   s(  |  j  j |  _ x0 |  j D]% } | j d  s |  j j |  q Wt |  j  d k rf |  j |  _	 n |  j d j
 |  _	 i  |  _ d } t j j |  rrt d |  } t | d d  d k r | j | _ | j d  } x | D] } y | j d } Wn t k
 r| j d	 } Yn Xy | j d
 } Wn t k
 rN| j d } Yn Xt j d |  |  j | j   <q Wd |  _ d |  _ t j d d d }	 |	 j d  }
 |	 j d  } | d k rt |	  } |	 |
 d |  j   } | r| | |  _ | |  j k r$|  j | |  _ | |  _ d S)zR
        Provide a set of mirrors where you can get the distribution from
        zcdrom:r   z%/usr/share/xml/iso-codes/iso_3166.xmlfileiterNZiso_3166_entryZcommon_namer"   Zalpha_2_codeZalpha_3_codeiso_3166ZLANGdefaultZen_UK_.   )r   base_urir   r   r'   r    r(   lenr   r   r&   	countriesospathexistsr   getattrZgetiteratorr1   ZattribKeyErrorr   dgettextlowercountrycountry_codegetenvfindr   )r   mirror_templateZmediumfnameZetitZelmZdescrcodeZlocaleazrC   r   r   r	   r-      sL    				zDistribution.get_mirrorsc             C   s   d } | j  d  } | j  d  } | d k rV | d	 k rV | | t d  |  } | |  j k rz t d  |  j | Sd | j d  Sd S)
zu Try to get a human readable name for the main mirror of a country
            Customize for different distributions Nz://z.archive.ubuntu.comr6   zServer for %sz%sz/ r7   r7   )rE   r9   r:   r4   rstrip)r   serverrB   ilr   r   r	   _get_mirror_name   s    zDistribution._get_mirror_namec             C   sY  d d   } g  } t  |  j  d k  sU t  |  j  d k r | |  j d |  j  r | j t d  |  j d g  |  j rU| j |  j |  j  |  j d g  nt  |  j  d k rh| |  j d |  j  rh| j t d  |  j d g  |  j d } |  j r@| | |  j  s@| j |  j |  j  |  j d g  | rU| j |  j |  | d g  n t  |  j  d k rU| j t d  |  j d g  |  j r| j |  j |  j  |  j d g  | j t d  d	 d g  xl |  j D]a } |  j |  | d g } | | |  j  s| | |  j  r8qq| | k r| j |  qW| S)
z- Return a list of used and suggested servers c             S   s"   t  j |  j d  | j d   S)zl Helper function that handles comaprision of mirror urls
                that could contain trailing slashesz/ )rematchstriprL   )Zmir1Zmir2r   r   r	   compare_mirrors   s    z5Distribution.get_server_list.<locals>.compare_mirrorsr6   r   zMain serverTFzCustom serversN)r9   r    r   r(   r4   r   rP   )r   rT   ZmirrorsrM   Zmirror_entryr   r   r	   get_server_list   sB    		"	zDistribution.get_server_listr   c             C   s   | d k r |  j  } | d k r* |  j } | d k rE t |  j  } | d k rZ |  j } |  j j | | | | |  } |  j r | |  j k r |  j j |  j | | | | d | j	 d |  j j j
 |  d d S)z3
        Add distribution specific sources
        Nr0   posr6   )r   r   r#   r   r   r   addr   r   r0   index)r   r+   r&   r$   r*   ZcommentZ
new_sourcer   r   r	   
add_source
  s    					zDistribution.add_sourcec             C   s   t  | g  } xP |  j D]E } x< | j j D]. } | j | k r, | j r, | j | j  q, Wq Wx | D] } |  j |  qi Wd S)z
        Enable a component in all main, child and source code sources
        (excluding cdrom based sources)

        comp:         the component that should be enabled
        N)r,   r   r%   Z
componentsr"   Zparent_componentrW   _enable_component)r   compr*   r.   cr   r   r	   enable_component!  s    zDistribution.enable_componentc       	         s    f d d   } g  } | j  |  j  | j  |  j  i  } i  } xg | D]_ } | j |  j k rK | j | k r t   | | j <x% | j D] } | | j j |  q WqK Wxj |  j	 D]_ } | j |  j
 k r | j | k r t   | | j <x% | j D] } | | j j |  q Wq Wt |  j  d k  rJ|  j d d   g  n? x | D] } | | |  qQWx |  j	 D] } | | |  qrW|  j rt |  j	  d k  r|  j d |  j
 d d   g  n! x |  j	 D] } | | |  qWd  S)Nc                sR   |  j  | k r d S  | |  j  k r* d S|  j j    | |  j  j    d S)z
            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$   r*   r(   rW   )r.   comps_per_dist)r[   r   r	   add_component_only_once3  s    z?Distribution._enable_component.<locals>.add_component_only_oncer6   r*   z%sr+   )r)   r   r   r+   r   r$   r,   r*   rW   r   r   r9   rY   r   )	r   r[   r_   sourcesr^   Zcomps_per_sdistsr\   r.   r   )r[   r	   rZ   1  s:    	#zDistribution._enable_componentc             C   s   g  } | j  |  j  | j  |  j  | j  |  j  | |  j k r[ g  } | j  |  j  xR | D]J } | | j k rb | j j |  t | j  d k  rb |  j j |  qb Wd S)zx
        Disable a component in all main, child and source code sources
        (excluding cdrom based sources)
        r6   N)	r)   r   r   r   r   r*   remover9   r   )r   r[   r`   r.   r   r   r	   disable_componento  s    zDistribution.disable_componentc                s     f d d   } g  } g  } |   _  x!   j D] } | | | |  q1 WxH   j D]= } | j j d k s | j j | j k rU | | | |  qU Wx!   j D] } | | | |  q Wd S)zN 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 |   d  S)Nr6   )r&   r*   r$   rb   r(   r9   r   )r.   r&   seenr[   )r   r   r	   change_server_of_source  s    	 z;Distribution.change_server.<locals>.change_server_of_sourceN)r   r   r   r%   r8   r&   r   )r   r&   re   Zseen_binaryZseen_sourcer.   r   )r   r	   change_server  s    	zDistribution.change_serverc             C   s   | |  j  k r d Sd Sd S)z1 Compare a given name with the release codename. TFN)r   )r   r"   r   r   r	   r!     s    zDistribution.is_codename)r   r   r   r   r/   r-   rP   rU   rY   r]   rZ   rc   rf   r!   r   r   r   r	   r
   &   s   ]93>r
   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	DebianDistributionz+ Class to support specific Debian features c             C   s*   | |  j  k s |  j d k r" d Sd Sd S)z Compare a given name with the release codename and check if
            if it can be used as a synonym for a development releases testingunstableTFN)rh   ri   )r   r   )r   r"   r   r   r	   r!     s    zDebianDistribution.is_codenamec             C   s   d } | j  d  } | j  d  } | d	 k rV | d
 k rV | | t d  |  } | |  j k r t d  t j d |  j | j    j   Sd | j d  Sd S)z_ Try to get a human readable name for the main mirror of a country
            Debian specific Nz://ftp.z.debian.orgr6   zServer for %sr2   z%sz/ r7   r7   )rE   r9   r:   r4   r   r@   rL   )r   rM   rB   rN   rO   r   r   r	   rP     s    z#DebianDistribution._get_mirror_namec             C   s   t  j |  d d d  S)NrF   z http://ftp.%s.debian.org/debian/)r
   r-   )r   r   r   r	   r-     s    zDebianDistribution.get_mirrorsN)r   r   r   __doc__r!   rP   r-   r   r   r   r	   rg     s   rg   c               @   s"   e  Z d  Z d Z d d   Z d S)UbuntuDistributionz+ Class to support specific Ubuntu features c             C   s   t  j |  d d d  S)NrF   z$http://%s.archive.ubuntu.com/ubuntu/)r
   r-   )r   r   r   r	   r-     s    zUbuntuDistribution.get_mirrorsN)r   r   r   rj   r-   r   r   r   r	   rk     s   rk   c               @   s"   e  Z d  Z d Z d d   Z d S)UbuntuRTMDistributionz/ Class to support specific Ubuntu RTM features c             C   s   |  j  j |  _ d  S)N)r   r8   r   )r   r   r   r	   r-     s    z!UbuntuRTMDistribution.get_mirrorsN)r   r   r   rj   r-   r   r   r   r	   rl     s   rl   c              C   s   d d l  m }  m } d d l } d d d d d d	 d
 d i } yX |  d d g d | j   d } | j d  } | j d d   | j d  D  WnH t k
 r } z( | j | j	 k r t
 j d |  WYd d } ~ Xn X| S)z-Call lsb_release --idrc and return a mapping.r   )PopenPIPENCodenameZsidzDistributor IDDebianDescriptionzDebian GNU/Linux unstable (sid)Releaseri   Zlsb_releasez-idrcstdoutzutf-8c             s   s*   |  ]  } d  | k r | j  d   Vq d S)z:	N)split).0rO   r   r   r	   	<genexpr>  s    z_lsb_release.<locals>.<genexpr>
z#lsb_release failed, using defaults:)
subprocessrm   rn   errnocommunicatedecodeupdatert   OSErrorENOENTloggingwarning)rm   rn   ry   resultoutexcr   r   r	   _lsb_release  s    "'$r   c              C   s  d d l  m }  m } d d l } y d d l  m } Wn* t k
 rb t j t j t j	  } Yn Xyl |  d d g d | d | d	 d
 j
   d } x7 | j   D]) } | j d  r | j d d  d Sq WWnH t k
 r} z( | j | j k rt j d |  WYd d } ~ Xn Xd S)z=Get the current channel from system-image-cli -i if possible.r   )rm   rn   N)DEVNULLzsystem-image-cliz-irs   stderrZuniversal_newlinesTz	channel: z: r6   z+system-image-cli failed, using defaults: %s)rx   rm   rn   ry   r   ImportErrorr;   opendevnullO_RDWRrz   
splitlinesr'   rt   r}   r~   r   r   )rm   rn   ry   r   r   rO   r   r   r   r	   _system_image_channel  s$    r   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 } | } | } |  d k r t |  | | |  S|  d k r t |  | | |  S|  d k rt |  | | |  St |  | | |  Sd S)z
    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
    zDistributor IDro   rq   rr   ZUbuntuNzubuntu-rtm/z
Ubuntu-RTM/r6   -r   rp   )r   r   rsplitrt   rk   rl   rg   r
   )r   r   r   r   r   Zchannelr   r   r	   
get_distro  s(    		



	&r   )r   r   rQ   r;   Zxml.etree.ElementTreer   Zapt_pkgr4   	Exceptionr   objectr
   rg   rk   rl   r   r   r   r   r   r   r	   <module>   s    