U
    &Ÿˆc1J  ã                   @   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 ddddd	g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k
rV   Y dS X d|k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    

ÿ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 k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"   ÚpiecesZ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krdd| _dS |dd… }| d¡}|dkr ||d d… | _|d|… }|  |¡}t|ƒdk rÀd| _dS |d  ¡ | _	| j	d	krâd| _dS |d  ¡ d d
kr€| 
d¡ d¡ ¡ }|D ]n}z| dd¡\}}W n tk
rD   d| _Y n8X |dkr^| d¡| _n|dkrvt |¡| _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!   r9   r4   r   ÚpopÚ	Exceptionr   r#   Zstring_to_boolr   r   r   r    )r+   r"   r7   r8   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k	rX|dd | j¡| jrLdndf 7 }n<| jdk	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]rA   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   rI   è   s,    

 ÿ


zSourceEntry.str)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r/   r9   r(   rH   rJ   rI   r   r   r   r   r   V   s   
%@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)rN   rO   rP   rQ   rR   r   r   r   r   r     s   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   rU   rR   )r+   r'   ÚpartsdirÚsourcer   r   r   rV     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 rT   )Úgetattr)Ú.0rE   ©Úattrsra   r   r   Ú	<genexpr>1  s     z%SourcesList.__find.<locals>.<genexpr>c                 3   s   | ]}|ˆ ƒV  qd S rT   r   )re   Z	predicate)ra   r   r   rh   2  s     )rC   r   r   r   Úall)r+   Z
predicatesrg   r   r   rf   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k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k	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 rT   ©r   r   r   ©r   r   r   Ú<lambda>E  ó    z!SourcesList.add.<locals>.<lambda>F)r   r   r   r   r   r   c                    s   t | jƒˆ kS rT   rk   r   rl   r   r   rm   R  rn   )r   r   r   r   r   z	[arch=%s]rA   rK   c                 s   s   | ]}|r|V  qd S rT   r   )re   Úpartr   r   r   rh   k  s      z"SourcesList.add.<locals>.<genexpr>rL   )r2   Ú
startswithrG   r   Ú_SourcesList__findr    r   r4   r   r   rM   Úextendr5   r   r'   rU   rR   r   Úinsert)r+   r   r   r   Z
orig_compsr!   Úposr'   r   r   r    Úsourcesra   Znew_compÚpartsr"   Z	new_entryr   rl   r   Úadd5  sh    
   þ
   ÿ


û


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   rx   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   rX   rY   N)
r#   r$   rZ   r[   r\   r]   ÚshutilÚcopyr%   r_   )r+   Ú
backup_extr'   r`   r   r   r   Úrestore_backup{  s    zSourcesList.restore_backupc                 C   sX   t ƒ }|dkrt d¡}| jD ]4}|j|k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]   ry   rz   )r+   r{   Zalready_backupedra   r   r   r   Úbackup†  s    


ÿzSourcesList.backupc              	   C   sb   z:t |dƒ&}|D ]}t||ƒ}| j |¡ qW 5 Q R X W n" tk
r\   t d| ¡ Y nX dS )z (re)load the current sources Úrzcould not open file '%s'
N)Úopenr   r   r5   rD   ÚloggingZwarning)r+   r'   Úfr"   ra   r   r   r   r^   ’  s    
zSourcesList.loadc              	   C   s¤   i }t | jƒdkrFtj d¡}d}t|dƒ}| |¡ W 5 Q R X dS z@| jD ]4}|j|krnt|jdƒ||j< ||j  | 	¡ ¡ qNW 5 |D ]}||  ¡  qŒX 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$   rZ   r   ÚwriteÚcloser'   rI   )r+   Úfilesr\   Úheaderrƒ   ra   r   r   r   Úsaveœ  s    ÿ

zSourcesList.savec                 C   sr   g }i }|D ]\}|j dkrq|j jrN|j }||kr:g ||< || }| |¡ qt|j jƒdkr| |¡ q||fS )z5get all parent and child channels in the sources listNr   )r)   Zchildr5   r4   r*   )r+   Zsources_listÚparentsZused_child_templatesra   rE   Ztempr   r   r   Úcheck_for_relations´  s    
zSourcesList.check_for_relations)TrS   )N)rN   rO   rP   rQ   r,   rV   rc   rq   rw   rx   r|   r   r^   r‰   r‹   r   r   r   r   r	   	  s      þ

	ÿ
B

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 k	rP| j  |¡ qPqd S )Nz	%s/*.infoz.infor   )Zbase_dir)	Ú	templatesr_   r[   r\   Úbasenamer&   r   Ú	match_urir5   )r+   rW   Z
spec_filesrƒ   r8   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   rR   Z
match_namer   r   r)   r   )r+   ra   Úfoundr)   r   r   r   rR   à  s&    
ÿ
üüÿzSourceEntryMatcher.matchN)rN   rO   rP   rQ   r,   rR   r   r   r   r   r
   Í  s   Ú__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   ry   r}   r#   Zdistinfor   Ú__all__r   r   Úobjectr   r   r	   r
   rN   Zinit_configru   rb   ÚinforI   Zmirrorr   r   r   r   Ú<module>   sD   ÿ
 - E(
ÿÿÿ