

`jI                 @   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 Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d	   d	 e  Z e d k re
 j   e   Z x% e D] Z e j d e j    q%We d d  Z e j d e  e j e d d   e j e d d   d S)    )absolute_importprint_functionN   )DistInfo	is_mirrorSourceEntryNullMatcherSourcesListSourceEntryMatcherc             C   s   | j  d  } |  j  d  }  | |  k r. d Sy* | j d  d } |  j d  d } Wn t k
 rp d SYn Xd | k r | | j d  d d  | k r d Sd 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               @   sm   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 j d  t
 j j d  } | |  _ |  j |  d  |  _ g  |  _ d  S)NF zDir::Etcz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   sX   |  j  | j  k oW |  j | j k oW |  j | j k oW |  j | j k oW |  j | j k S)z- equal operator for two sources.list entries )r   r   r   r   r   )r)   otherr   r   r   __eq__l   s
    zSourceEntry.__eq__c             C   sC  | j    } g  } d } d } d } x t t |   D] } | | d k r | ry d } d } | j |  | | } qd } | | | 7} q7 | | d k r d } | | | 7} q7 | r | | j   r d } | j |  | | } q7 | | j   r| rd } q7 | | | 7} q7 Wt |  d k r?| j |  | 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  | |  _  | j   } | d k s- | d k r: d |  _ d S| d d k r d |  _ | d d  j   j   } | d d k r d |  _ d S| d d  } | j d  } | d k r | | d d  |  _ | d |  } |  j |  } t |  d k  rd |  _ d S| d j   |  _	 |  j	 d k r;d |  _ d S| d j   d d k r	| j
 d  j d  j   } x | D] } y | j d d  \ } } Wn t k
 rd |  _ YqzX| d k r| j d  |  _ qz| d k rt j |  |  _ qzd |  _ qzW| d j   |  _ t |  j  d k  r:d |  _ | d j   |  _ t |  d k ru| 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-   z[]=Zarch,r      )r8   r9   r:   r;   )r:   r;   r8   r9   )r    r/   r   r   r   r$   r   r6   r1   r   pop	Exceptionr   r!   Zstring_to_boolr   r   r   r   )r)   r    r4   r5   ZoptionsZoptionkeyvaluer   r   r   r&      sV    							zSourceEntry.parsec             C   s[   | |  _  | r. |  j j   j d  |  _ n) |  j j   d d k rW d |  j |  _ d S)z# set a line to enabled or disabled r7   r   N)r   r    lstripr/   )r)   Z	new_valuer   r   r   set_enabled   s
    
zSourceEntry.set_enabledc             C   s   |  j    j   S)z debug helper )strr/   )r)   r   r   r   __str__   s    zSourceEntry.__str__c             C   sE  |  j  r |  j Sd } |  j r% d } | |  j 7} |  j r| |  j d k	 r| | d d j |  j  |  j rn d n d f 7} nR |  j d k	 r | d |  j r d n d 7} n# |  j r | d	 d j |  j  7} | d
 |  j |  j f 7} t	 |  j
  d k r| d d j |  j
  7} |  j d k r7| d |  j 7} | d 7} | S)z# return the current line as string r   z# Nz [arch=%s trusted=%s]r>   yesnoz [trusted=%s]z
 [arch=%s]z %s %sr    z #
)r   r    r   r   r   r   joinr   r   r1   r   r   )r)   r    r   r   r   rF      s(    		, 	
zSourceEntry.str)__name__
__module____qualname____doc__r*   r,   r6   r&   rE   rG   rF   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)rM   rN   rO   rP   rQ   r   r   r   r   r     s   c               @   s   e  Z d  Z d Z d d d d  Z d d   Z d d	   Z d
 d   Z d d d g  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 Tz /usr/share/python-apt/templates/c             C   s;   g  |  _  | r! t |  |  _ n t   |  _ |  j   d  S)N)r   r
   matcherr   refresh)r)   ZwithMatchermatcherPathr   r   r   r*     s
    	zSourcesList.__init__c             C   s   g  |  _  t j j d  } |  j |  t j j d  } x( t j d |  D] } |  j |  qN Wx* |  j  D] } | j so |  j j	 |  qo Wd S)z" update the list of known entries zDir::Etc::sourcelistzDir::Etc::sourcepartsz	%s/*.listN)
r   r!   r"   	find_fileloadr#   globr   rR   rQ   )r)   r%   partsdirsourcer   r   r   rS     s    		zSourcesList.refreshc             c   s   x |  j  D] } | Vq
 Wd S)zM simple iterator to go over self.list, returns SourceEntry
            types N)r   )r)   entryr   r   r   __iter__%  s    zSourcesList.__iter__c             /   s^   xW |  j  D]L  t    f d d     D  r
 t  f d d   | D  r
  Vq
 Wd  S)Nc             3   s(   |  ] } t   |    | k Vq d  S)N)getattr).0rB   )attrsrY   r   r   	<genexpr>-  s    z%SourcesList.__find.<locals>.<genexpr>c             3   s   |  ] } |    Vq d  S)Nr   )r]   Z	predicate)rY   r   r   r_   .  s    )r   all)r)   Z
predicatesr^   r   )r^   rY   r   Z__find+  s    "zSourcesList.__findr   r   Nc	                s  | j    } | j d  }	 |	 r7 | d d  j   } t      | d d  }
 |  j   f d d   d |	 d d d	 | d
 | d | } xT | D]L } xC |
 D]; } | | j k r |
 |
 j |  =t |
  d k r | Sq Wq W|  j   f d d   d d d	 | d
 | d | } x | D] } | j |	 k rgt | j  t |
  k rct	 | j |
  | _ | S| j r|	 rt | j  t |
  k rd | _ | SqW|	 rd n d |   rd d j
    n d | | g } | j |
  | r| j d |  d j
 d d   | D  d } t |  } | d k	 rB| | _ |  j j |  | d k  rq|  j j |  n |  j 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
        r7   r   Nc                s   t  |  j    k S)N)r   r   )r   )r   r   r   <lambda>A  s    z!SourcesList.add.<locals>.<lambda>r   r   Fr   r   r   r   c                s   t  |  j    k S)N)r   r   )r   )r   r   r   ra   N  s    r   z	[arch=%s]r>   rJ   c             s   s   |  ] } | r | Vq d  S)Nr   )r]   partr   r   r   r_   g  s    z"SourcesList.add.<locals>.<genexpr>rK   )r/   
startswithrD   r   _SourcesList__findr   r   r1   r   r   rL   extendr2   r   r%   rR   rQ   r   insert)r)   r   r   r   Z
orig_compsr   posr%   r   r   r   sourcesrY   Znew_comppartsr    Z	new_entryr   )r   r   add1  sV    		 	zSourcesList.addc             C   s   |  j  j |  d S)z2 remove the specified entry from the sources.list N)r   remove)r)   Zsource_entryr   r   r   rk   s  s    zSourcesList.removec             C   s   t  j j d  } t j j | |  rN t j j |  rN t j | | |  t  j j d  } xE t	 j	 d |  D]0 } t j j | |  rt t j | | |  qt Wd S)z: restore sources.list files based on the backup extension zDir::Etc::sourcelistzDir::Etc::sourcepartsz	%s/*.listN)
r!   r"   rU   ospathexistsshutilcopyr#   rW   )r)   
backup_extr%   rX   r   r   r   restore_backupw  s    (zSourcesList.restore_backupc             C   s   t    } | d k r$ t j d  } xU |  j D]J } | j | k r. t j j | j  r. t j	 | j d | j | f  q. W| 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   timeZstrftimer   r%   rl   rm   rn   ro   rp   )r)   rq   Zalready_backupedrY   r   r   r   backup  s    	$zSourcesList.backupc             C   sm   yM t  | d  7 } x- | D]% } t | |  } |  j j |  q WWd QRXWn t j d |  Yn Xd S)z (re)load the current sources rNzcould not open file '%s'
)openr   r   r2   loggingZwarning)r)   r%   fr    rY   r   r   r   rV     s    zSourcesList.loadc             C   s   i  } t  |  j  d k r] t j j d  } d } t | d   } | j |  Wd QRXd SzZ xS |  j D]H } | j | k r t | j d  | | j <| | j j | j    qj WWd x | D] } | | j	   q WXd S)z save the current sources r   zDir::Etc::sourcelistz## 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)
r1   r   r!   r"   rU   rv   writer%   rF   close)r)   filesrm   headerrx   rY   r   r   r   save  s    "zSourcesList.savec             C   s   g  } i  } x | D] } | j  d k r+ q | j  j rp | j  } | | k rV g  | | <| | } | j |  q t | j  j  d k r | j |  q W| | f S)z5get all parent and child channels in the sources listNr   )r'   Zchildr2   r1   r(   )r)   Zsources_listparentsZused_child_templatesrY   rB   Ztempr   r   r   check_for_relations  s    	

zSourcesList.check_for_relations)rM   rN   rO   rP   r*   rS   r[   rd   rj   rk   rr   rt   rV   r~   r   r   r   r   r   r	   	  s   	A
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   s   g  |  _  t j d |  } x | D]| } t j j |  } | j d  } | d |  } t | d | } x0 | j  D]% } | j d  k	 rv |  j  j |  qv Wq# Wd  S)Nz	%s/*.infoz.infor   Zbase_dir)		templatesrW   rl   rm   basenamer$   r   	match_urir2   )r)   rT   Z
spec_filesrx   r5   r   r'   r   r   r   r*     s    	zSourceEntryMatcher.__init__c             C   s   d } x |  j  D] } t j | j | j  rz t j | j | j  rz | j | j k sg | j d k rz d } | | _	 Pq | j
 | j  r t j | j | j  r d } | | _	 Pq W| S)z%Add a matching template to the sourceFr:   T)r   researchr   r   rQ   Z
match_namer   r   r'   r   )r)   rY   foundr'   r   r   r   rQ     s    !		zSourceEntryMatcher.matchN)rM   rN   rO   rP   r*   rQ   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   rW   rw   os.pathrl   r   ro   rs   r!   Zdistinfor   __all__r   r   objectr   r   r	   r
   rM   Zinit_configrh   rZ   inforF   Zmirrorr   r   r   r   <module>   s8   	
(
		
