

`4-                 @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z m Z d  d l Z d  d l	 Z	 d  d l	 m
 Z Gd d   d e  Z Gd d   d e  Z Gd	 d
   d
 e  Z Gd d   d e  Z d d   Z Gd d   d e  Z e d k r	e d d  Z e j e j  x e j D] Z e j d e j  e j d e j  e j d e j  e j d e j  e j i  k re j d e e j j     x4 e j  D]) Z! e j d e! j e! j e! j" f  qWx% e j# D] Z$ e j d e$ j  qWq!Wd S)    )print_functionN)PopenPIPE)gettextc               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)Templatec             C   s   d  |  _  d |  _ g  |  _ d  |  _ d  |  _ d  |  _ d  |  _ g  |  _ g  |  _ d  |  _	 i  |  _
 d  |  _ d |  _ d |  _ d  S)NFT)namechildparents
match_namedescriptionbase_uritype
componentschildren	match_uri
mirror_setdistribution	availableofficial)self r   5/usr/lib/python3/dist-packages/aptsources/distinfo.py__init__'   s    													zTemplate.__init__c             C   s   | d d   |  j  D k S)z8 Check if the distribution provides the given component c             s   s   |  ] } | j  Vq d  S)N)r   ).0cr   r   r   	<genexpr>9   s    z)Template.has_component.<locals>.<genexpr>)r   )r   compr   r   r   has_component7   s    zTemplate.has_componentc             C   sC   t  |  \ } } } | |  j k r; |  j | j | |  Sd Sd S)z8 Check if a given url of a repository is a valid mirror FN)	split_urlr   has_repository)r   urlprotohostnamedirr   r   r   	is_mirror;   s    zTemplate.is_mirrorN)__name__
__module____qualname__r   r   r$   r   r   r   r   r   %   s   r   c               @   sm   e  Z d  Z d d d d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z	 d S)	ComponentNc             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)r   r   description_longparent_component)r   r   descZ	long_descr*   r   r   r   r   F   s    			zComponent.__init__c             C   s   |  j  S)N)r*   )r   r   r   r   get_parent_componentL   s    zComponent.get_parent_componentc             C   s   | |  _  d  S)N)r*   )r   parentr   r   r   set_parent_componentO   s    zComponent.set_parent_componentc             C   s4   |  j  d  k	 r |  j  S|  j d  k	 r, |  j Sd  Sd  S)N)r)   r   )r   r   r   r   get_descriptionR   s
    zComponent.get_descriptionc             C   s   | |  _  d  S)N)r   )r   r+   r   r   r   set_descriptionZ   s    zComponent.set_descriptionc             C   s   | |  _  d  S)N)r)   )r   r+   r   r   r   set_description_long]   s    zComponent.set_description_longc             C   s   |  j  S)N)r)   )r   r   r   r   get_description_long`   s    zComponent.get_description_long)
