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 ¡ �qdqÎd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¡}|s z$tdddgdt	d	� 
¡ d
  ¡ }W nD ttfk
rž } z"|jtjkrŠt d| ¡ d}W 5 d }~X Y nX || _i }d||f }t|ƒ��l}	d }
d }|	D �]B}| dd¡}t|ƒdk rîqÎ|d
  ¡ }|d  ¡ }|dk�rt|ƒ| _qÎ|dk�r.|| _ qÎ|dk�rf|  |
|¡ d }tƒ }
||
_||
_d| |
_qÎ|dk�rx||
_qÎ|dk�r¼d|
_| jD ]*}|j|k�rŽ|
j |¡ |j  |
¡ �qŽqÎ|dk�rÔt !|¡|
_"qÎ|dk�rìt !|¡|
_#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�r‚t'j( )|¡�r�|�p¤t'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�r˜t|ƒ|
_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>    ! 




þ
