
    xkdk]                     
   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e	          Z
 G d de          Z G d d	e          Z G d
 de          Z G d de          Zd Zd Z G d d          Zddddg fdZdS )    N)ElementTree)gettextc                       e Zd ZdS )NoDistroTemplateExceptionN)__name__
__module____qualname__     3/usr/lib/python3/dist-packages/aptsources/distro.pyr   r   %   s        Dr   r   c                   X    e Zd Zg fdZd ZddZd Zd ZddZd	 Z	d
 Z
d Zd Zd ZdS )Distributionc                 h    || _         || _        || _        || _        || _        d| _        d| _        dS )z0Container for distribution specific informationsdebzdeb-srcN)idcodenamedescriptionreleaseis_likebinary_typesource_type)selfr   r   r   r   r   s         r   __init__zDistribution.__init__*   s>      & $r   c                    || _         d| _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _	        d| _
        g | _        d| _        d| _        d| _        g | _        | j         j        j        D ]5}|                     |j                  r|j        | j        k    r	|| _         n6| j        t-          d| j        d| j                  g }g }g }g }| j         j        D ]}|j        s|                     |j                  r|j        r|j        j        r|                     |j        j                  r|j                            d          r=|j        s6| j                             |           |!                    |j"                   n7|j                            d          r"|j        r| j                             |           n|j#        | j$        k    rV|j        sO| j                             |           |!                    |j"                   |                     |j                   n|j#        | j$        k    r"|j        r| j                             |           nc|j#        | j%        k    r"|j        s| j                             |           n1|j#        | j%        k    r!|j        r| j                             |           |j        s|j        | j        j&        v r|j        s,|j#        | j$        k    r| j                             |           6|j        s,|j#        | j%        k    r| j                             |           i| j                             |           tO          |          | _        tO          |          | _        |!                    |           |!                    |           tO          |          | _        tO          |          | _	        | (                                 dS )zf
        Find the corresponding template, main and child sources
        for the distribution
        NF z2Error: could not find a distribution template for /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   r6   mediar=   r&   r%   sources           r   get_sourceszDistribution.get_sources6   s    '# " $#%  !  (0: 	 	H.. 83HDG3S3S (0$'++777DMM+   &+ 	9 	9FN9$$V[119 O9 O,	9
 $$V_%9::9 :((22 96? 9&--f555&&v|4444Z**844 9 9&--f5555[D$444V_4%,,V444LL...LL,,,,[D$4444)008888[D$444V_4,33F;;;;[D$4444)00888> 9fo9M9V&V&V 96;$:J+J+J&--f5555 9V[D<L-L-L,33F;;;;)00888!%jj{++U###[))) //e**r   Nc                 b   | j         j        | _        | j        D ]1}|                    d          s| j                            |           2t          | j                  dk    r| j        | _	        n| j        d         j
        | _	        i | _        d}t          j                            |          rt          |          }t!          |dd          |j        |_        |                    d          }|D ]}	 |j        d         }n# t(          $ r |j        d	         }Y nw xY w	 |j        d
         }n# t(          $ r |j        d         }Y nw xY wt+          j        d|          | j        |                                <   d| _        d| _        t          j        dd          }	|	                    d          }
