o
    4ñÕfÓ\  ã                   @   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   ú4/usr/lib/python3/dist-packages/DistUpgrade/distro.pyr   %   s    r   c                   @   sp   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 Ú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¡rH|j| jkrH|| _ nq5| jdu rXt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¡r“|js“| j  |¡ | !|j"¡ n[|j d¡r£|jr£| j  |¡ nK|j#| j$kr¿|js¿| j  |¡ | !|j"¡ |  |j¡ n/|j#| j$krÏ|jrÏ| j  |¡ n|j#| j%krß|jsß| j  |¡ n|j#| j%krî|jrî| j  |¡ |j�s$|j| jj&v �r$|j�s|j#| j$k�r| j  |¡ qd|j�s|j#| j%k�r| j  |¡ qd| j  |¡ qdt'|ƒ| _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_sourcesÚdisabled_sourcesÚcdrom_sourcesÚdownload_compsÚenabled_compsÚcdrom_compsÚ
used_mediaÚget_source_codeÚsource_code_sourcesÚdefault_serverÚmain_serverÚnearest_serverÚused_serversÚmatcherÚ	templatesÚis_codenameÚnameÚdistributionr   r   r   ÚlistÚinvalidÚdistÚtemplateÚofficialÚuriÚ
startswithÚdisabledÚappendÚextendÚcompsÚtyper   r   ÚchildrenÚsetÚget_mirrors)r   r   r/   Úmediar6   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r"| j| _	n| jd j
| _	i | _d}tj |¡r†t|d�}t|ddƒdu rE|j|_| d¡}|D ]9}z|jd }W n tyc   |jd	 }Y nw z|jd
 }W n tyy   |jd }Y nw t d|¡| j| ¡ < qLd| _d| _tjddd�}	|	 d¡}
|	 d¡}|dkr¥t|	ƒ}|	|
d |…  ¡ }|r¶|| | _|| jv rÆ| j| | _|| _dS 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ÚiterNÚiso_3166_entryÚcommon_namer*   Úalpha_2_codeÚalpha_3_codeÚiso_3166ÚLANGÚen_UK)ÚdefaultÚ_Ú.éÿÿÿÿé   )r   Úbase_urir$   r    r2   r&   r4   Úlenr   r#   r1   Ú	countriesÚosÚpathÚexistsr   ÚgetattrÚgetiteratorr?   ÚattribÚKeyErrorr   ÚdgettextÚlowerÚcountryÚcountry_codeÚgetenvÚfindr%   )r   Úmirror_templateÚmediumÚfnameÚetÚitÚelmÚdescrÚcodeÚlocaleÚaÚzrY   r   r   r	   r:   ”   sV   


€


ÿÿÿ




þzDistribution.get_mirrorsc                 C   sf   d}|  d¡}|  d¡}|dkr|dkr||tdƒ |… }|| jv r,tdƒ| j|  S d| 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.comrJ   úServer for %sú%sú/ )r[   rM   rN   rH   Úrstrip©r   ÚserverrX   ÚiÚ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r:|| jd | jƒr:| tdƒ| jdg¡ | jr8| |  | j¡| jdg¡ |S t | jƒdkr|| jd | jƒs| tdƒ| jdg¡ | jd }| jrp||| jƒsp| |  | j¡| jdg¡ |r}| |  |¡|dg¡ |S t | jƒdkrÏ| tdƒ| jdg¡ | jr¡| |  | j¡| jdg¡ | tdƒd	dg¡ | jD ] }|  |¡|dg}||| jƒsÄ||| jƒrÅq®||v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 slashesri   )ÚreÚmatchÚstriprj   )Úmir1Úmir2r   r   r	   Úcompare_mirrorsÞ   s   z5Distribution.get_server_list.<locals>.compare_mirrorsrK   r   zMain serverTFzCustom serversN)rM   r&   r$   r4   rH   r%   ro   )r   ru   Úmirrorsrl   Úmirror_entryr   r   r	   Úget_server_listÛ   sT   ÿÿ!áÿ
ÿïÿ

ÿ
€zDistribution.get_server_listr   c              
   C   s˜   |du r| j }|du r| j}|du rt| jƒ}|du r| j}| j |||||¡}| jrH|| jkrJ| jj| j|||||j	| jj 
