o
    ra/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re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rede ej!   ej"D ]Z#ede#je#je#j$f  qej%D ]
Z&ede&j  qqnd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 rqdV  |d d	d|d< t| I}|D ]>}d|vsd|d vrqd
|v r|d  t fdd|d
 dD rqt|jdi | D ]}|d
rq|V  qqW d   n1 sw   Y  qt| 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{NZ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_pkgZversion_compare).0fieldZver 5/usr/lib/python3/dist-packages/aptsources/distinfo.py	<genexpr>Z   s    


z#_expand_template.<locals>.<genexpr>r   )setr   ZTagFileupdatesplitaddstr
splitlinesopenreversedlistcsvZ
DictReaderreplaceanyformatr   )	templateZcsv_pathZknown_suitesZtmplsectionZ
csv_objectZreleasesZrelliner   r   r   _expand_template&   sp   

$r%   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)r)   )r   cr   r   r   r      s    z)Template.has_component.<locals>.<genexpr>)r0   )r8   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_urlr3   has_repository)r8   urlprotohostnamedirr   r   r   	is_mirror   s   
zTemplate.is_mirrorN)__name__
__module____qualname__r9   r=   rD   r   r   r   r   r'   w   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)r8   r)   descZ	long_descrJ   r   r   r   r9      s   
zComponent.__init__c                 C      | j S r:   rJ   r7   r   r   r   get_parent_component      zComponent.get_parent_componentc                 C   
   || _ d S r:   rM   )r8   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 r:   )rI   r-   r7   r   r   r   get_description   s
   

zComponent.get_descriptionc                 C   rP   r:   )r-   r8   rK   r   r   r   set_description   rS   zComponent.set_descriptionc                 C   rP   r:   rI   rU   r   r   r   set_description_long   rS   zComponent.set_description_longc                 C   rL   r:   rW   r7   r   r   r   get_description_long   rO   zComponent.get_description_long)NNN)
rE   rF   rG   r9   rN   rR   rT   rV   rX   rY   r   r   r   r   rH      s    
rH   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:   )rB   repositoriesadd_repositorylocation)r8   rA   rB   rC   r]   r   r   r   r9      s   
zMirror.__init__c                 C   s   | j t|| d S r:   )r[   append
Repositoryr8   rA   rC   r   r   r   r\      s   zMirror.add_repositoryc                    s    fdd| j D S )Nc                    s   g | ]	}|j  kr|qS r   rA   r   rra   r   r   
<listcomp>   s    z5Mirror.get_repositories_for_proto.<locals>.<listcomp>r[   )r8   rA   r   ra   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 r(   )r[   rA   rC   )r8   rA   rC   rc   r   r   r   r?      s   
zMirror.has_repositoryc                    s    fdd j D S )Nc                    s   g | ]}|  jqS r   )get_urlrB   rb   r7   r   r   rd      s    z(Mirror.get_repo_urls.<locals>.<listcomp>re   r7   r   r7   r   get_repo_urls   rg   zMirror.get_repo_urlsc                 C   rL   r:   r]   r7   r   r   r   get_location   rO   zMirror.get_locationc                 C   rP   r:   rj   )r8   r]   r   r   r   set_location   rS   zMirror.set_locationr:   )rE   rF   rG   __doc__r9   r\   rf   r?   ri   rk   rl   r   r   r   r   rZ      s    
rZ   c                   @   r&   )r_   c                 C   s   || _ || _d S r:   rA   rC   r`   r   r   r   r9      s   
zRepository.__init__c                 C   s   | j | jfS r:   rn   r7   r   r   r   get_info   s   zRepository.get_infoc                 C   s   d| j || jf S )Nz
%s://%s/%srn   )r8   rB   r   r   r   rh      s   zRepository.get_urlN)rE   rF   rG   r9   ro   rh   r   r   r   r   r_      s    r_   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   lenr^   )r@   r   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   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rt|| _qk|dkr|| _ qk|dkr| |
| d }t }
||
_||
_d| |
_qk|dkr||
_qk|dkrd|
_| jD ]}|j|kr|
j !| |j"!|
 qqk|dkrt#||
_$qk|dkrt#||
_%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rt)j*+|rB|pLt)j*,t)j*-||}||vri }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#|}qt6|\}}}||v r|| 7|| qt8||||||< q|||< || |
_9qk|d$krt||
_: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/\.:\-_@]+)$)Zlsb_releasez-iz-sT)Zuniversal_newlinesstdoutr   z#lsb_release failed, using defaults:ZDebianz
%s/%s.infoz/usr/share/distro-info/{}.csv:   rp   ZChangelogURIZMetaReleaseURIr   z^%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   rd   A  s    z%DistInfo.__init__.<locals>.<listcomp>z#WARNING: Failed to read mirror filez#LOC:z\1ZDescriptionrH   ZCompDescriptionZCompDescriptionLongZParentComponent)AZmetarelease_uri	templatesr   ZconfigfindZarchrr   compiler   r   Zcommunicatery   OSErrorIOErrorerrnoENOENTloggingZwarningdistr!   lowerr%   r   rs   _changelogs_urifinish_templater'   r)   r4   r,   r*   r+   r^   r1   Zstring_to_boolr5   r6   r/   r.   r2   ospathisabsabspathjoinr   r   filtermatch	Exceptionprintr   subr>   r\   rZ   r3   r-   r=   r0   rH   rV   rX   rR   )r8   r   base_dirr]   Z	match_locZmatch_mirror_lineexcZmap_mirror_setsZ
dist_fnameZ	csv_fnamer"   	componentr$   tokensr   valueZnannyr3   Zvalue_fZmirror_datarA   rB   rC   r   r   r   r9      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)
r2   r*   r+   r3   r=   r)   r0   r^   r6   r{   )r8   r"   r   tr   r   r   r   b  s(   



zDistInfo.finish_template)Nru   )rE   rF   rG   r9   r   r   r   r   r   rt      s    
trt   __main__ZUbunturu   z

Suite: %szDesc: %szBaseURI: %szMatchURI: %szMirrors: %sz %s -%s -%sz  %s)'Z
__future__r   r   r   r   r   
subprocessr   r   rr   r   r   r   r%   objectr'   rH   rZ   r_   r>   rt   rE   dinfor   r{   r"   r)   r-   r.   r2   r3   r   keysr0   r<   rI   r1   r*   r   r   r   r   <module>   sJ   Q ! 