|	                    d          }|dk    rt          |	          }|	|
dz   |                                         }|r
||z  | _        || 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_namer1   alpha_2_codealpha_3_codeiso_3166LANGen_UK)default_.   )r   base_urir+   r'   r9   r-   r;   lenr!   r*   r8   	countriesospathexistsr   getattrgetiteratorrG   attribKeyErrorr   dgettextlowercountrycountry_codegetenvfindr,   )r   mirror_templatemediumfnameetitelmdescrcodelocaleazra   s                r   rA   zDistribution.get_mirrors   s_   
  /8 o 	1 	1F$$X.. 1!((000t !!Q&&"&"2D"&"3A"6":D 77>>%   	S%(((B r64((0.)**B 	S 	S/J}5EE / / /Jv.EEE/6:n5DD 6 6 6:n5DDD6/6/?
E/R/Rtzz||,,  67333KKKK77FAa!eai(..00 	A"1L"@D4>))>,7DL ,D *)s$   6DDD"D00E
	E
c                 "   d}|                     d          }|                     d          }|dk    r |dk    r||t          d          z   |         }|| j        v rt          d          | j        |         z  S d|                    d          z  S )zoTry to get a human readable name for the main mirror of a country
        Customize for different distributionsNz://z.archive.ubuntu.comrR   Server for %s%s/ )rc   rU   rV   rP   rstripr   serverr`   ilis        r   _get_mirror_namezDistribution._get_mirror_name   s     KK[[.//77rRxxQU^b01Gdn$$_%%w(???&------r   c                 H   d }g }t          | j                  dk     s4t          | j                  dk    r || j        d         | j                  rh|                    t	          d          | j        dg           | j        r5|                    |                     | j                  | j        dg           nt          | j                  dk    r || j        d         | j                  s|                    t	          d          | j        dg           | j        d         }| j        rF ||| j                  s5|                    |                     | j                  | j        dg           |r+|                    |                     |          |dg           nt          | j                  dk    r|                    t	          d          | j        dg           | j        r5|                    |                     | j                  | j        dg           |                    t	          d          ddg           | j        D ]V}|                     |          |dg} ||| j                  s ||| j                  r=||vr|                    |           W|S )	z+Return a list of used and suggested serversc                 x    t          j        |                     d          |                    d                    S )zgHelper function that handles comaprision of mirror urls
            that could contain trailing slashesrr   )rematchstriprs   )mir1mir2s     r   compare_mirrorsz5Distribution.get_server_list.<locals>.compare_mirrors   s.     8DJJt,,dkk$.?.?@@@r   rS   r   zMain serverTFzCustom serversN)rU   r-   r+   r;   rP   r,   rx   )r   r   mirrorsru   mirror_entrys        r   get_server_listzDistribution.get_server_list   s   	A 	A 	A t !!A%%!""a'' 1! 4d6FGG ( NNAm,,d.>EFFF" --d.ABB+   "##q((a $"22
 2
( NNAm,,d.>FGGG&q)F " &vt/BCC NN 11$2EFF /!    N 5 5f = =vtLMMM"##a'' NNAm,,d.>FGGG" --d.ABB+   NNA.//t<===+ 1 1 $ 5 5f = =vuM"?64+>?? 1??D,D D 1 !00NN<000r   r   c                 f   || j         }|| j        }|t          | j                  }|| j        }| j                            |||||          }| j        rX|| j        k    rO| j                            | j        |||||j	        | j        j        
                    |          dz              dS dS dS )z3
        Add distribution specific sources
        NrS   )rF   pos)r*   r   r3   r%   r   r   addr(   r   rF   index)r   r>   r8   r5   r=   comment
new_sources          r   
add_sourcezDistribution.add_source  s     ;%C<=D=+,,E<#D%))$T5'JJ
  		DD,<$<$<   _$)//
