U
    &cr-                     @   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 G dd deZG dd deZG d	d
 d
eZG dd deZdd ZG dd deZedkr~eddZeej ejD ]Zedej  edej  edej  edej  eji kr6edeej   ej D ] Z!ede!je!je!j"f  q<ej#D ]Z$ede$j  qdqdS )    )print_functionN)PopenPIPE)gettextc                   @   s$   e Zd Zdd Zdd Zdd ZdS )Templatec                 C   sX   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   s2   t |\}}}|| jkr*| j| ||S dS d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                   @   sF   e 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 )	ComponentNc                 C   s   || _ || _|| _|| _d S r   )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 r   r-   r   r   r   r   get_parent_componentL   s    zComponent.get_parent_componentc                 C   s
   || _ d S r   r/   )r   parentr   r   r   set_parent_componentO   s    zComponent.set_parent_componentc                 C   s(   | j d k	r| j S | jd k	r | jS d S d S r   )r,   r   r   r   r   r   get_descriptionR   s
    

zComponent.get_descriptionc                 C   s
   || _ d S r   )r   r   r.   r   r   r   set_descriptionZ   s    zComponent.set_descriptionc                 C   s
   || _ d S r   r,   r4   r   r   r   set_description_long]   s    zComponent.set_description_longc                 C   s   | j S r   r6   r   r   r   r   get_description_long`   s    zComponent.get_description_long)NNN)
r(   r)   r*   r   r0   r2   r3   r5   r7   r8   r   r   r   r   r+   D   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 )Mirrorz( Storage for mirror related information Nc                 C   s"   || _ g | _| || || _d S r   )r%   repositoriesadd_repositorylocation)r   r$   r%   r&   r<   r   r   r   r   g   s    zMirror.__init__c                 C   s   | j t|| d S r   )r:   append
Repositoryr   r$   r&   r   r   r   r;   m   s    zMirror.add_repositoryc                    s    fdd| j D S )Nc                    s   g | ]}|j  kr|qS r   r$   r   rr@   r   r   
<listcomp>q   s     
 z5Mirror.get_repositories_for_proto.<locals>.<listcomp>r:   )r   r$   r   r@   r   get_repositories_for_protop   s    z!Mirror.get_repositories_for_protoc                 C   s6   |d krdS | j D ]}|j|kr||jkr dS qdS r   )r:   r$   r&   )r   r$   r&   rB   r   r   r   r"   s   s    
zMirror.has_repositoryc                    s    fdd j D S )Nc                    s   g | ]}|  jqS r   )get_urlr%   rA   r   r   r   rC   |   s     z(Mirror.get_repo_urls.<locals>.<listcomp>rD   r   r   r   r   get_repo_urls{   s    zMirror.get_repo_urlsc                 C   s   | j S r   r<   r   r   r   r   get_location~   s    zMirror.get_locationc                 C   s
   || _ d S r   rH   )r   r<   r   r   r   set_location   s    zMirror.set_location)N)r(   r)   r*   __doc__r   r;   rE   r"   rG   rI   rJ   r   r   r   r   r9   d   s   
r9   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r>   c                 C   s   || _ || _d S r   r$   r&   r?   r   r   r   r      s    zRepository.__init__c                 C   s   | j | jfS r   rL   r   r   r   r   get_info   s    zRepository.get_infoc                 C   s   d| j || jf S )Nz
%s://%s/%srL   )r   r%   r   r   r   rF      s    zRepository.get_urlN)r(   r)   r*   r   rM   rF   r   r   r   r   r>      s   r>   c                 C   s,   t jd| dd}t|dk r(|d q|S )zE split a given URL into the protocoll, the hostname and the dir part z:*\/+   )maxsplit   N)resplitlenr=   )r#   rR   r   r   r   r!      s    r!   c                   @   s   e Zd ZdddZdd ZdS )DistInfoN/usr/share/python-apt/templatesc                 C   s6  d| _ g | _tjd| _d }td}td}|sz$tdddgdt	d	
 d
  }W nD ttfk
r } z"|jtjkrtd|  d}W 5 d }~X Y nX || _i }d||f }t|l}	d }
d }|	D ]B}|dd}t|dk rq|d
  }|d  }|dkrt|| _q|dkr.|| _ q|dkrf| |
| d }t }
||
_||
_d| |
_q|dkrx||
_q|dkrd|
_| jD ]*}|j|kr|
j| |j |
 qq|dkrt!||
_"q|dkrt!||
_#q|dkr||
_$q|dkr|
j%s||
_%q|d| j kr0||
_%q|dkrJ|
j&sJ||
_&q|d| j krb||
_&q|dks||d| j krt'j()|r|pt'j(*t'j(+||}||krvi }z2t| }t,t-|j.d d! |D }W 5 Q R X W n" t/k
r   t0d" g }Y nX |D ]^}|1d#r.|2d$|}qt3|\}}}||krX|| 4|| nt5||||||< q|||< || |
_6q|d%krt||
_7q|d&kr|r|
8|js|
j9| t:|}q|d'kr|;t| q|d(kr |<t| q|d)kr|=| q| |
| d }
d }W 5 Q R X d S )*N zAPT::Architecturez^#LOC:(.+)$zS^(#LOC:.+)|(((http)|(ftp)|(rsync)|(file)|(mirror)|(https))://[A-Za-z0-9/\.:\-_@]+)$Zlsb_releasez-iz-sT)Zuniversal_newlinesstdoutr   z#lsb_release failed, using defaults:ZDebianz
%s/%s.info:   rN   ZChangelogURIZMetaReleaseURIZSuitez^%s$Z	MatchNameZParentSuiteZ	AvailableZOfficialZRepositoryTypeZBaseURIz
BaseURI-%sZMatchURIzMatchURI-%sZMirrorsFilezMirrorsFile-%sc                 S   s   g | ]}|  qS r   )strip)r   xr   r   r   rC      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_pkgZconfigfindZarchrQ   compiler   r   ZcommunicaterZ   OSErrorIOErrorerrnoZENOENTloggingZwarningdistopenrR   rS   _changelogs_urifinish_templater   r   r   r   r	   r
   r=   r   Zstring_to_boolr   r   r   r   r   ospathisabsabspathjoinlistfiltermatch	Exceptionprint
startswithsubr!   r;   r9   r   r   r    r   r+   r5   r7   r2   )r   rd   Zbase_dirr<   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r6|jr6|jD ]}|j r|j |_  q6q|ji krd|jrd|jD ]}|j rL|j|_ qdqL|r||js|j| d}|jD ]}|j|_q| j	| dS )z finish the current tempalte N)
r   r	   r
   r   r    r   r   r=   r   r\   )r   rv   rw   tr   r   r   rh     s$    



zDistInfo.finish_template)NrU   )r(   r)   r*   r   rh   r   r   r   r   rT      s   
srT   __main__ZUbunturU   z

Suite: %szDesc: %szBaseURI: %szMatchURI: %szMirrors: %sz %s -%s -%sz  %s)%Z
__future__r   rb   rc   ri   
subprocessr   r   rQ   r]   r   rf   objectr   r+   r9   r>   r!   rT   r(   dinforg   r\   rv   r   r   r   r   r   rn   keysr   r   r,   r   r	   r   r   r   r   <module>   s>    ! 





