U
    zk^-                     @   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| rzzt
d| }W n |k
rt   tY nX |S d S )Nr   )ConfigParserNoOptionErrorz"/usr/share/python3/debian_defaultsZDEFAULT)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              	      sh  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
krqb|dt  td}||}	zx|	std|	d|	d }
d\}}t|dk rW qb|
dkr| n d}||
   fdd|D }W qb tk
r:   tdY qbX qbd|krd||d< |rd|||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   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   parse_versions'   sP     


r;   Fc                 C   sX   t s>z 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    
rC   c                 C   sX   t s>z 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<   r   r   r   r   d   s     z(unsupported_versions.<locals>.<listcomp>r
   c                 S   s   g | ]}|d d qS r>   r   r@   r   r   r   r   h   s     )_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k
r   ddddg}z&d	d l}|j|d
d|jd}|j}W n$ t	k
r   t
d|}Y nX d }|D ](}|dr|dd
d
  d}q|  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<   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/   subr<   r   r   r   r      s     c                 S   s   g | ]}|d d qS r>   r   r@   r   r   r   r      s     )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   s@      



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 %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X |	  |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      s     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| k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      s     )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 5 Q R X |D ]|}|dkr\|dkr\|dkrV qd}q6|drld}q6|d	| r|}q6| d
r6|dkrtd|ddd  }q6|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   r]   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 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k
r } ztd| | td W 5 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 }ztj|r*|}z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 5 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_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