U
    &Ÿˆc™\  ã                   @   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m Z G dd„ de	ƒZ
G dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdd„ Zdd„ ZG dd„ dƒZddddg f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                   @   sl   e Zd Zg fdd„Zdd„ Zddd„Zdd	„ Zd
d„ Z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Úis_likeÚbinary_typeÚsource_type)Úselfr   r   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 | _| j jjD ]&}|  |j¡rj|j| jkrj|| _ q’qj| jdkr°td| j| jf ƒ‚g }g }g }g }| j jD �]ž}|j�s |  |j¡�r |j�r |jj�r |  |jj¡�r |j d¡�r6|j�s6| j  |¡ | !|j"¡ nÊ|j d¡�rZ|j�rZ| j  |¡ n¦|j#| j$k�r–|j�s–| j  |¡ | !|j"¡ |  |j¡ nj|j#| j$k�rº|j�rº| j  |¡ nF|j#| j%k�rÞ|j�sÞ| j  |¡ n"|j#| j%k�r |j�r | j  |¡ |jsÈ|j| jj&krÈ|j�s8|j#| j$k�r8| j  |¡ qÈ|j�s\|j#| j%k�r\| j  |¡ qÈ| j  |¡ qÈt'|ƒ| _t'|ƒ| _| !|¡ | !|¡ t'|ƒ| _t'|ƒ| _	|  (¡  dS )zf
        Find the corresponding template, main and child sources
        for the distribution
        NFÚ z7Error: could not find a distribution template for %s/%sú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_sources7   s°    
ÿ

ÿÿ
ÿþýüÿÿÿÿÿÿÿ
ÿ
ÿ





zDistribution.get_sourcesNc              	   C   s˜  | j j| _| jD ]}| d¡s| j |¡ qt| jƒdkrD| j| _	n| jd j
| _	i | _d}tj |¡�rt|d�}t|ddƒdkrŒ|j|_| d¡}|D ]v}z|jd }W n tk
rÊ   |jd	 }Y nX z|jd
 }W n tk
rø   |jd }Y nX t d|¡| j| ¡ < qšd| _d| _tjddd�}	|	 d¡}
|	 d¡}|dk�rRt|	ƒ}|	|
d |…  ¡ }|�rv|| | _|| jk�r”| j| | _|| _dS )zR
        Provide a set of mirrors where you can get the distribution from
        r   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LANGZen_UK)ÚdefaultÚ_Ú.éÿÿÿÿé   )r   Úbase_urir    r   r)   r"   r*   Úlenr   r   r(   Ú	countriesÚosÚpathÚexistsr   ÚgetattrZgetiteratorr3   ZattribÚKeyErrorr   ÚdgettextÚlowerÚcountryÚcountry_codeÚgetenvÚfindr!   )r   Úmirror_templateZmediumÚfnameZetÚitZelmZdescrÚcodeZlocaleÚaÚzrE   r   r   r	   r/   ”   sN    





ÿ



zDistribution.get_mirrorsc                 C   sj   d}|  d¡}|  d¡}|dkr<|dkr<||tdƒ |… }|| jkrXtdƒ| j|  S d| d¡ S dS )zu Try to get a human readable name for the main mirror of a country
            Customize for different distributions Nz://z.archive.ubuntu.comr8   úServer for %sú%sú/ )rG   r;   r<   r6   Úrstrip©r   ÚserverrD   ÚiZlir   r   r	   Ú_get_mirror_nameÍ   s    


zDistribution._get_mirror_namec                 C   s°  dd„ }g }t | jƒdk s:t | jƒdkrt|| jd | jƒrt| tdƒ| jdg¡ | jrp| |  | j¡| jdg¡ �n8t | jƒdkrü|| jd | jƒsü| tdƒ| jdg¡ | jd }| jrà||| jƒsà| |  | j¡| jdg¡ |rú| |  |¡|dg¡ n°t | jƒdk�r¬| tdƒ| jdg¡ | j�rD| |  | j¡| jdg¡ | tdƒd	dg¡ | jD ]L}|  |¡|dg}||| jƒ�s^||| jƒ�r”�q^n||k�r^| |¡ �q^|S )
z- Return a list of used and suggested servers c                 S   s   t  |  d¡| d¡¡S )zl Helper function that handles comaprision of mirror urls
                that could contain trailing slashesrP   )ÚreÚmatchÚstriprQ   )Zmir1Zmir2r   r   r	   Úcompare_mirrorsÞ   s    z5Distribution.get_server_list.<locals>.compare_mirrorsr9   r   zMain serverTFzCustom serversN)r;   r"   r    r*   r6   r!   rU   )r   rY   ZmirrorsrS   Zmirror_entryr   r   r	   Úget_server_listÛ   sV    ÿþ ÿ
ÿ
 ÿ ÿ

ÿ
zDistribution.get_server_listr   c              
   C   s�   |dkr| j }|dkr| j}|dkr.t| jƒ}|dkr<| j}| j |||||¡}| jrŒ|| jkrŒ| jj| j|||||j	| jj 
