o
    4f/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{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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/\.:\-_@]+)$)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 ! 






