o
    4f1J                     @   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re
  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 qw |dkr|d| _q|dkrt|| _qd| _q|d  | _t| jdk rd| _|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|	st|jt|
krd|_|  S q]|	rdnd| rd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(