|¡d d� dS )z3
        Add distribution specific sources
        Nr9   )r2   Úpos)r   r   r%   r   r   r   Úaddr   r   r2   Úindex)r   r-   r(   r&   r,   ZcommentZ
new_sourcer   r   r	   Ú
add_source  s&    
    ýzDistribution.add_sourcec                 C   sX   t |gƒ}| jD ].}|jjD ] }|j|kr|jr| |j¡ qq|D ]}|  |¡ qDd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_componentr\   Ú_enable_component)r   Úcompr,   r0   Úcr   r   r	   Úenable_component%  s    

zDistribution.enable_componentc           	         sZ  ‡ fdd„}g }|  | j¡ |  | j¡ i }i }|D ]B}|j| jkr4|j|krZtƒ ||j< |jD ]}||j  |¡ q`q4| j	D ]B}|j| j
kr~|j|kr¤tƒ ||j< |jD ]}||j  |¡ qªq~t| jƒdk rä| jdˆ  gd� n*|D ]}|||ƒ qè| j	D ]}|||ƒ qþ| j�rVt| j	ƒdk �r>| j| j
dˆ  gd� n| j	D ]}|||ƒ �qDd S )Nc                    s@   | j |krdS ˆ || j  kr dS | 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*   r\   )r0   Úcomps_per_dist©r`   r   r	   Úadd_component_only_once7  s    
z?Distribution._enable_component.<locals>.add_component_only_oncer9   rO   )r,   )r-   r,   )r+   r   r   r-   r   r&   r.   r,   r\   r   r   r;   r^   r   )	r   r`   re   Úsourcesrc   Zcomps_per_sdistÚsra   r0   r   rd   r	   r_   5  s:    






zDistribution._enable_componentc                 C   s€   g }|  | j¡ |  | j¡ |  | j¡ || jkrBg }|  | j¡ |D ]4}||jkrF|j |¡ t|jƒdk rF| j |¡ qFdS )zx
        Disable a component in all main, child and source code sources
        (excluding cdrom based sources)
        r9   N)	r+   r   r   r   r   r,   Úremover;   r   )r   r`   rf   r0   r   r   r	   Údisable_components  s    

zDistribution.disable_componentc                    s€   ‡ fdd„}g }g }|ˆ _ ˆ jD ]}||||ƒ q ˆ jD ]*}|jjdksV|jj|jkr8||||ƒ q8ˆ jD ]}||||ƒ qjdS )zN Change the server of all distro specific sources to
            a given host c                    sd   || _ | jD ]8}| j | j|g|kr0| j |¡ q| | j | j|g¡ qt| jƒdk r`ˆ j | ¡ d S )Nr9   )r(   r,   r&   rh   r*   r;   r   )r0   r(   Úseenr`   ©r   r   r	   Úchange_server_of_source‰  s    
z;Distribution.change_server.<locals>.change_server_of_sourceN)r   r   r   r'   r:   r(   r   )r   r(   rl   Zseen_binaryZseen_sourcer0   r   rk   r	   Úchange_server…  s    

ÿ
zDistribution.change_serverc                 C   s   || j krdS dS dS )z1 Compare a given name with the release codename. TFN)r   ©r   r$   r   r   r	   r#   ¡  s    
zDistribution.is_codename)N)NNNNr   )r   r   r   r   r1   r/   rU   rZ   r^   rb   r_   ri   rm   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S dS d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 )ZtestingÚunstableTFN)r   r   rn   r   r   r	   r#   ¬  s    zDebianDistribution.is_codenamec                 C   sz   d}|  d¡}|  d¡}|dkr<|dkr<||tdƒ |… }|| jkrhtdƒt d| j|  ¡ ¡ ¡  S d| d¡ S dS )	z_ Try to get a human readable name for the main mirror of a country
            Debian specific Nz://ftp.z.debian.orgr8   rN   r4   rO   rP   )rG   r;   r<   r6   r   rB   rQ   rR   r   r   r	   rU   ´  s    



 ÿ
z#DebianDistribution._get_mirror_namec                 C   s   t j| dd� d S )Nz http://ftp.%s.debian.org/debian/©rH   ©r
   r/   rk   r   r   r	   r/   Ã  s     ÿzDebianDistribution.get_mirrorsN)r   r   r   Ú__doc__r#   rU   r/   r   r   r   r	   ro   ©  s   ro   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 )Nz$http://%s.archive.ubuntu.com/ubuntu/rq   rr   rk   r   r   r	   r/   Ë  s     ÿzUbuntuDistribution.get_mirrorsN©r   r   r   rs   r/   r   r   r   r	   rt   È  s   rt   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   r:   r    rk   r   r   r	   r/   Ó  s    z!UbuntuRTMDistribution.get_mirrorsNru   r   r   r   r	   rv   Ð  s   rv   c               
   C   s¨   ddl m} m} ddl}dddddœ}z@| d	d
