U
    ž`1J                     @   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   qe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}|| || }qd}||| 7 }q$|| dkrd}||| 7 }q$|r||  sd}|| || }q$||  r|sd}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 rd| _dS |d  | _	| j	d	krd| _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 rd| _|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rt|jt|
krt	|j|
 |_|  S |jr|	st|jt|
krd|_|  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 ]}||   qX 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}||_	 qq
|
|jr
t|j|jr
d}||_	 qq
|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(
