3
	`4-                 @   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reddZejej xejD ]Zejdej  ejdej  ejdej  ejdej  eji kr8ejdeejj   x*ej D ] Z!ejde!je!je!j"f  q@W x ej#D ]Z$ejde$j  qlW qW 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| 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 )N)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 )N)r*   )r   r   r   r   get_parent_componentL   s    zComponent.get_parent_componentc             C   s
   || _ d S )N)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 )N)r)   r   )r   r   r   r   get_descriptionR   s
    

zComponent.get_descriptionc             C   s
   || _ d S )N)r   )r   r+   r   r   r   set_descriptionZ   s    zComponent.set_descriptionc             C   s
   || _ d S )N)r)   )r   r+   r   r   r   set_description_long]   s    zComponent.set_description_longc             C   s   | j S )N)r)   )r   r   r   r   get_description_long`   s    zComponent.get_description_long)NNN)
r%   r&   r'   r   r,   r.   r/   r0   r1   r2   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 | _| j|| || _d S )N)r"   repositoriesadd_repositorylocation)r   r!   r"   r#   r6   r   r   r   r   g   s    zMirror.__init__c             C   s   | j jt|| d S )N)r4   append
Repository)r   r!   r#   r   r   r   r5   m   s    zMirror.add_repositoryc                s    fdd| j D S )Nc                s   g | ]}|j  kr|qS r   )r!   )r   r)r!   r   r   
<listcomp>q   s    z5Mirror.get_repositories_for_proto.<locals>.<listcomp>)r4   )r   r!   r   )r!   r   get_repositories_for_protop   s    z!Mirror.get_repositories_for_protoc             C   s8   |d krdS x&| j D ]}|j|kr||jkrdS qW dS )NFT)r4   r!   r#   )r   r!   r#   r9   r   r   r   r   s   s    zMirror.has_repositoryc                s    fdd j D S )Nc                s   g | ]}|j  jqS r   )get_urlr"   )r   r9   )r   r   r   r:   |   s    z(Mirror.get_repo_urls.<locals>.<listcomp>)r4   )r   r   )r   r   get_repo_urls{   s    zMirror.get_repo_urlsc             C   s   | j S )N)r6   )r   r   r   r   get_location~   s    zMirror.get_locationc             C   s
   || _ d S )N)r6   )r   r6   r   r   r   set_location   s    zMirror.set_location)N)r%   r&   r'   __doc__r   r5   r;   r   r=   r>   r?   r   r   r   r   r3   d   s   
r3   c               @   s$   e Zd Zdd Zdd Zdd ZdS )r8   c             C   s   || _ || _d S )N)r!   r#   )r   r!   r#   r   r   r   r      s    zRepository.__init__c             C   s   | j | jfS )N)r!   r#   )r   r   r   r   get_info   s    zRepository.get_infoc             C   s   d| j || jf S )Nz
%s://%s/%s)r!   r#   )r   r"   r   r   r   r<      s    zRepository.get_urlN)r%   r&   r'   r   rA   r<   r   r   r   r   r8      s   r8   c             C   s0   t jd| dd}xt|dk r*|jd qW |S )zE split a given URL into the protocoll, the hostname and the dir part z:*\/+   )maxsplit   N)resplitlenr7   )r    rF   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   sD  d| _ g | _tjjd| _d }tjd}tjd}|sy"tdddgt	dj
 d	 j }W n@ tk
r } z$|jtjkrtjd
|  d}W Y d d }~X nX || _i }d||f }t|}	d }
d }xZ|	D ]P}|jdd}t|dk rq|d	 j }|d j }|dkrt|| _q|dkr,|| _ q|dkrd| j|
| d }t }
||
_||
_d| |
_q|dkrv||
_q|dkrd|
_x4| jD ]*}|j|kr|
jj| |jj|
 qW q|dkrtj ||
_!q|dkrtj ||
_"q|dkr ||
_#q|dkr|
j$ r||
_$q|d| j kr4||
_$q|dkrP|
j% rP||
_%q|d| j krh||
_%q|dks|d| j krt&j'j(|r|pt&j'j)t&j'j*||}||kri }y2t| }t+t,|j-d d! |D }W d Q R X W n" t.k
r   t/d" g }Y nX xf|D ]^}|j0d#r6|j1d$|}qt2|\}}}||kr`|| j3|| nt4||||||< qW |||< || |
_5q|d%krt||
_6q|d&kr|r|
j7|j r|
j8j| t9|}q|d'kr|j:t| q|d(kr|j;t| q|d)kr|j<| qW | j|
| d }
d }W d 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-s)stdoutr   z#lsb_release failed, using defaults:ZDebianz
%s/%s.info:   rB   ZChangelogURIZMetaReleaseURIZSuitez^%s$Z	MatchNameZParentSuiteTZ	AvailableZOfficialZRepositoryTypeZBaseURIz
BaseURI-%sZMatchURIzMatchURI-%sZMirrorsFilezMirrorsFile-%sc             S   s   g | ]}|j  qS r   )strip)r   xr   r   r   r:      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_pkgconfigfindZarchrE   compiler   r   ZcommunicaterN   OSErrorerrnoENOENTloggingZwarningdistopenrF   rG   _changelogs_urifinish_templater   r   r   r
   r   r	   r7   r   Zstring_to_boolr   r   r   r   r   ospathisabsabspathjoinlistfiltermatch	Exceptionprint
startswithsubr   r5   r3   r   r   r   r   r(   r0   r1   r.   )r   rY   Zbase_dirr6   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r8|jr8x|jD ]}|j r |j |_ P q W |ji krh|jrhx|jD ]}|j rP|j|_P qPW |r|j|j r|jj| d}x|jD ]}|j|_qW | j	j| dS )z finish the current tempalte N)
r   r   r	   r   r   r   r   r7   r   rP   )r   rk   rl   tr   r   r   r]     s$    zDistInfo.finish_template)NrI   )r%   r&   r'   r   r]   r   r   r   r   rH      s   
rrH   __main__ZUbuntuz/usr/share/python-apt/templatesz

Suite: %szDesc: %szBaseURI: %szMatchURI: %szMirrors: %sz %s -%s -%sz  %s)%Z
__future__r   rV   rX   r^   
subprocessr   r   rE   rQ   r   r[   objectr   r(   r3   r8   r   rH   r%   dinfor\   rP   rk   r   r   r   r   r   rc   keysr   r   r)   r   r   r   r   r   r   <module>   s<    ! 


