o
    4ñÕf/8  ã                   @   sŽ  d dl mZ d dl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 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dd„ ZG dd„ deƒZedkrÃeddƒZe ej¡ ejD ]VZe dej ¡ e dej ¡ e dej ¡ e dej ¡ eji kr¡e 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 ¡ q·qndS dS )é    )Úprint_functionN)ÚPopenÚPIPE)Úgettextc           	   	   #   sl  � t ƒ }t | ¡�P}|D ],}d|v r| |d  d¡¡ d|v r/d|d v r( n| |d ¡ t|ƒ ¡ E dH  q	 W d  ƒ dS |D ]}d|v rQ| |d ¡ qDW d  ƒ n1 s\w   Y  t|ƒ�}t	t
t |¡ƒƒ}W d  ƒ n1 syw   Y  |D ]l}|d |v r‰q€dV  |d  d	d¡|d< t | ¡�I}|D ]>}d|vsªd|d vr«qžd
|v rÄ|d ‰ t‡ fdd„|d
  d¡D ƒƒrÄqžt|ƒjdi |¤Ž ¡ D ]}| d
¡rØqÐ|V  qÐqžW d  ƒ n1 sçw   Y  q€t | ¡�9}|D ]}d|v �rd|d v �r nqõ|D ]}d|v �rd|d v �r�qt|ƒ ¡ E dH  �qW d  ƒ dS 1 �s/w   Y  dS )a~  Expand the given template.

    A template file consists of a header, followed by paragraphs
    of templated suites, followed by a footer. A templated suite
    is any paragraph where the Suite field contains {.

    This function expands all templated suites using the information
    found in the CSV file supplied by distro-info-data.

    It yields lines of template info.
    zX-Exclude-Suitesz, ÚSuiteú{NÚseriesÚ Úversionz LTSz	X-Versionc                 3   sT   � | ]%}|  d ¡rt |dd… ˆ ¡dk p%|  d¡o%t |dd… ˆ ¡dkV  qdS )Úleé   Nr   Úge)Ú
startswithÚapt_pkgÚversion_compare)Ú.0Úfield©Úver© ú5/usr/lib/python3/dist-packages/aptsources/distinfo.pyÚ	<genexpr>Z   s   € 
üÿ

üz#_expand_template.<locals>.<genexpr>r   )Úsetr   ÚTagFileÚupdateÚsplitÚaddÚstrÚ
splitlinesÚopenÚreversedÚlistÚcsvÚ
DictReaderÚreplaceÚanyÚformatr   )	ÚtemplateÚcsv_pathÚknown_suitesÚtmplÚsectionÚ
csv_objectÚreleasesÚrelÚliner   r   r   Ú_expand_template&   sp   €ó€þñ
ÿû
ýñÿ€€ú$úr0   c                   @   ó$   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©Úselfr   r   r   Ú__init__y   s   
zTemplate.__init__c                 C   s   |dd„ | j D ƒv S )z8 Check if the distribution provides the given component c                 s   s   � | ]}|j V  qd S ©N)r4   )r   Úcr   r   r   r   ‹   s   € z)Template.has_component.<locals>.<genexpr>)r;   )rC   Úcompr   r   r   Úhas_component‰   s   zTemplate.has_componentc                 C   s.   t |ƒ\}}}|| jv r| j|  ||¡S dS )z8 Check if a given url of a repository is a valid mirror F)Ú	split_urlr>   Úhas_repository)rC   ÚurlÚprotoÚhostnameÚdirr   r   r   Ú	is_mirror�   s   
zTemplate.is_mirrorN)Ú__name__Ú
__module__Ú__qualname__rD   rH   rO   r   r   r   r   r2   w   s    r2   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 rE   )r4   r8   Údescription_longÚparent_component)rC   r4   ÚdescÚ	long_descrU   r   r   r   rD   ˜   s   
zComponent.__init__c                 C   ó   | j S rE   ©rU   rB   r   r   r   Úget_parent_componentž   ó   zComponent.get_parent_componentc                 C   ó
   || _ d S rE   rY   )rC   Úparentr   r   r   Úset_parent_component¡   ó   
zComponent.set_parent_componentc                 C   s$   | j d ur| j S | jd ur| jS d S rE   )rT   r8   rB   r   r   r   Úget_description¤   s
   

