a
    †?i1J  ã                   @   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 ¡  ¡ qÈeddƒZe de ¡ e eddƒ¡ e eddƒ¡ 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yT   Y dS 0 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)Z
master_uriZcompare_uriZcompare_srvZ
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oF| j|jkoF| j d¡|j d¡koF| j|jkoF| 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 ]ª}|| dkrf|rTd}d}| |¡ || }qÎd}||| 7 }q$|| dkr„d}||| 7 }q$|r¬||  ¡ s¬d}| |¡ || }q$||  ¡ rÂ|sÂd}q$||| 7 }q$t|ƒdkræ| |¡ |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ÚtmpZp_foundZspace_foundÚir   r   r   Úmysplitt   s6    




zSourceEntry.mysplitc              	   C   sØ  || _ | ¡ }|dks|dkr(d| _dS |d dkrpd| _|dd…  ¡  ¡ }|d dvrdd| _dS |dd… }| d¡}|dkr ||d d… | _|d|… }|  |¡}t|ƒdk rÀd| _dS |d  ¡ | _	| j	d	vrâd| _dS |d  ¡ d d
kr~| 
d¡ d¡ ¡ }|D ]l}z| dd¡\}}W n tyB   d| _Y n80 |dkr\| d¡| _n|dkrtt |¡| _nd| _q|d  ¡ | _t| jƒdk r¢d| _|d  ¡ | _t|ƒdkrÎ|dd… | _n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é   )r>   r?   r<   r=   r0   z[]ú=Zarchú,r   é   )r"   r2   r   r   r   r&   r!   r:   r4   r   ÚpopÚ	Exceptionr   r#   Zstring_to_boolr   r   r   r    )r+   r"   r7   r9   ZoptionsZoptionÚkeyÚvaluer   r   r   r(   ™   sV    





zSourceEntry.parsec                 C   sB   | | _ |r | j ¡  d¡| _n| j ¡ d dkr>d| j | _dS )z# set a line to enabled or disabled r;   r   N)r   r"   Úlstripr2   )r+   Z	new_valuer   r   r   Úset_enabledÙ   s
    zSourceEntry.set_enabledc                 C   s   |   ¡  ¡ S )z debug helper )Ústrr2   )r+   r   r   r   Ú__str__ä   s    zSourceEntry.__str__c                 C   sî   | j r| jS d}| jrd}|| j7 }| jrX| jdurX|dd | j¡| jrLdndf 7 }n<| jdurz|d| jrpdnd 7 }n| jr”|d	d | j¡ 7 }|d
| j| jf 7 }t	| j
ƒdkrÊ|dd | j
¡ 7 }| jdkrâ|d| j 7 }|d7 }|S )z# return the current line as string r   z# Nz [arch=%s trusted=%s]rB   ZyesZnoz [trusted=%s]z
 [arch=%s]z %s %sr   ú z #Ú
)r   r"   r   r   r   r   Újoinr   r   r4   r    r!   )r+   r"   r   r   r   rJ   è   s*    
ÿ


zSourceEntry.str)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r/   r:   r(   rI   rK   rJ   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)rO   rP   rQ   rR   rS   r   r   r   r   r     s   r   c                   @   sv   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 ©N)r   r
   Úmatcherr   Úrefresh)r+   ZwithMatcherÚmatcherPathr   r   r   r,     s
    zSourcesList.__init__c                 C   st   g | _ tj d¡}tj |¡r(|  |¡ tj d¡}t	 	d| ¡D ]}|  |¡ qB| j D ]}|j
sX| j |¡ qXdS )z" update the list of known entries r   úDir::Etc::sourcepartsú	%s/*.listN)r   r#   r$   Ú	find_fileÚosÚpathÚexistsÚloadr%   Úglobr   rV   rS   )r+   r'   ÚpartsdirÚsourcer   r   r   rW     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                 /   sl   ˆ   dd ¡}| jD ]T‰|r2| d¡ˆj d¡kr2qt‡ ‡fdd„ˆ D ƒƒrt‡fdd„|D ƒƒrˆV  qd S )Nr   r-   c                 3   s    | ]}t ˆ|ƒˆ | kV  qd S rU   )Úgetattr)Ú.0rF   ©Úattrsrb   r   r   Ú	<genexpr>1  ó    z%SourcesList.__find.<locals>.<genexpr>c                 3   s   | ]}|ˆ ƒV  qd S rU   r   )rf   Z	predicate)rb   r   r   ri   2  rj   )rD   r   r   r   Úall)r+   Z
predicatesrh   r   r   rg   r   Z__find,  s    
ÿzSourcesList.__findr   éÿÿÿÿNc	                    sÎ  |  ¡ }| d¡}	|	r&|dd…  ¡ }tˆ ƒ‰ |dd… }
| j‡ fdd„|	d|||d}|D ]<}|
D ]2}||jv rd|
|
 |¡= t|
ƒdkrd|    S qdq\| j‡ fd	d„d|||d
}|D ]b}|j|	kròt|jƒt|
ƒkrêt	|j|
 ƒ|_|  S |jrº|	sºt|jƒt|
