a
    j`-                     @   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rx| rxzt
d| }W n |yr   tY n0 |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              	      sf  t | ddkrtddd l}|j|j|j|j|jd}i }t }tt	dd}d}| dD ]}|
 }|d	krxqb|d
v rqb|dt  td}||}	zx|	std|	d|	d }
d\}}t|dk rW qb|
dv r| n d}||
   fdd|D }W qb ty8   tdY qb0 qbd|v rb||d< |rb|||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Zfiltopvr   r   r   G       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   parse_versions'   sN    


r;   Fc                 C   sV   t s<z td}dd |dD a W n ty:   g a Y n0 | rNdd t D S t S d S )Nzold-versionsc                 S   s   g | ]}|  qS r   r   r   sr   r   r   r   V   r$   z old_versions.<locals>.<listcomp>r
   c                 S   s   g | ]}|d d qS    Nr   r   r#   r   r   r   r   Z   r$   )_old_versionsr   r&   r   r   r   r   r   r   old_versionsQ   s    
rC   c                 C   sV   t s<z td}dd |dD a W n ty:   g a Y n0 | rNdd t D S t S d S )Nzunsupported-versionsc                 S   s   g | ]}|  qS r   r   r<   r   r   r   r   d   r$   z(unsupported_versions.<locals>.<listcomp>r
   c                 S   s   g | ]}|d d qS r>   r   r@   r   r   r   r   h   r$   )_unsupported_versionsr   r&   r   rB   r   r   r   unsupported_versions_   s    
rE   c           	      C   s  t   tsz td}dd |dD aW n ty   g d}z&dd l}|j|dd|jd	}|j}W n" t	y   t
d
|}Y n0 d }|D ](}|dr|ddd  d}q|  dd |D }|aY n0 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<   r   r   r   r   s   r$   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/   subr<   r   r   r   r      r$   c                 S   s   g | ]}|d d qS r>   r   r@   r   r   r   r      r$   )default_version_supported_versionsr   r&   r   
subprocessPopenPIPErH   ImportErrorr   popenjoin
startswithr   r   popindex_default_versionsortappend)	r   r   cmdrN   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v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?   )rW   r   readlinkr   r   rS   r   )r   Zdebian_defaultr   r   r   rL      s    
rL   c                 C   s   t   d }t| }tdd}t|dkr|d |}g }|D ]}|| q>z||tdd  }W n   d}Y n0 |	  |r|| nt
d|st
d|r|S d	d
 |D S d S )NTr   r   r   r?   r	   z/No supported python3 versions in version stringzempty set of versionsc                 S   s   g | ]}d | qS r   r   r@   r   r   r   r      r$   z&requested_versions.<locals>.<listcomp>)rL   r;   r-   r%   intersectionrY   rU   rV   rW   rX   r   )r7   r   r   r8   	supportedZvlversionr^   r   r   r   requested_versions   s*    

rc   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| v rt j|qS r   )r   r   basenamer<   ra   r   r   r      s   z&installed_versions.<locals>.<listcomp>z/usr/bin/python3.[0-9]c                 S   s   g | ]}|d d qS r>   r   r@   r   r   r   r      r$   )globr-   rX   )r   rf   r   r   re   r   installed_versions   s    
rg   c                   @   s   e Zd ZdS )ControlFileValueErrorN__name__
__module____qualname__r   r   r   r   rh      s   rh   c                   @   s   e Zd ZdS )MissingVersionValueErrorNri   r   r   r   r   rm      s   rm   c                 C   s   d}d}d}t | dd}dd |D }W d   n1 s<0    Y  |D ]|}|dkrp|dkrp|dkrj qd}qJ|drd}qJ|d	| r|}qJ| d
rJ|dkrtd|ddd  }qJ|du rtd|dkr|du 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   r]   r   r   r   r      r$   z/extract_pyversion_attribute.<locals>.<listcomp>r	   SourcezSource:z	Package: zx-python3-version:z1attribute X-Python3-Version not in Source sectionrJ   r   znot a control filez$no X-Python3-Version in control file)r   rT   lowerr   r&   r   rh   rm   )fnpkgrb   ZsversionZsectionZcontrolfilelinesr]   r   r   r   extract_pyversion_attribute   s2    ,
rt   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
ztt|j W n> ty } z$td| | td W Y d }~n
d }~0 0 n|jr8t
|dkr8tdt|j nr|jrft
|dkrftdt|j nD|jrt
|dkrt
|dkrd }n|d }ztj|r(|}zt|d!}	t|	|j}
W n^ ty   tjd"||f  td Y n. ty$   tjd#|  t|j}
Y n0 nt||j}
td|
 W nD ty } z*tjd$||f  td W Y d }~n
d }~0 0 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_truer^   )helpactiondestz-sz--supportedz$print the supported python3 versionsra   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   )rx   r^   ry   rz   z%s:r   rI   zdebian/controlro   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optparseru   Z
add_option
parse_argsr   r   rd   sysargvr^   r%   printrL   r   r   exitra   rS   r-   r|   rg   r{   isfilert   rc   rh   stderrwriterm   )ru   rv   parserZoptsargsZprogrammsgr   rq   r7   Zvsr   r   r   main  s|    
$
"r   __main__)N)F)F)F)F)F)F)r   r/   r~   r   rA   rD   environr   r&   rM   rW   r   r;   rC   rE   r-   rL   rc   rg   r   rh   rm   rt   r   rj   r   r   r   r   <module>   s.   
*


!


=A