g|d� ¡ d }| d¡}| dd„ | d¡D ƒ¡ W n< tk
r¢ } z|j|j	kr’t
 d| ¡ W 5 d}~X Y nX |S )z-Call lsb_release --idrc and return a mapping.r   ©ÚPopenÚPIPENZsidÚDebianzDebian GNU/Linux unstable (sid)rp   )ÚCodenameúDistributor IDÚDescriptionÚReleaseZlsb_releasez-idrc)Ústdoutzutf-8c                 s   s    | ]}d |kr|  d ¡V  qdS )z:	N)Úsplit)Ú.0Úlr   r   r	   Ú	<genexpr>â  s      z_lsb_release.<locals>.<genexpr>Ú
z#lsb_release failed, using defaults:)Ú
subprocessrx   ry   ÚerrnoÚcommunicateÚdecodeÚupdater€   ÚOSErrorÚENOENTÚloggingÚwarning)rx   ry   r†   ÚresultÚoutÚexcr   r   r	   Ú_lsb_release×  s    þ
 r‘   c               
   C   sÞ   ddl m} m} ddl}zddl m} W n$ tk
rL   t tjtj	¡}Y nX zN| ddg||dd� 
¡ d }| ¡ D ]$}| d	¡rt| d
d¡d   W S qtW n< tk
rØ } z|j|jkrÈt d| ¡ W 5 d}~X Y nX dS )z=Get the current channel from system-image-cli -i if possible.r   rw   N)ÚDEVNULLzsystem-image-cliz-iT)r   ÚstderrZuniversal_newlinesz	channel: z: r9   z+system-image-cli failed, using defaults: %s)r…   rx   ry   r†   r’   ÚImportErrorr=   ÚopenÚdevnullÚO_RDWRr‡   Ú
splitlinesr)   r€   rŠ   r‹   rŒ   r�   )rx   ry   r†   r’   r�   r‚   r�   r   r   r	   Ú_system_image_channelé  s0      þþ
ÿr™   c                   @   s>   e Zd ZdZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )Ú
_OSReleasez/etc/os-releaseTc                 C   sD   i | _ d| _tj| _tj | j¡s&d S |  ¡  d| _|r@|  	¡  d S )NFT)
rŽ   Úvalidrš   ÚOS_RELEASE_FILEr2   r=   r>   ÚisfileÚparseÚinject_lsb_compat)r   Z
lsb_compatr   r   r	   r     s    z_OSRelease.__init__c                 C   sd   | j d | j d< | j d | j d< | j  d¡| j d< | j d sN| j  d¡| j d< | j  d¡| j d	< d S )
NZIDr|   ZPRETTY_NAMEr}   ZVERSION_CODENAMEr{   ZUBUNTU_CODENAMEZ
VERSION_IDr~   )rŽ   Úgetrk   r   r   r	   rŸ     s    
z_OSRelease.inject_lsb_compatc                 C   sB   t | jdƒ}|D ]$}| ¡ }|s"q| j| dd¡Ž  q| ¡  d S )NÚrú=r9   )r•   r2   rX   Úparse_entryr€   Úclose)r   ÚfÚliner   r   r	   rž     s    z_OSRelease.parsec                 C   s4   |   |¡}|dkr&t|tƒr&| d¡}|| j|< d S )NÚID_LIKEú )Úparse_valueÚ
isinstanceÚstrr€   rŽ   )r   ÚkeyÚvaluer   r   r	   r£   &  s    

z_OSRelease.parse_entryc                 C   s"   t  |¡}t|ƒdkr|d S |S )Nr9   r   )Úshlexr€   r;   )r   r­   Úvaluesr   r   r	   r©   .  s    
z_OSRelease.parse_valueN)T)
r   r   r   ZDEFAULT_OS_RELEASE_FILErœ   r   rŸ   rž   r£   r©   r   r   r   r	   rš      s   

	rš   c           	      C   s   | r|r|r|s¤t ƒ }g }tƒ }|jr,|j}tƒ }|d } |d }|d }|d }| dg ¡}| dkr¤tƒ }|dk	r¤d|kr¤d	} | d
d¡d  dd¡d }|}|}| dkr¼t| ||||ƒS | d	krÔt	| ||||ƒS | dkrìt
| ||||ƒS t| ||||ƒS d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
    r|   r{   r}   r~   r§   ZUbuntuNzubuntu-rtm/z
Ubuntu-RTMú/r9   ú-r   rz   )rš   r‘   r›   rŽ   r    r™   Úrsplitr€   rt   rv   ro   r
   )	r   r   r   r   r   Z
os_releaseZ	os_resultZ
lsb_resultZchannelr   r   r	   Ú
get_distro5  s@    

    ÿr³   )r   rŒ   rV   r®   r=   Zxml.etree.ElementTreer   Úapt_pkgr6   Ú	Exceptionr   Úobjectr
   ro   rt   rv   r‘   r™   rš   r³   r   r   r   r	   Ú<module>   s(      5ÿ