o
    4ñÕf1J  ã                   @   s  d dl mZmZ d dlZd dlZ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 g d¢Zdd„ Zdd	„ 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edkr�e
 ¡  eƒ ZeD ]Ze de ¡  ¡ qceddƒZe de ¡ e eddƒ¡ e eddƒ¡ dS dS )é    )Úabsolute_importÚprint_functionNé   )ÚDistInfo)Ú	is_mirrorÚSourceEntryÚNullMatcherÚSourcesListÚSourceEntryMatcherc                 C   s€   |  d¡}|   d¡} || krdS z| d¡d }|  d¡d }W n
 ty*   Y dS w d|v r>|| d¡d d… |kr>dS dS )z® check if the given add_url is idential or a mirror of orig_uri e.g.:
        master_uri = archive.ubuntu.com
        compare_uri = de.archive.ubuntu.com
        -> True
    z/ Tz//r   FÚ.N)ÚrstripÚsplitÚ
IndexErrorÚindex)Ú
master_uriÚcompare_uriÚcompare_srvÚ
master_srv© r   ú8/usr/lib/python3/dist-packages/aptsources/sourceslist.pyr   .   s   

þr   c                 C   s   t t| ƒƒS )a.   simple and efficient way to return uniq collection

    This is not intended for use with a SourceList. It is provided
    for internal use only. It does not have a leading underscore to
    not break any old code that uses it; but it should not be used
    in new code (and is not listed in __all__).)ÚlistÚset©Úsr   r   r   ÚuniqL   s   r   c                   @   sJ   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )r   z single sources.list entry Nc                 C   s|   d| _ d| _d| _g | _d | _d| _d| _g | _d| _|| _	|d u r.t
j d¡t
j d¡ }|| _|  |¡ d | _g | _d S )NFÚ zDir::EtcúDir::Etc::sourcelist)ÚinvalidÚdisabledÚtypeÚarchitecturesÚtrustedÚuriÚdistÚcompsÚcommentÚlineÚapt_pkgÚconfigÚfind_dirÚfindÚfileÚparseÚtemplateÚchildren)Úselfr&   r+   r   r   r   Ú__init__Y   s(   ÿ
ÿ

zSourceEntry.__init__c                 C   sH   | j |j ko#| j|jko#| j d¡|j d¡ko#| j|jko#| j|jkS )z- equal operator for two sources.list entries ú/)r   r   r"   r   r#   r$   )r/   Úotherr   r   r   Ú__eq__l   s   
ÿþ
ý
üzSourceEntry.__eq__c                 C   sê   |  ¡ }g }d}d}d}tt|ƒƒD ]U}|| dkr3|r*d}d}| |¡ || }qd}||| 7 }q|| dkrBd}||| 7 }q|rV||  ¡ sVd}| |¡ || }q||  ¡ ra|sad}q||| 7 }qt|ƒdkrs| |¡ |S )z‚ a split() implementation that understands the sources.list
            format better and takes [] into account (for e.g. cdroms) r   Fú[Tú]r   )ÚstripÚrangeÚlenÚappendÚisspace)r/   r&   ÚpiecesÚtmpÚp_foundÚspace_foundÚir   r   r   Úmysplitt   s6   




