B
    
\-                 @   s   d dl Z d dlZd dlZdadadadd e jdd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 | ]}|  rd |   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drD| }td}|| |	  |atrz| rzyt
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
   ZconfigZdefaultsfilevaluer   r   r   read_default   s    

r   c          	      sj  t | ddkrtddd l}|j|j|j|j|jd}i }t }tt	dd}d}x| dD ]}|
 }|d	krzqd|d
krqd|dt  td}||}	yv|	std|	d|	d }
d\}}t|dk rwd|
dkr| n d}||
   fdd|D }W qd tk
r:   tdY qdX qdW d|krf||d< |rf|||d< |S )Nr      zDtoo many arguments provided for X-Python3-Version: min and max only.r   )N=z>=z<=z<<T)version_onlyFall)ZcurrentZcurrent_extversionsz(>=|<=|<<|=)? *(\d\.\d)$z'error parsing Python3-Version attribute   .   )Nr   c                s   g | ]} |r|qS r   r   )r   av)filtopvr   r   r   G   s    z"parse_versions.<locals>.<listcomp>)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    



r7   Fc             C   sX   t s>y td}dd |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 | ]}|  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 |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 | ]}|  qS r   )r   )r   r8   r   r   r   r   d   s    z(unsupported_versions.<locals>.<listcomp>r   c             S   s   g | ]}|d d qS )r9   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 |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
d|}Y nX d }x0|D ](}|dr|dd
d
  d}qW |  dd |D }|aY nX ttt}t  t| | rdd tD S tS d S )Nzsupported-versionsc             S   s   g | ]}|  qS r   )r   )r   r8   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 d d|qS )z\s*(\S+)[ (]?.*z\1)r+   sub)r   r8   r   r   r   r      s    c             S   s   g | ]}|d d qS )r9   Nr   )r   r    r   r   r   r      s    )default_version_supported_versionsr   r"   r   
subprocessPopenPIPEr@   ImportErrorr   popenjoin
startswithr   r   popindex_default_versionsortappend)	r   r   cmdrF   pfdZdependslinedefaultr   r   r   r)   m   s8    




r)   c             C   sT   t stda td}t |t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 %sr9   )rO   r   readlinkr   r   rK   r   )r   Zdebian_defaultr   r   r   rD      s    
rD   c             C   s   t   d }t| }tdd}t|dkr|d |}g }x|D ]}|| q@W y||tdd  }W n   d}Y nX |	  |r|| nt
d|st
d|r|S d	d
 |D S d S )NT)r   r   r   r9   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>)rD   r7   r)   r!   intersectionrQ   rM   rN   rO   rP   r   )r3   r   r   r4   	supportedZvlversionrV   r   r   r   requested_versions   s,    

 
r[   c                sH   dd l }t   fdd| dD }|  | r@dd |D S |S d S )Nr   c                s(   g | ] }t j| krt j|qS r   )r   r   basename)r   r8   )rY   r   r   r      s   z&installed_versions.<locals>.<listcomp>z/usr/bin/python3.[0-9]c             S   s   g | ]}|d d qS )r9   Nr   )r   r    r   r   r   r      s    )globr)   rP   )r   r]   r   r   )rY   r   installed_versions   s    
r^   c               @   s   e Zd ZdS )ControlFileValueErrorN)__name__
__module____qualname__r   r   r   r   r_      s   r_   c               @   s   e Zd ZdS )MissingVersionValueErrorN)r`   ra   rb   r   r   r   r   rc      s   rc   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|drld}q8|d	| r|}q8| d
r8|dkrtd|ddd  }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 | ]}|  qS r   )r   )r   rU   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 sectionrB   r   znot a control filez$no X-Python3-Version in control file)r   rL   lowerr   r"   r   r_   rc   )fnpkgrZ   ZsversionZsectionZcontrolfilelinesrU   r   r   r   extract_pyversion_attribute   s2    

rj   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 | \}}tjtjd }|j	rt
|dkrytt|j W n< tk
r } ztd| | td W d d }~X Y nX n|jr6t
|dkr6tdt|j nt|jrdt
|dkrdtdt|j nF|jrt
|dkrt
|dkrd }n|d }ytj|r*|}yt|d!}	t|	|j}
W nb tk
r   tjd"||f  td Y n0 tk
r&   tjd#|  t|j}
Y nX nt||j}
td|
 W nB tk
r } z"tjd$||f  td W d d }~X Y nX ntjd%||f  td d S )&Nr   )OptionParserz;[-v] [-h] [-d|--default] [-s|--supported] [-i|--installed] )usagez-dz	--defaultz!print the default python3 version
store_truerV   )helpactiondestz-sz--supportedz$print the supported python3 versionsrY   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   )rn   rV   ro   rp   z%s:r   rA   zdebian/controlre   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optparserk   Z
add_option
parse_argsr   r   r\   sysargvrV   r!   printrD   r   r   exitrY   rK   r)   rr   r^   rq   isfilerj   r[   r_   stderrwriterc   )rk   rl   parserZoptsargsZprogrammsgr   rg   r3   Zvsr   r   r   main  sn    




 
r~   __main__)N)F)F)F)F)F)F)r   r+   rt   r   r:   r<   environr   r"   rE   rO   r   r7   r;   r=   r)   rD   r[   r^   r   r_   rc   rj   r~   r`   r   r   r   r   <module>   s,   
*


!


=A