a
    ?i/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 ]Zedej  edej  edej  edej  eji krFede ej!   ej"D ] Z#ede#je#je#j$f  qLej%D ]Z&ede&j  qtqdS )    )print_functionN)PopenPIPE)gettextc           	   	   #   s  t  }t| }|D ]X}d|v r6||d d d|v r\d|d v rN q||d  t| E dH  qW d   dS |D ]}d|v r||d  qW d   n1 s0    Y  t|"}t	t
t|}W d   n1 s0    Y  |D ]}|d |v rqdV  |d d	d|d< t| }|D ]}d|vs:d|d vrZq:d
|v r|d  t fdd|d
 dD rq:t|jf i | D ]}|d
rq|V  qq:W d   q1 s0    Y  qt| t}|D ]$}d|v rd|d v r q$q|D ]4}d|v rHd|d v rHq(t| E dH  q(W d   n1 st0    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   sR   | ]J}| d r(t|dd  dk pH| doHt|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>)setr   ZTagFileupdatesplitaddstr
splitlinesopenreversedlistcsvZ
DictReaderreplaceanyformatr   )	templateZcsv_pathZknown_suitesZtmplZsectionZ
csv_objectZreleasesZrelliner   r   r   _expand_template&   sV    .
0

0
r$   c                   @   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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          z)Template.has_component.<locals>.<genexpr>)r.   )r6   compr   r   r   has_component   s    zTemplate.has_componentc                 C   s2   t |\}}}|| jv r*| j| ||S dS dS )z8 Check if a given url of a repository is a valid mirror FN)	split_urlr1   has_repository)r6   urlprotohostnamedirr   r   r   	is_mirror   s    
zTemplate.is_mirrorN)__name__
__module____qualname__r7   r<   rC   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 r8   )r'   r+   description_longparent_component)r6   r'   descZ	long_descrI   r   r   r   r7      s    zComponent.__init__c                 C   s   | j S r8   rI   r5   r   r   r   get_parent_component   s    zComponent.get_parent_componentc                 C   s
   || _ d S r8   rK   )r6   parentr   r   r   set_parent_component   s    zComponent.set_parent_componentc                 C   s(   | j d ur| j S | jd ur | jS d S d S r8   )rH   r+   r5   r   r   r   get_description   s
    

zComponent.get_descriptionc                 C   s
   || _ d S r8   )r+   r6   rJ   r   r   r   set_description   s    zComponent.set_descriptionc                 C   s
   || _ d S r8   rH   rP   r   r   r   set_description_long   s    zComponent.set_description_longc                 C   s   | j S r8   rR   r5   r   r   r   get_description_long   s    zComponent.get_description_long)NNN)
rD   rE   rF   r7   rL   rN   rO   rQ   rS   rT   r   r   r   r   rG      s   
rG   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 r8   )rA   repositoriesadd_repositorylocation)r6   r@   rA   rB   rX   r   r   r   r7      s    zMirror.__init__c                 C   s   | j t|| d S r8   )rV   append
Repositoryr6   r@   rB   r   r   r   rW      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>   r:   z5Mirror.get_repositories_for_proto.<locals>.<listcomp>rV   )r6   r@   r   r\   r   get_repositories_for_proto   s    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&   )rV   r@   rB   )r6   r@   rB   r^   r   r   r   r>      s    
zMirror.has_repositoryc                    s    fdd j D S )Nc                    s   g | ]}|  jqS r   )get_urlrA   r]   r5   r   r   r_      r:   z(Mirror.get_repo_urls.<locals>.<listcomp>r`   r5   r   r5   r   get_repo_urls   s    zMirror.get_repo_urlsc                 C   s   | j S r8   rX   r5   r   r   r   get_location   s    zMirror.get_locationc                 C   s
   || _ d S r8   rd   )r6   rX   r   r   r   set_location   s    zMirror.set_location)N)rD   rE   rF   __doc__r7   rW   ra   r>   rc   re   rf   r   r   r   r   rU      s   
rU   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )rZ   c                 C   s   || _ || _d S r8   r@   rB   r[   r   r   r   r7      s    zRepository.__init__c                 C   s   | j | jfS r8   rh   r5   r   r   r   get_info   s    zRepository.get_infoc                 C   s   d| j || jf S )Nz
%s://%s/%srh   )r6   rA   r   r   r   rb      s    zRepository.get_urlN)rD   rE   rF   r7   ri   rb   r   r   r   r   rZ      s   rZ   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:*\/+   )maxsplitr   N)rer   lenrY   )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   sH  d| _ g | _tjd| _d }td}td}|sz"tg ddt	d
 d  }W nF ttfy } z*|jtjkrtd	|  d
}W Y d }~n
d }~0 0 || _i }d||f }d| }	d }
d }t||	D ]V}|dd}t|dk rq|d  }|d  }|dkr$t|| _q|dkr6|| _ q|dkrn| |
| d }t }
||
_||
_d| |
_q|dkr||
_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r8||
_'q|dkrR|
j(sR||
_(q|d| j krj||
_(q|dks|d| j krt)j*+|r|pt)j*,t)j*-||}||vri }zHt.|*}t/t0|j1dd  |D }W d    n1 s0    Y  W n  t2y$   t3d! g }Y n0 |D ]^}|4d"rJ|5d#|}q*t6|\}}}||v rt|| 7|| nt8||||||< q*|||< || |
_9q|d$krt||
_:q|d%kr|r|
;|js|
j<!| t=|}q|d&kr|>t| q|d'kr|?t| q|d(kr|@| q| |
| 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:   rj   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   r_   A  r:   z%DistInfo.__init__.<locals>.<listcomp>z#WARNING: Failed to read mirror filez#LOC:z\1ZDescriptionrG   ZCompDescriptionZCompDescriptionLongZParentComponent)AZmetarelease_uri	templatesr   ZconfigfindZarchrl   compiler   r   Zcommunicaters   OSErrorIOErrorerrnoZENOENTloggingZwarningdistr!   lowerr$   r   rm   _changelogs_urifinish_templater%   r'   r2   r*   r(   r)   rY   r/   Zstring_to_boolr3   r4   r-   r,   r0   ospathisabsabspathjoinr   r   filtermatch	Exceptionprintr   subr=   rW   rU   r1   r+   r<   r.   rG   rQ   rS   rN   )r6   r|   Zbase_dirrX   Z	match_locZmatch_mirror_lineexcZmap_mirror_setsZ
dist_fnameZ	csv_fnamer"   	componentr#   tokensr   valueZnannyr1   Zvalue_fZmirror_datar@   rA   rB   r   r   r   r7      s    












*







zDistInfo.__init__c                 C   s   |sdS |j du 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)
r0   r(   r)   r1   r<   r'   r.   rY   r4   ru   )r6   r"   r   tr   r   r   r   b  s$    



zDistInfo.finish_template)Nro   )rD   rE   rF   r7   r   r   r   r   r   rn      s   
trn   __main__ZUbunturo   z

Suite: %szDesc: %szBaseURI: %szMatchURI: %szMirrors: %sz %s -%s -%sz  %s)'Z
__future__r   r   rz   r{   r   
subprocessr   r   rl   r   r   r~   r$   objectr%   rG   rU   rZ   r=   rn   rD   dinfor   ru   r"   r'   r+   r,   r0   r1   r   keysr.   r;   rH   r/   r(   r   r   r   r   <module>   sB   Q ! 