|¡d d� dS dS dS )z3
        Add distribution specific sources
        NrK   )r>   Úpos)r#   r   r,   r   r   r   Úaddr!   r   r>   Úindex)r   r7   r1   r.   r6   ÚcommentÚ
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 ]}|  |¡ q"d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)r9   r   r/   Ú
componentsr*   Úparent_componentrz   Ú_enable_component)r   Úcompr6   r<   Úcr   r   r	   Úenable_component%  s   

€þÿzDistribution.enable_componentc           	         sZ  ‡ fdd„}g }|  | j¡ |  | j¡ i }i }|D ]!}|j| jkr;|j|vr-tƒ ||j< |jD ]
}||j  |¡ q0q| j	D ]!}|j| j
kr`|j|vrRtƒ ||j< |jD ]
}||j  |¡ qUq?t| jƒdk rr| jdˆ  gd� n|D ]}|||ƒ qt| j	D ]}|||ƒ q| jr©t| j	ƒdk rž| j| j
dˆ  gd� d S | j	D ]	}|||ƒ q¡d S d S )Nc                    s@   | j |vrdS ˆ || j  v 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.   r6   r4   rz   )r<   Úcomps_per_dist©r‚   r   r	   Úadd_component_only_once7  s   
z?Distribution._enable_component.<locals>.add_component_only_oncerK   rh   )r6   )r7   r6   )r5   r   r   r7   r   r.   r9   r6   rz   r"   r   rM   r~   r!   )	r   r‚   r‡   Úsourcesr…   Úcomps_per_sdistÚsrƒ   r<   r   r†   r	   r�   5  sB   

€


€

ùzDistribution._enable_componentc                 C   s€   g }|  | j¡ |  | j¡ |  | j¡ || jv r!g }|  | j¡ |D ]}||jv r=|j |¡ t|jƒdk r=| j |¡ q#dS )zx
        Disable a component in all main, child and source code sources
        (excluding cdrom based sources)
        rK   N)	r5   r   r   r"   r   r6   ÚremoverM   r   )r   r‚   rˆ   r<   r   r   r	   Údisable_components  s   

€üzDistribution.disable_componentc                    s€   ‡ fdd„}g }g }|ˆ _ ˆ jD ]}||||ƒ qˆ jD ]}|jjdu s+|jj|jkr1||||ƒ qˆ jD ]}||||ƒ q5dS )zN Change the server of all distro specific sources to
            a given host c                    sh   || _ | jD ]}| j | j|g|v r| j |¡ q| | j | j|g¡ qt| jƒdk r2ˆ j | ¡ d S d S )NrK   )r1   r6   r.   r‹   r4   rM   r   )r<   r1   Úseenr‚   ©r   r   r	   Úchange_server_of_source‰  s   
ÿz;Distribution.change_server.<locals>.change_server_of_sourceN)r#   r   r   r/   rL   r1   r"   )r   r1   r�   Úseen_binaryÚseen_sourcer<   r   rŽ   r	   Úchange_server…  s   

€
ÿzDistribution.change_serverc                 C   s   || j krdS dS )z1 Compare a given name with the release codename. TF)r   ©r   r*   r   r   r	   r)   ¡  s   
zDistribution.is_codename©N)NNNNr   )r   r   r   r   r=   r:   ro   rx   r~   r„   r�   rŒ   r’   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v r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 )ÚtestingÚunstableTF)r   r   r“   r   r   r	   r)   ¬  s   zDebianDistribution.is_codenamec                 C   sv   d}|  d¡}|  d¡}|dkr|dkr||tdƒ |… }|| jv r4tdƒt d| j|  ¡ ¡ ¡  S d| d¡ S )	z_ Try to get a human readable name for the main mirror of a country
            Debian specific Nz://ftp.z.debian.orgrJ   rg   rD   rh   ri   )r[   rM   rN   rH   r   rV   rj   rk   r   r   r	   ro   ´  s   



ÿÿz#DebianDistribution._get_mirror_namec                 C   ó   t j| dd� d S )Nz http://ftp.%s.debian.org/debian/©r\   ©r
   r:   rŽ   r   r   r	   r:   Ã  ó   
ÿzDebianDistribution.get_mirrorsN)r   r   r   Ú__doc__r)   ro   r:   r   r   r   r	   r•   ©  s
    r•   c                   @   ó   e Zd ZdZdd„ ZdS )ÚUbuntuDistributionz+ Class to support specific Ubuntu features c                 C   r˜   )Nz$http://%s.archive.ubuntu.com/ubuntu/r™   rš   rŽ   r   r   r	   r:   Ë  r›   zUbuntuDistribution.get_mirrorsN©r   r   r   rœ   r:   r   r   r   r	   rž   È  ó    rž   c                   @   r�   )ÚUbuntuRTMDistributionz/ Class to support specific Ubuntu RTM features c                 C   s   | j j| _d S r”   )r   rL   r$   rŽ   r   r   r	   r:   Ó  s   z!UbuntuRTMDistribution.get_mirrorsNrŸ   r   r   r   r	   r¡   Ð  r    r¡   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 |S  ty[ } z|j|j	krPt
 d| ¡ W Y d}~|S W Y d}~|S d}~ww )z-Call lsb_release --idrc and return a mapping.r   ©ÚPopenÚPIPENÚsidÚDebianzDebian GNU/Linux unstable (sid)r—   )ÚCodenameúDistributor IDÚDescriptionÚReleaseÚlsb_releasez-idrc)Ústdoutzutf-8c                 s   s"   � | ]}d |v r|  d ¡V  qdS )z:	N)Úsplit)Ú.0Úliner   r   r	   Ú	<genexpr>â  s   € ÿÿz_lsb_release.<locals>.<genexpr>Ú
z#lsb_release failed, using defaults:)Ú
subprocessr£   r¤   ÚerrnoÚcommunicateÚdecodeÚupdater­   ÚOSErrorÚENOENTÚloggingÚwarning)r£   r¤   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y%   t tjtj	¡}Y nw z(| ddg||dd� 
¡ d }| ¡ D ]}| d	¡rK| d
d¡d   W S q9W dS  tyu } z|j|jkrjt d| ¡ W Y d}~dS W Y d}~dS d}~ww )z=Get the current channel from system-image-cli -i if possible.r   r¢   N)ÚDEVNULLzsystem-image-cliz-iT)r¬   ÚstderrÚuniversal_newlinesz	channel: z: rK   z+system-image-cli failed, using defaults: %s)r²   r£   r¤   r³   r¿   ÚImportErrorrO   ÚopenÚdevnullÚO_RDWRr´   Ú
splitlinesr2   r­   r·   r¸   r¹   rº   )r£   r¤   r³   r¿   r¼   r¯   r½   r   r   r	   Ú_system_image_channelê  s<   þ
þþ
ÿÿüÿ
ý€ü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   sH   i | _ d| _tj| _tj | j¡sd S |  ¡  d| _|r"|  	¡  d S d S )NFT)
r»   ÚvalidrÈ   ÚOS_RELEASE_FILEr>   rO   rP   ÚisfileÚparseÚinject_lsb_compat)r   Ú
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 s'| j  d¡| j d< | j  d¡| j d	< d S )
NÚIDr¨   ÚPRETTY_NAMEr©   ÚVERSION_CODENAMEr§   ÚUBUNTU_CODENAMEÚ
VERSION_IDrª   )r»   ÚgetrŽ   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ú=rK   )rÃ   r>   rr   Úparse_entryr­   Úclose)r   Úfr¯   r   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 )NrK   r   )Úshlexr­   rM   )r   rà   Úvaluesr   r   r	   rÜ   /  s   
z_OSRelease.parse_valueN)T)
r   r   r   ÚDEFAULT_OS_RELEASE_FILErÊ   r   rÍ   rÌ   r×   rÜ   r   r   r   r	   rÈ     s    

	rÈ   c           	      C   sü   | r|r|r|sRt ƒ }g }tƒ }|jr|j}tƒ }|d } |d }|d }|d }| dg ¡}| dkrRtƒ }|durRd|v rRd	} | d
d¡d  dd¡d }|}|}| dkr^t| ||||ƒS | d	krjt	| ||||ƒS | dkrvt
| ||||ƒS t| ||||ƒ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Ú   ÚUbuntuNzubuntu-rtm/z
Ubuntu-RTMú/rK   ú-r   r¦   )rÈ   r¾   rÉ   r»   rÔ   rÇ   Úrsplitr­   rž   r¡   r•   r
   )	r   r   r   r   r   Ú
os_releaseÚ	os_resultÚ
lsb_resultÚchannelr   r   r	   Ú
get_distro6  s8   


ÿrì   )r   r¹   rp   rá   rO   Úxml.etree.ElementTreer   Úapt_pkgrH   Ú	Exceptionr   Úobjectr
   r•   rž   r¡   r¾   rÇ   rÈ   rì   r   r   r   r	   Ú<module>   s*      5ÿ