ƒkrºd|_|  S qº|	r(dnd|ˆ r@dd 
ˆ ¡ nd||g}| |
¡ |rh| d| ¡ d 
dd„ |D ƒ¡d }t|ƒ}|dur˜||_| j |¡ |dk r¼| j |¡ n| 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
        r;   r   Nc                    s   t | jƒˆ kS rU   ©r   r   r   ©r   r   r   Ú<lambda>E  rj   z!SourcesList.add.<locals>.<lambda>F)r   r   r   r   r   r   c                    s   t | jƒˆ kS rU   rm   r   rn   r   r   ro   R  rj   )r   r   r   r   r   z	[arch=%s]rB   rL   c                 s   s   | ]}|r|V  qd S rU   r   )rf   Úpartr   r   r   ri   k  rj   z"SourcesList.add.<locals>.<genexpr>rM   )r2   Ú
startswithrH   r   Ú_SourcesList__findr    r   r4   r   r   rN   Úextendr5   r   r'   rV   rS   r   Úinsert)r+   r   r   r   Z
orig_compsr!   Úposr'   r   r   r    Úsourcesrb   Znew_compÚpartsr"   Z	new_entryr   rn   r   Úadd5  s\    
þ
ÿ


û


zSourcesList.addc                 C   s   | j  |¡ dS )z2 remove the specified entry from the sources.list N)r   Úremove)r+   Zsource_entryr   r   r   ry   w  s    zSourcesList.removec                 C   s|   t j d¡}tj || ¡r8tj |¡r8t || |¡ t j d¡}t	 	d| ¡D ]$}tj || ¡rRt || |¡ qRdS )z: restore sources.list files based on the backup extension r   rY   rZ   N)
r#   r$   r[   r\   r]   r^   ÚshutilÚcopyr%   r`   )r+   Ú
backup_extr'   ra   r   r   r   Úrestore_backup{  s    zSourcesList.restore_backupc                 C   sX   t ƒ }|du rt d¡}| jD ]4}|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'   r\   r]   r^   rz   r{   )r+   r|   Zalready_backupedrb   r   r   r   Úbackup†  s    


ÿzSourcesList.backupc                 C   st   zNt |dƒ0}|D ]}t||ƒ}| j |¡ qW d  ƒ n1 sB0    Y  W n  tyn   t d| ¡ Y n0 dS )z (re)load the current sources ÚrNzcould not open file '%s'
)Úopenr   r   r5   rE   ÚloggingZwarning)r+   r'   Úfr"   rb   r   r   r   r_   ’  s    
0zSourcesList.loadc              	   C   sÎ   i }t | jƒdkrZtj d¡}d}t|dƒ}| |¡ W d  ƒ n1 sL0    Y  dS zV| jD ]4}|j|vr‚t|jdƒ||j< ||j  | ¡ ¡ qbW |D ]}||  	¡  qžn|D ]}||  	¡  q¶0 dS )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)
r4   r   r#   r$   r[   r‚   Úwriter'   rJ   Úclose)r+   Úfilesr]   Úheaderr„   rb   r   r   r   Úsaveœ  s"    ÿ(

ÿzSourcesList.savec                 C   sr   g }i }|D ]\}|j du rq|j jrN|j }||vr:g ||< || }| |¡ qt|j jƒdkr| |¡ q||fS )z5get all parent and child channels in the sources listNr   )r)   Úchildr5   r4   r*   )r+   Zsources_listÚparentsZused_child_templatesrb   rF   Ztempr   r   r   Úcheck_for_relations´  s    
zSourcesList.check_for_relations)TrT   )N)rO   rP   rQ   rR   r,   rW   rd   rr   rx   ry   r}   r€   r_   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}tj |¡}| d¡}|d|… }t||d}|j D ]}|jd urP| j  |¡ qPqd S )Nz	%s/*.infoz.infor   )Zbase_dir)	Ú	templatesr`   r\   r]   Úbasenamer&   r   Ú	match_urir5   )r+   rX   Z
spec_filesr„   r9   r   r)   r   r   r   r,   Ò  s    


zSourceEntryMatcher.__init__c                 C   s„   d}| j D ]t}t |j|j¡rTt |j|j¡rT|j|jksD|jdkrTd}||_	 q€q
| 
|j¡r
t |j|j¡r
d}||_	 q€q
|S )z%Add a matching template to the sourceFr>   T)rŽ   ÚreÚsearchr   r   rS   Z
match_namer   r   r)   r   )r+   rb   Úfoundr)   r   r   r   rS   à  s&    
ÿ
üüÿzSourceEntryMatcher.matchN)rO   rP   rQ   rR   r,   rS   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)Z
__future__r   r   r`   rƒ   Úos.pathr\   r‘   rz   r~   r#   Zdistinfor   Ú__all__r   r   Úobjectr   r   r	   r
   rO   Zinit_configrv   rc   ÚinforJ   Zmirrorr   r   r   r   Ú<module>   s@   
 - E(
ÿÿÿ