;;a? !     		 		$<$<r   c                     t          |g          }| j        D ]=}|j        j        D ].}|j        |k    r!|j        r|                    |j                   />|D ]}|                     |           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)r@   r!   r6   
componentsr1   parent_componentr   _enable_component)r   compr=   rC   cs        r   enable_componentzDistribution.enable_component2  s     TF' 	2 	2F_/ 2 26T>>a&8>IIa01112  	& 	&A""1%%%%	& 	&r   c                 n   fd}g }|                     | j                   |                     | j                   i }i }|D ][}|j        | j        k    rI|j        |vrt                      ||j        <   |j        D ]"}||j                                     |           #\| j	        D ][}|j        | j
        k    rI|j        |vrt                      ||j        <   |j        D ]"}||j                                     |           #\t          | j                  dk     r|                     dz  g           n'|D ]} |||           | j	        D ]} |||           | j        rPt          | j	                  dk     r"|                     | j
        dz  g           d S | j	        D ]} |||           d S d S )Nc                     | j         |vrdS || j                  v rdS | xj        gz  c_        || 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)r5   r=   r   )rC   comps_per_distr   s     r   add_component_only_oncez?Distribution._enable_component.<locals>.add_component_only_onceC  sc     {.00 ~fk222LLTF"LL6;'++D11111r   rS   rq   )r=   )r>   r=   )r<   r!   r    r>   r   r5   r@   r=   r   r)   r   rU   r   r(   )	r   r   r   sourcesr   comps_per_sdistsr   rC   s	    `       r   r   zDistribution._enable_componentB  sI   	2 	2 	2 	2 	2$ t()))t)***  	2 	2Av)))6//-0UUN16* 2 2A"16*..q1111) 	3 	3Av)))600.1eeOAF+ 3 3A#AF+//2222 t !!A%%OO4$;-O0000 " @ @''????2 A A''@@@@  	E4+,,q00T%5dTk]KKKKK #6 E EF++FODDDD	E 	EE Er   c                    g }|                     | j                   |                     | j                   |                     | j                   || j        v rg }|                     | j                   |D ]W}||j        v rL|j                            |           t          |j                  dk     r| j                            |           XdS )zx
        Disable a component in all main, child and source code sources
        (excluding cdrom based sources)
        rS   N)	r<   r!   r    r)   r&   r=   removerU   r   )r   r   r   rC   s       r   disable_componentzDistribution.disable_component  s    
 t()))t)***t/0004###GNN4,--- 	4 	4Fv|####D)))v|$$q(($++F333		4 	4r   c                       fd}g }g }| _          j        D ]} ||||            j        D ]0}|j        j        |j        j        |j        k    r ||||           1 j        D ]} ||||           dS )zHChange the server of all distro specific sources to
        a given hostc                 .   || _         | j        D ]P}| j         | j        |g|v r| j                            |           .|                    | j         | j        |g           Qt          | j                  dk     rj                            |            d S d S )NrS   )r8   r=   r5   r   r;   rU   r   )rC   r8   seenr   r   s       r   change_server_of_sourcez;Distribution.change_server.<locals>.change_server_of_source  s    FJ A AJT2d::L''----KKV[$ ?@@@@6<  1$$ ''///// %$r   N)r*   r!   r    r6   rT   r8   r)   )r   r8   r   seen_binaryseen_sourcerC   s   `     r   change_serverzDistribution.change_server  s    		0 		0 		0 		0 		0 !' 	> 	>F##FC====( 	B 	BF (0?+vz99''[AAA. 	> 	>F##FC====	> 	>r   c                      || j         k    rdS dS )z/Compare a given name with the release codename.TF)r   r   r1   s     r   r0   zDistribution.is_codename  s    4=  45r   N)NNNNr   )r   r   r	   r   rD   rA   rx   r   r   r   r   r   r   r0   r
   r   r   r   r   )   s        CE 
% 
% 
% 
%T T Tl6- 6- 6- 6-p. . .C C CJ   6& & & ;E ;E ;Ez4 4 4$> > ><    r   r   c                   $    e Zd ZdZd Zd Zd ZdS )DebianDistributionz)Class to support specific Debian featuresc                 2    || 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   s     r   r0   zDebianDistribution.is_codename  s(     4=  DL4K$K$K45r   c                    d}|                     d          }|                     d          }|dk    r |dk    r||t          d          z   |         }|| j        v rTt          d          t	          j        d| j        |                                                                                   z  S d|                    d          z  S )	zYTry to get a human readable name for the main mirror of a country
        Debian specificNz://ftp.z.debian.orgrR   rp   rL   rq   rr   )rc   rU   rV   rP   r   r^   rs   rt   s        r   rx   z#DebianDistribution._get_mirror_name  s     KK	""[[''77rRxxQY/"45Gdn$$ /"""w 7 > > @ @ &(( &------r   c                 >    t                               | d           d S )Nz http://ftp.%s.debian.org/debian/rd   r   rA   r   s    r   rA   zDebianDistribution.get_mirrors  s/      "D 	! 	
 	
 	
 	
 	
r   N)r   r   r	   __doc__r0   rx   rA   r
   r   r   r   r     sG        33  . . .&
 
 
 
 
r   r   c                       e Zd ZdZd ZdS )UbuntuDistributionz)Class to support specific Ubuntu featuresc                 >    t                               | d           d S )Nz$http://%s.archive.ubuntu.com/ubuntu/r   r   r   s    r   rA   zUbuntuDistribution.get_mirrors  s/      "H 	! 	
 	
 	
 	
 	
r   Nr   r   r	   r   rA   r
   r   r   r   r     s)        33
 
 
 
 
r   r   c                       e Zd ZdZd ZdS )UbuntuRTMDistributionz-Class to support specific Ubuntu RTM featuresc                 (    | j         j        | _        d S r   )r   rT   r+   r   s    r   rA   z!UbuntuRTMDistribution.get_mirrors  s    /8r   Nr   r
   r   r   r   r     s)        779 9 9 9 9r   r   c                     ddl m} m} ddl}ddddd}	  | d	d