zSourceEntry.mysplitc              	   C   sÌ  || _ | ¡ }|dks|dkrd| _dS |d dkr8d| _|dd…  ¡  ¡ }|d dvr2d| _dS |dd… }| d¡}|dkrP||d d… | _|d|… }|  |¡}t|ƒdk r`d| _dS |d  ¡ | _	| j	d	vrqd| _dS |d  ¡ d d
krº| 
d¡ d¡ ¡ }|D ]2}z
| dd¡\}}W n tyŸ   d| _Y q‡w |dkr«| d¡| _q‡|dkr¶t |¡| _q‡d| _q‡|d  ¡ | _t| jƒdk rËd| _|d  ¡ | _t|ƒdkrá|dd… | _dS g | _dS )z^ parse a given sources.list (textual) line and break it up
            into the field we have r   ú#TNr   r   )Úrpmúrpm-srcÚdebúdeb-srcé   )rD   rE   rB   rC   r4   z[]ú=Úarchú,r!   é   )r&   r6   r   r   r   r*   r%   r@   r8   r   ÚpopÚ	Exceptionr    r'   Ústring_to_boolr!   r"   r#   r$   )r/   r&   r;   r?   ÚoptionsÚoptionÚkeyÚvaluer   r   r   r,   ™   sX   



ÿ
zSourceEntry.parsec                 C   sH   | | _ |r| j ¡  d¡| _dS | j ¡ d dkr"d| j | _dS dS )z# set a line to enabled or disabled rA   r   N)r   r&   Úlstripr6   )r/   Ú	new_valuer   r   r   Úset_enabledÙ   s   ÿzSourceEntry.set_enabledc                 C   s   |   ¡  ¡ S )z debug helper )Ústrr6   )r/   r   r   r   Ú__str__ä   s   zSourceEntry.__str__c                 C   sî   | j r| jS d}| jrd}|| j7 }| jr,| jdur,|dd | j¡| jr&dndf 7 }n| jdur=|d| jr8dnd 7 }n| jrJ|d	d | j¡ 7 }|d
| j| jf 7 }t	| j
ƒdkre|dd | j
¡ 7 }| jdkrq|d| j 7 }|d7 }|S )z# return the current line as string r   z# Nz [arch=%s trusted=%s]rI   ÚyesÚnoz [trusted=%s]z
 [arch=%s]z %s %sr   ú z #Ú
)r   r&   r   r   r    r!   Újoinr"   r#   r8   r$   r%   )r/   r&   r   r   r   rU   è   s*   

ÿ

zSourceEntry.str©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r3   r@   r,   rT   rV   rU   r   r   r   r   r   V   s    
%@r   c                   @   s   e Zd ZdZdd„ ZdS )r   z a Matcher that does nothing c                 C   s   dS )NTr   )r/   r   r   r   r   Úmatch  s   zNullMatcher.matchN)r]   r^   r_   r`   ra   r   r   r   r   r     s    r   c                   @   sz   e Zd ZdZ		ddd„Zdd„ Zdd	„ Zd
d„ Zdddg fdd„Zdd„ Z	dd„ Z
ddd„Zdd„ Zdd„ Zdd„ ZdS )r	   z8 represents the full sources.list + sources.list.d file Tú /usr/share/python-apt/templates/c                 C   s*   g | _ |rt|ƒ| _ntƒ | _|  ¡  d S r\   )r   r
   Úmatcherr   Úrefresh)r/   ÚwithMatcherÚmatcherPathr   r   r   r0     s
   zSourcesList.__init__c                 C   st   g | _ tj d¡}tj |¡r|  |¡ tj d¡}t	 	d| ¡D ]}|  |¡ q!| j D ]}|j
s7| j |¡ q,dS )z" update the list of known entries r   úDir::Etc::sourcepartsú	%s/*.listN)r   r'   r(   Ú	find_fileÚosÚpathÚexistsÚloadr)   Úglobr   rc   ra   )r/   r+   ÚpartsdirÚsourcer   r   r   rd     s   

€þzSourcesList.refreshc                 c   s   � | j D ]}|V  qdS )zM simple iterator to go over self.list, returns SourceEntry
            types N)r   )r/   Úentryr   r   r   Ú__iter__&  s   €
ÿzSourcesList.__iter__c                 /   sn   � ˆ   dd ¡}| jD ]*‰|r| d¡ˆj d¡krq
t‡ ‡fdd„ˆ D ƒƒr4t‡fdd„|D ƒƒr4ˆV  q
d S )Nr"   r1   c                 3   s"   � | ]}t ˆ|ƒˆ | kV  qd S r\   )Úgetattr)Ú.0rP   ©Úattrsrp   r   r   Ú	<genexpr>1  s   €  z%SourcesList.__find.<locals>.<genexpr>c                 3   s   � | ]}|ˆ ƒV  qd S r\   r   )rt   Ú	predicate)rp   r   r   rw   2  ó   € )rK   r   r   r"   Úall)r/   Ú
predicatesrv   r"   r   ru   r   Ú__find,  s   €
ÿ€ûzSourcesList.__findr   éÿÿÿÿNc	                    sÆ  |  ¡ }| d¡}	|	r|dd…  ¡ }tˆ ƒ‰ |dd… }
| j‡ fdd„|	d|||d�}|D ]}|
D ]}||jv rK|
|
 |¡= t|
ƒdkrK|    S q2q.| j‡ fd	d„d|||d
�}|D ]1}|j|	kryt|jƒt|
ƒkrut	|j|
 ƒ|_|  S |jrŽ|	sŽt|jƒt|
ƒkrŽd|_|  S q]|	r“dnd|ˆ rždd 
ˆ ¡ nd||g}| |
¡ |r±| d| ¡ d 
dd„ |D ƒ¡d }t|ƒ}|durÈ||_| j |¡ |dk rÚ| j |¡ |S | j ||¡ |S )z¨
        Add a new source to the sources.list.
        The method will search for existing matching repos and will try to
        reuse them as far as possible
        rA   r   Nc                    ó   t | jƒˆ kS r\   ©r   r    r   ©r    r   r   Ú<lambda>E  ó    z!SourcesList.add.<locals>.<lambda>F)r   r   r   r"   r#   r   c                    r~   r\   r   r   r€   r   r   r�   R  r‚   )r   r   r"   r#   r   z	[arch=%s]rI   rY   c                 s   s   � | ]}|r|V  qd S r\   r   )rt   Úpartr   r   r   rw   k  ry   z"SourcesList.add.<locals>.<genexpr>rZ   )r6   Ú
startswithrR   r   Ú_SourcesList__findr$   r   r8   r   r   r[   Úextendr9   r   r+   rc   ra   r   Úinsert)r/   r   r"   r#   Ú
orig_compsr%   Úposr+   r    r   r$   Úsourcesrp   Únew_compÚpartsr&   Ú	new_entryr   r€   r   Úadd5  sd   
þ
€úÿ