zComponent.get_descriptionc                 C   r\   rE   )r8   ©rC   rV   r   r   r   Úset_description¬   r_   zComponent.set_descriptionc                 C   r\   rE   ©rT   ra   r   r   r   Úset_description_long¯   r_   zComponent.set_description_longc                 C   rX   rE   rc   rB   r   r   r   Úget_description_long²   r[   zComponent.get_description_long)NNN)
rP   rQ   rR   rD   rZ   r^   r`   rb   rd   re   r   r   r   r   rS   –   s    
rS   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 rE   )rM   ÚrepositoriesÚadd_repositoryÚlocation)rC   rL   rM   rN   ri   r   r   r   rD   ¹   s   
zMirror.__init__c                 C   s   | j  t||ƒ¡ d S rE   )rg   ÚappendÚ
Repository©rC   rL   rN   r   r   r   rh   ¿   s   zMirror.add_repositoryc                    s   ‡ fdd„| j D ƒS )Nc                    s   g | ]	}|j ˆ kr|‘qS r   ©rL   ©r   Úrrm   r   r   Ú
<listcomp>Ã   s    z5Mirror.get_repositories_for_proto.<locals>.<listcomp>©rg   )rC   rL   r   rm   r   Úget_repositories_for_protoÂ   ó   z!Mirror.get_repositories_for_protoc                 C   s6   |d u rdS | j D ]}|j|kr||jv r dS q	dS r3   )rg   rL   rN   )rC   rL   rN   ro   r   r   r   rJ   Å   s   
€zMirror.has_repositoryc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s   g | ]}|  ˆ j¡‘qS r   )Úget_urlrM   rn   rB   r   r   rp   Î   s    z(Mirror.get_repo_urls.<locals>.<listcomp>rq   rB   r   rB   r   Úget_repo_urlsÍ   rs   zMirror.get_repo_urlsc                 C   rX   rE   ©ri   rB   r   r   r   Úget_locationÐ   r[   zMirror.get_locationc                 C   r\   rE   rv   )rC   ri   r   r   r   Úset_locationÓ   r_   zMirror.set_locationrE   )rP   rQ   rR   Ú__doc__rD   rh   rr   rJ   ru   rw   rx   r   r   r   r   rf   ¶   s    
rf   c                   @   r1   )rk   c                 C   s   || _ || _d S rE   ©rL   rN   rl   r   r   r   rD   Ù   s   
zRepository.__init__c                 C   s   | j | jfS rE   rz   rB   r   r   r   Úget_infoÝ   s   zRepository.get_infoc                 C   s   d| j || jf S )Nz
%s://%s/%srz   )rC   rM   r   r   r   rt   à   s   zRepository.get_urlN)rP   rQ   rR   rD   r{   rt   r   r   r   r   rk   ×   s    rk   c                 C   s6   t jd| dd�}t|ƒdk r| d¡ t|ƒdk s|S )zE split a given URL into the protocoll, the hostname and the dir part z:*\/+é   )Úmaxsplitr   N)Úrer   Úlenrj   )rK   r   r   r   r   rI   ä   s
   
ÿrI   c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚDistInfoNú/usr/share/python-apt/templatesc              
   C   s8  d| _ g | _tj d¡| _d }t d¡}t d¡}|sPztg d¢dt	d� 
¡ d  ¡ }W n# ttfyO } z|jtjkrCt d	| ¡ d
}W Y d }~nd }~ww || _i }d||f }d | ¡ ¡}	d }
d }t||	ƒD �]£}| dd¡}t|ƒdk r{qk|d  ¡ }|d  ¡ }|dkr‘t|ƒ| _qk|dkr™|| _ qk|dkr´|  |
|¡ d }tƒ }
||
_||
_d| |
_qk|dkr¼||
_qk|dkrÛd|
_| jD ]}|j|krÙ|
j  !|¡ |j" !|
¡ qÆqk|dkræt #|¡|
_$qk|dkrñt #|¡|
_%qk|dkrù||
_&qk|dk�r|
j'�s||
_'qk|d| j k�r||
_'qk|dk�r|
j(�s||
_(qk|d| j k�r+||
_(qk|dk�s8|d| j k�rÆt)j* +|¡�rB|�pLt)j* ,t)j* -||¡¡}||v�rÀi }z$t.|ƒ�}t/t0|j1dd „ |D ƒƒƒ}W d   ƒ n	1 �srw   Y  W n t2�yˆ   t3d!ƒ g }Y nw |D ]0}| 4d"¡�r›| 5d#|¡}�q‹t6|ƒ\}}}||v �r±||  7||¡ �q‹t8||||ƒ||< �q‹|||< || |
_9qk|d$k�rÑt|ƒ|
_:qk|d%k�rë|�ræ|
 ;|j¡�sæ|
j< !|¡ t=|ƒ}qk|d&k�rø| >t|ƒ¡ qk|d'k�r| ?t|ƒ¡ qk|d(k�r| @|¡ qk|  |
|¡ d }
d }d S ))Nr	   zAPT::Architecturez^#LOC:(.+)$zS^(#LOC:.+)|(((http)|(ftp)|(rsync)|(file)|(mirror)|(https))://[A-Za-z0-9/\.:\-_@]+)$)Úlsb_releasez-iz-sT)Úuniversal_newlinesÚstdoutr   z#lsb_release failed, using defaults:ÚDebianz
%s/%s.infoz/usr/share/distro-info/{}.csvú:é   r|   ÚChangelogURIÚMetaReleaseURIr   z^%s$Ú	MatchNameÚParentSuiteÚ	AvailableÚOfficialÚRepositoryTypeÚBaseURIz
BaseURI-%sÚMatchURIzMatchURI-%sÚMirrorsFilezMirrorsFile-%sc                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r   Úxr   r   r   rp   A  s    z%DistInfo.__init__.<locals>.<listcomp>z#WARNING: Failed to read mirror filez#LOC:z\1ÚDescriptionrS   ÚCompDescriptionÚCompDescriptionLongÚParentComponent)AÚmetarelease_uriÚ	templatesr   ÚconfigÚfindÚarchr~   Úcompiler   r   Úcommunicater’   ÚOSErrorÚIOErrorÚerrnoÚENOENTÚloggingÚwarningÚdistr&   Úlowerr0   r   r   Ú_Úchangelogs_uriÚfinish_templater2   r4   r?   r7   r5   r6   rj   r<   Ústring_to_boolr@   rA   r:   r9   r=   ÚosÚpathÚisabsÚabspathÚjoinr   r!   ÚfilterÚmatchÚ	ExceptionÚprintr   ÚsubrI   rh   rf   r>   r8   rH   r;   rS   rb   rd   r^   )rC   r¥   Úbase_dirri   Ú	match_locÚmatch_mirror_lineÚexcÚmap_mirror_setsÚ
dist_fnameÚ	csv_fnamer'   Ú	componentr/   Útokensr   ÚvalueÚnannyr>   Úvalue_fÚmirror_datarL   rM   rN   r   r   r   rD   î   sô   
ÿþþþÿ€ü

€ü
ÿ

þÿ€þ
ÿ


ÿ




€zDistInfo.__init__c                 C   s¨   |sdS |j du r|jr|jD ]}|j r|j |_  nq|ji kr2|jr2|jD ]}|j r1|j|_ nq&|rB| |j¡sB|j |¡ d}|jD ]}|j|_qE| j	 |¡ dS )z finish the current tempalte N)
r=   r5   r6   r>   rH   r4   r;   rj   rA   r™   )rC   r'   r¼   Útr   r   r   r©   b  s(   
þ
þ

zDistInfo.finish_template)Nr�   )rP   rQ   rR   rD   r©   r   r   r   r   r€   ì   s    
tr€   Ú__main__ÚUbuntur�   z

Suite: %szDesc: %szBaseURI: %szMatchURI: %szMirrors: %sz %s -%s -%sz  %s)'Ú
__future__r   r"   r¡   r£   r«   Ú
subprocessr   r   r~   r   r   r§   r0   Úobjectr2   rS   rf   rk   rI   r€   rP   ÚdÚinfor¨   r™   r'   r4   r8   r9   r=   r>   r!   Úkeysr;   rG   rT   r<   r5   r   r   r   r   Ú<module>   sJ   Q ! 





þ
ÿò