g|                                          d         }|                    d          }|                    d |                    d          D                        n># t          $ r1}|j        |j	        k    rt          j        d|z             Y d}~nd}~ww xY w|S )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              3   H   K   | ]}d |v |                     d           V  dS )z:	N)split).0lines     r   	<genexpr>z_lsb_release.<locals>.<genexpr>  s3      UUDuPT}}djj''}}}}UUr   
z#lsb_release failed, using defaults:)
subprocessr   r   errnocommunicatedecodeupdater   OSErrorENOENTloggingwarning)r   r   r   resultoutexcs         r   _lsb_releaser     s    &&&&&&&&LLL "8	 FIe]G,T:::FFHHKjj!!UUCIIdOOUUUUUUU I I I9$$OACGHHHI Ms   A.B 
B?'B::B?c                     ddl m} m} ddl}	 ddl m} n9# t
          $ r, t          j        t          j        t          j	                  }Y nw xY w	  | ddg||d          
                                d         }|                                D ]5}|                    d	          r|                    d
d          d         c S 6n># t          $ r1}|j        |j        k    rt!          j        d|z             Y d}~nd}~ww xY wdS )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: rS   z+system-image-cli failed, using defaults: %s)r   r   r   r   r   ImportErrorrW   opendevnullO_RDWRr   
splitlinesr9   r   r   r   r   r   )r   r   r   r   r   r   r   s          r   _system_image_channelr     s^   &&&&&&&&LLL1&&&&&&& 1 1 1'"*bi001Qe&#	
 
 

 +-- NN$$ 	. 	.D{++ .zz$**1----.	.  Q Q Q9$$OICOPPPQ 4s.    3A
AA1C C 
C>'C99C>c                   6    e Zd ZdZdZd	dZd Zd Zd Zd Z	dS )

_OSReleasez/etc/os-releaseTc                     i | _         d| _        t          j        | _        t
          j                            | j                  sd S |                                  d| _        |r| 	                                 d S d S )NFT)
r   validr   OS_RELEASE_FILErF   rW   rX   isfileparseinject_lsb_compat)r   
lsb_compats     r   r   z_OSRelease.__init__  su    
.	w~~di(( 	F


 	%""$$$$$	% 	%r   c                 @   | 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   s    r   r   z_OSRelease.inject_lsb_compat-  s    (,D(9$%%)[%?M""&+//2D"E"EJ{:& 	I&*koo6G&H&HDK
#!%!>!>Ir   c                     t          | j        d          }|D ]7}|                                }|s | j        |                    dd            8|                                 d S )Nr=rS   )r   rF   r}   parse_entryr   close)r   fr   s      r   r   z_OSRelease.parse7  sl    C   	2 	2D::<<D Ddjja00111						r   c                     |                      |          }|dk    r*t          |t                    r|                    d          }|| j        |<   d S )NID_LIKE )parse_value
isinstancestrr   r   )r   keyvalues      r   r   z_OSRelease.parse_entry@  sT      '')
5# 6 6 KK$$E Cr   c                 d    t          j        |          }t          |          dk    r|d         S |S )NrS   r   )shlexr   rU   )r   r   valuess      r   r   z_OSRelease.parse_valueH  s1    U##v;;!!9r   N)T)
r   r   r	   DEFAULT_OS_RELEASE_FILEr   r   r   r   r   r   r
   r   r   r   r     so        /'O% % % %? ? ?  ! ! !    r   r   c                 Z   | r|r|r|st                      }g }t                      }|j        r|j        }t                      }|d         } |d         }|d         }|d         }|                    dg           }| dk    rPt                      }|@d|v 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 )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-RTMr   rS   -r   r   )r   r   r   r   r   r   rsplitr   r   r   r   r   )	r   r   r   r   r   
os_release	os_result
lsb_resultchannels	            r   
get_distror  O  sg     #8 # # #\\
	!^^
 	*")I "^^
()j) /Y'--	2..>>+--G"}'?'?!">>#q11!4::3BB1E&"	X~~!"hWgNNN	\$R;QQQ	x!"hWgNNNB+wHHHr   )r   r   r{   r   rW   xml.etree.ElementTreer   apt_pkgrP   	Exceptionr   objectr   r   r   r   r   r   r   r  r
   r   r   <module>r     s  4   				  				 - - - - - -            	 	 	 	 		 	 	 	K K K K K6 K K K\!
 !
 !
 !
 !
 !
 !
 !
H
 
 
 
 
 
 
 
9 9 9 9 9. 9 9 9  ,  41 1 1 1 1 1 1 1h 4r -I -I -I -I -I -Ir   