r%   r&   r'   r   r,   r.   r/   r0   r1   r2   r   r   r   r   r(   D   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)Mirrorz( Storage for mirror related information Nc             C   s/   | |  _  g  |  _ |  j | |  | |  _ d  S)N)r"   repositoriesadd_repositorylocation)r   r!   r"   r#   r6   r   r   r   r   g   s    		zMirror.__init__c             C   s   |  j  j t | |   d  S)N)r4   append
Repository)r   r!   r#   r   r   r   r5   m   s    zMirror.add_repositoryc                s     f d d   |  j  D S)Nc                s%   g  |  ] } | j    k r |  q Sr   )r!   )r   r)r!   r   r   
<listcomp>q   s   	 z5Mirror.get_repositories_for_proto.<locals>.<listcomp>)r4   )r   r!   r   )r!   r   get_repositories_for_protop   s    z!Mirror.get_repositories_for_protoc             C   sJ   | d  k r d Sx3 |  j  D]( } | j | k r | | j k r d Sq Wd S)NFT)r4   r!   r#   )r   r!   r#   r9   r   r   r   r   s   s    zMirror.has_repositoryc                s     f d d     j  D S)Nc                s"   g  |  ] } | j    j   q Sr   )get_urlr"   )r   r9   )r   r   r   r:   |   s   	 z(Mirror.get_repo_urls.<locals>.<listcomp>)r4   )r   r   )r   r   get_repo_urls{   s    zMirror.get_repo_urlsc             C   s   |  j  S)N)r6   )r   r   r   r   get_location~   s    zMirror.get_locationc             C   s   | |  _  d  S)N)r6   )r   r6   r   r   r   set_location   s    zMirror.set_location)r%   r&   r'   __doc__r   r5   r;   r   r=   r>   r?   r   r   r   r   r3   d   s   r3   c               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)r8   c             C   s   | |  _  | |  _ d  S)N)r!   r#   )r   r!   r#   r   r   r   r      s    	zRepository.__init__c             C   s   |  j  |  j f S)N)r!   r#   )r   r   r   r   get_info   s    zRepository.get_infoc             C   s   d |  j  | |  j f S)Nz
%s://%s/%s)r!   r#   )r   r"   r   r   r   r<      s    zRepository.get_urlN)r%   r&   r'   r   rA   r<   r   r   r   r   r8      s   r8   c             C   sB   t  j d |  d d } x# t |  d k  r= | j d  q W| S)zE split a given URL into the protocoll, the hostname and the dir part z:*\/+maxsplit      N)resplitlenr7   )r    rF   r   r   r   r      s    r   c               @   s.   e  Z d  Z d d d d  Z d d   Z d S)DistInfoNz/usr/share/python-apt/templatesc          )   C   s  d |  _  g  |  _ t j j d  |  _ d  } t j d  } t j d  } | s y/ t d d d g d t	 j
   d	 j   } WnN t k
 r } z. | j t j k r t j d
 |  d } WYd  d  } ~ Xn X| |  _ i  } d | | f } t |  }	 d  }
 d  } xT|	 D]L} | j d d  } t |  d k  r?q| d	 j   } | d j   } | d k r}t |  |  _ q| d k r| |  _  q| d k r|  j |
 |  d  } t   }
 | |
 _ | |
 _ d | |
 _ q| d k r| |
 _ q| d k rUd |
 _ xL|  j D]5 } | j | k r|
 j j |  | j j |
  qWq| d k rvt j  |  |
 _! q| d k rt j  |  |
 _" q| d k r| |
 _# q| d k r|
 j$ r| |
 _$ q| d |  j k r| |
 _$ q| d k r|
 j% r| |
 _% q| d |  j k r1| |
 _% q| d k sP| d |  j k rt& j' j( |  rh| pt& j' j) t& j' j* | |   } | | k ri  } y? t |  , } t+ t, | j- d  d!   | D   } Wd  QRXWn" t. k
 rt/ d"  g  } Yn Xx | D]{ } | j0 d#  r0| j1 d$ |  } qt2 |  \ } } } | | k rh| | j3 | |  qt4 | | | |  | | <qW| | | <| | |
 _5 q| d% k rt |  |
 _6 q| d& k r| r|
 j7 | j  r|
 j8 j |  t9 |  } q| d' k r#| j: t |   q| d( k rE| j; t |   q| d) k r| j< |  qW|  j |
 |  d  }
 d  } Wd  QRXd  S)*N zAPT::Architecturez^#LOC:(.+)$zS^(#LOC:.+)|(((http)|(ftp)|(rsync)|(file)|(mirror)|(https))://[A-Za-z0-9/\.:\-_@]+)$Zlsb_releasez-iz-sstdoutr   z#lsb_release failed, using defaults:ZDebianz
%s/%s.info:   rC   ZChangelogURIZMetaReleaseURIZSuitez^%s$Z	MatchNameZParentSuiteTZ	AvailableZOfficialZRepositoryTypeZBaseURIz
BaseURI-%sZMatchURIzMatchURI-%sZMirrorsFilezMirrorsFile-%sc             S   s   g  |  ] } | j     q Sr   )strip)r   xr   r   r   r:      s   	 z%DistInfo.__init__.<locals>.<listcomp>z#WARNING: Failed to read mirror filez#LOC:z\1ZDescriptionr(   ZCompDescriptionZCompDescriptionLongZParentComponent)=Zmetarelease_uri	templatesapt_pkgconfigfindZarchrE   compiler   r   ZcommunicaterM   OSErrorerrnoENOENTloggingZwarningdistopenrF   rG   _changelogs_urifinish_templater   r   r   r
   r   r	   r7   r   Zstring_to_boolr   r   r   r   r   ospathisabsabspathjoinlistfiltermatch	Exceptionprint
startswithsubr   r5   r3   r   r   r   r   r(   r0   r1   r.   )r   rX   Zbase_dirr6   Z	match_locZmatch_mirror_lineexcZmap_mirror_setsZ
dist_fnameZ	dist_filetemplate	componentlinetokensZfieldvalueZnannyr   Zvalue_fZmirror_datar!   r"   r#   r   r   r   r      s    								!$

zDistInfo.__init__c             C   s   | s
 d S| j  d k rL | j rL x' | j D] } | j  r, | j  | _  Pq, W| j i  k r | j r x' | j D] } | j  rn | j | _ Pqn W| r | j | j  r | j j |  d } x | j D] } | j | _ q W|  j	 j |  d S)z finish the current tempalte N)
r   r   r	   r   r   r   r   r7   r   rO   )r   rj   rk   tr   r   r   r\     s$    		zDistInfo.finish_template)r%   r&   r'   r   r\   r   r   r   r   rH      s   rrH   __main__ZUbuntuz/usr/share/python-apt/templatesz

Suite: %szDesc: %szBaseURI: %szMatchURI: %szMirrors: %sz %s -%s -%sz  %s)%Z
__future__r   rU   rW   r]   
subprocessr   r   rE   rP   r   rZ   objectr   r(   r3   r8   r   rH   r%   dinfor[   rO   rj   r   r   r   r   r   rb   keysr   r   r)   r   r   r   r   r   r   <module>   s:    ! 