3
Ĥ[-                 @   s   d dl Z d dlZd dlZdadadadd e jjddjdD a	da
d!ddZd	d
 Zd"ddZd#ddZd$ddZd%ddZd&ddZd'ddZG dd deZG dd deZdd Zdd Zed kre  dS )(    Nc             C   s    g | ]}|j  rd |j   qS )zpython%s)strip).0Zver r   !/usr/share/python3/py3versions.py
<listcomp>
   s    r   ZDEBPYTHON3_SUPPORTED ,c             C   s~   ddl m}m} tsDtjjdrD| }td}|j| |j	  |atrz| rzytj
d| }W n |k
rt   tY nX |S d S )Nr   )ConfigParserNoOptionErrorz"/usr/share/python3/debian_defaultsZDEFAULT)Zconfigparserr	   r
   	_defaultsospathexistsopenZ	read_filecloseget
ValueError)namer	   r
   configZdefaultsfilevaluer   r   r   read_default   s    

r   c                sl  t | jddkrtddd l}|j|j|j|j|jd}i }t }tt	dd}d}x| jdD ]}|j
 }|d	krzqd|dkrqd|jdt  tjd}|j|}	yx|	std|	jd|	jd }
jd\}}t|dk rwd|
dk r|j n d}||
   fdd|D }W qd tk
r<   tdY qdX qdW d|krh||d< |rh|j||d< |S )Nr      zDtoo many arguments provided for X-Python3-Version: min and max only.r   )N=z>=z<=z<<T)version_onlyFallcurrentcurrent_extversionsz(>=|<=|<<|=)? *(\d\.\d)$z'error parsing Python3-Version attribute   .   r   c                s   g | ]} |r|qS r   r   )r   av)filtopvr   r   r   G   s    z"parse_versions.<locals>.<listcomp>)r   r   )Nr   )lensplitr   operatoreqgeleltsetsupported_versionsr   
setdefaultrecompilematchgroupintadd	Exceptionunion)vstringr&   Z	operatorsvinfoZexact_versionsZversion_rangeZ
relop_seenZfieldZvemopZvmajZvminr   )r"   r#   r   parse_versions'   sL    




r:   Fc             C   sX   t s>y td}dd |jdD a W n tk
r<   g a Y nX | rPdd t D S t S d S )Nzold-versionsc             S   s   g | ]}|j  qS r   )r   )r   sr   r   r   r   V   s    z old_versions.<locals>.<listcomp>r   c             S   s   g | ]}|d d qS )   Nr   )r   r#   r   r   r   r   Z   s    )_old_versionsr   r%   r   )r   r   r   r   r   old_versionsQ   s    
r>   c             C   sX   t s>y td}dd |jdD a W n tk
r<   g a Y nX | rPdd t D S t S d S )Nzunsupported-versionsc             S   s   g | ]}|j  qS r   )r   )r   r;   r   r   r   r   d   s    z(unsupported_versions.<locals>.<listcomp>r   c             S   s   g | ]}|d d qS )r<   Nr   )r   r#   r   r   r   r   h   s    )_unsupported_versionsr   r%   r   )r   r   r   r   r   unsupported_versions_   s    
r@   c       	      C   s&  t   tsy td}dd |jdD aW n tk
r   ddddg}y&d	d l}|j|d
d|jd}|j}W n$ t	k
r   t
jdj|}Y nX d }x0|D ](}|jdr|jdd
d
 j jd}qW |j  dd |D }|aY nX tjtjt}tj  tj| | rdd tD S tS d S )Nzsupported-versionsc             S   s   g | ]}|j  qS r   )r   )r   r;   r   r   r   r   s   s    z&supported_versions.<locals>.<listcomp>r   z/usr/bin/apt-cachez--no-all-versionsZshowzpython3-allr   r   F)bufsizeshellstdout zDepends::c             S   s   g | ]}t jd d|qS )z\s*(\S+)[ (]?.*z\1)r.   sub)r   r;   r   r   r   r      s    c             S   s   g | ]}|d d qS )r<   Nr   )r   r#   r   r   r   r      s    )default_version_supported_versionsr   r%   r   
subprocessPopenPIPErC   ImportErrorr   popenjoin
startswithr   r   popindex_default_versionsortappend)	r   r   cmdrI   pfdZdependslinedefaultr   r   r   r,   m   s8    