€
û
ÿzSourcesList.addc                 C   s   | j  |¡ dS )z2 remove the specified entry from the sources.list N)r   Úremove)r/   Úsource_entryr   r   r   r�   w  s   zSourcesList.removec                 C   s|   t j d¡}tj || ¡rtj |¡rt || |¡ t j d¡}t	 	d| ¡D ]}tj || ¡r;t || |¡ q)dS )z: restore sources.list files based on the backup extension r   rg   rh   N)
r'   r(   ri   rj   rk   rl   ÚshutilÚcopyr)   rn   )r/   Ú
backup_extr+   ro   r   r   r   Úrestore_backup{  s   €þzSourcesList.restore_backupc                 C   sX   t ƒ }|du rt d¡}| jD ]}|j|vr)tj |j¡r)t 	|jd|j|f ¡ q|S )z† make a backup of the current source files, if no backup extension
            is given, the current date/time is used (and returned) Nz%y%m%d.%H%Mz%s%s)
r   ÚtimeÚstrftimer   r+   rj   rk   rl   r‘   r’   )r/   r“   Úalready_backupedrp   r   r   r   Úbackup†  s   


ÿ€zSourcesList.backupc                 C   sx   z*t |dƒ�}|D ]}t||ƒ}| j |¡ q	W d  ƒ W dS 1 s#w   Y  W dS  ty;   t d| ¡ Y dS w )z (re)load the current sources ÚrNzcould not open file '%s'
)Úopenr   r   r9   rL   ÚloggingÚwarning)r/   r+   Úfr&   rp   r   r   r   rm   ’  s   
þ&ÿÿzSourcesList.loadc              	   C   sÎ   i }t | jƒdkr.tj d¡}d}t|dƒ�}| |¡ W d  ƒ dS 1 s'w   Y  dS z,| jD ]}|j|vrBt|jdƒ||j< ||j  | ¡ ¡ q2W |D ]}||  	¡  qPdS |D ]}||  	¡  q]w )z save the current sources r   r   z ## See sources.list(5) for more information, especialy
# Remember that you can only use http, ftp or file URIs
# CDROMs are managed through the apt-cdrom tool.
ÚwN)
r8   r   r'   r(   ri   rš   Úwriter+   rU   Úclose)r/   Úfilesrk   Úheaderr�   rp   r   r   r   Úsaveœ  s,   ÿ
ÿþ

ýÿÿzSourcesList.savec                 C   sr   g }i }|D ].}|j du rq|j jr'|j }||vrg ||< || }| |¡ qt|j jƒdkr4| |¡ q||fS )z5get all parent and child channels in the sources listNr   )r-   Úchildr9   r8   r.   )r/   Úsources_listÚparentsÚused_child_templatesrp   rP   Útempr   r   r   Úcheck_for_relations´  s   

€zSourcesList.check_for_relations)Trb   r\   )r]   r^   r_   r`   r0   rd   rr   r…   rŽ   r�   r”   r˜   rm   r£   r©   r   r   r   r   r	   	  s"    
þ
	
ÿB

r	   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r
   z} matcher class to make a source entry look nice
        lots of predefined matchers to make it i18n/gettext friendly
        c                 C   sr   g | _ t d| ¡}|D ]*}tj |¡}| d¡}|d|… }t||d�}|j D ]}|jd ur5| j  |¡ q(qd S )Nz	%s/*.infoz.infor   )Úbase_dir)	Ú	templatesrn   rj   rk   Úbasenamer*   r   Ú	match_urir9   )r/   rf   Ú
spec_filesr�   r?   r#   r-   r   r   r   r0   Ò  s   


€þzSourceEntryMatcher.__init__c                 C   s†   d}| j D ];}t |j|j¡r*t |j|j¡r*|j|jks"|jdkr*d}||_	 |S | 
|j¡r@t |j|j¡r@d}||_	 |S q|S )z%Add a matching template to the sourceFrD   T)r«   ÚreÚsearchr­   r"   ra   Ú
match_namer#   r   r-   r   )r/   rp   Úfoundr-   r   r   r   ra   à  s&   
ÿûÿ€ zSourceEntryMatcher.matchN)r]   r^   r_   r`   r0   ra   r   r   r   r   r
   Í  s    r
   Ú__main__zentry %sz!http://archive.ubuntu.com/ubuntu/z$http://de.archive.ubuntu.com/ubuntu/zis_mirror(): %sz http://archive.ubuntu.com/ubuntuz#http://de.archive.ubuntu.com/ubuntu)Ú
__future__r   r   rn   r›   Úos.pathrj   r¯   r‘   r•   r'   Údistinfor   Ú__all__r   r   Úobjectr   r   r	   r
   r]   Úinit_configrŠ   rq   ÚinforU   Úmirrorr   r   r   r   Ú<module>   sD   
 - E(ÿÿ
ÿò