r,   c             C   sT   t stjda td}t |tjjd|fkr8td| |a | rLt dd  S t S d S )Nz/usr/bin/python3zdefault-versionz/usr/binzkthe symlink /usr/bin/python3 does not point to the python3 default version. It must be reset to point to %sr<   )rR   r   readlinkr   r   rN   r   )r   Zdebian_defaultr   r   r   rG      s    
rG   c             C   s   t   d }t| }tdd}t|dkr|d j|}g }x|D ]}|j| q@W y|j|jtdd  }W n   d}Y nX |j	  |r|j| nt
d|st
d|r|S d	d
 |D S d S )NT)r   r   r   r<   r   z/No supported python3 versions in version stringzempty set of versionsc             S   s   g | ]}d | qS )zpython%sr   )r   r#   r   r   r   r      s    z&requested_versions.<locals>.<listcomp>)rG   r:   r,   r$   intersectionrT   rP   rQ   rR   rS   r   )r6   r   r   r7   	supportedZvlversionrY   r   r   r   requested_versions   s,    

 
r^   c                sH   dd l }t   fdd|j dD }|j  | r@dd |D S |S d S )Nr   c                s(   g | ] }t jj| krt jj|qS r   )r   r   basename)r   r;   )r\   r   r   r      s   z&installed_versions.<locals>.<listcomp>z/usr/bin/python3.[0-9]c             S   s   g | ]}|d d qS )r<   Nr   )r   r#   r   r   r   r      s    )globr,   rS   )r   r`   r   r   )r\   r   installed_versions   s    
ra   c               @   s   e Zd ZdS )ControlFileValueErrorN)__name__
__module____qualname__r   r   r   r   rb      s   rb   c               @   s   e Zd ZdS )MissingVersionValueErrorN)rc   rd   re   r   r   r   r   rf      s   rf   c             C   s   d}d}d}t | dd}dd |D }W dQ R X x|D ]z}|dkr\|dkr\|dkrVP d}q8|jdrld}q8|jd	| r|}q8|j jd
r8|dkrtd|jddd j }q8W |dkrtd|dkr|dkrtd|S |S )zFread the debian/control file, extract the X-Python3-Version
    field.Nzutf-8)encodingc             S   s   g | ]}|j  qS r   )r   )r   rX   r   r   r   r      s    z/extract_pyversion_attribute.<locals>.<listcomp>r   SourcezSource:z	Package: zx-python3-version:z1attribute X-Python3-Version not in Source sectionrE   r   znot a control filez$no X-Python3-Version in control file)r   rO   lowerr   r%   r   rb   rf   )fnpkgr]   ZsversionZsectionZcontrolfilelinesrX   r   r   r   extract_pyversion_attribute   s2    

rm   c              C   s  ddl m}  d}| |d}|jddddd	d
 |jdddddd
 |jdddddd
 |jdddddd
 |jddddddd |j \}}tjjtjd }|j	ot
|dkrytt|j W n: tk
r } ztd| | tjd W Y d d }~X nX n|jr2t
|dkr2tdjt|j nt|jr`t
|dkr`tdjt|j nF|jrt
|dkrt
|dkrd }n|d }ytjj|r&|}yt|d!}	t|	|j}
W nb tk
r   tjjd"||f  tjd Y n0 tk
r"   tjjd#|  t|j}
Y nX nt||j}
tdj|
 W nB tk
r } z$tjjd$||f  tjd W Y d d }~X nX ntjjd%||f  tjd d S )&Nr   )OptionParserz;[-v] [-h] [-d|--default] [-s|--supported] [-i|--installed] )usagez-dz	--defaultz!print the default python3 version
store_truerY   )helpactiondestz-sz--supportedz$print the supported python3 versionsr\   z-rz--requestedzprint the python3 versions requested by a build; the argument is either the name of a control file or the value of the X-Python3-Version attribute	requestedz-iz--installedz.print the installed supported python3 versions	installedz-vz	--versionz print just the version number(s)Fr   )rq   rY   rr   rs   z%s:r   rD   zdebian/controlrh   z%s: not a control file: %s, zC%s: no X-Python3-Version in control file, using supported versions
z%s: %s
zusage: %s %s
)Zoptparsern   Z
add_option
parse_argsr   r   r_   sysargvrY   r$   printrG   r   r   exitr\   rN   r,   ru   ra   rt   isfilerm   r^   rb   stderrwriterf   )rn   ro   parserZoptsargsZprogrammsgr   rj   r6   Zvsr   r   r   main  sn    




 
r   __main__)N)F)F)F)F)F)F)r   r.   rw   r   r=   r?   environr   r%   rH   rR   r   r:   r>   r@   r,   rG   r^   ra   r   rb   rf   rm   r   rc   r   r   r   r   <module>   s,   
*


!


=A