o
    年f.                     @   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 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rpe  dS dS )%    Nc                 C   s    g | ]}|  rd |   qS zpython%sstrip).0ver r   !/usr/share/python3/py3versions.py
<listcomp>
   s    r	   DEBPYTHON3_SUPPORTED ,c                 C   sv   ddl m}m} ts"tjdr"| }td}|| |	  |atr9| r9z	t
d| }W |S  |y8   tw d S )Nr   )ConfigParserNoOptionErrorz"/usr/share/python3/debian_defaultsDEFAULT)configparserr   r   	_defaultsospathexistsopen	read_filecloseget
ValueError)namer   r   configdefaultsfilevaluer   r   r   read_default   s    
r   c                 C   s   t dd | dD S )Nc                 s   s    | ]}t |V  qd S N)int)r   partr   r   r   	<genexpr>(   s    z#version_to_tuple.<locals>.<genexpr>.)tuplesplit)versionr   r   r   version_to_tuple'   s   r'   c              	      sZ  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 ]h}|
 }|d	kr<q1|d
v rAq1|dt  td}||}	z<|	sYtd|	d|	d}
d\}}t|dk rsW q1|
dv r}| nd}||
   fdd|D }W q1 ty   tdw d|v r||d< |r|||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#      )Nr)   c                    s"   g | ]} t |t r|qS r   )r'   )r   avfiltopvr   r   r	   K   s   
 z"parse_versions.<locals>.<listcomp>)lenr%   r   operatoreqgeleltsetsupported_versionsr   
setdefaultrecompilematchgroupr    add	Exceptionunion)vstringr7   	operatorsvinfoexact_versionsversion_range
relop_seenfieldvemopvmajvminr   r3   r   parse_versions+   sR   

rR   Fc                 C   R   t sztd}dd |dD a W n ty   g a Y nw | r'dd t D S t S )Nzold-versionsc                 S      g | ]}|  qS r   r   r   sr   r   r   r	   [       z old_versions.<locals>.<listcomp>r   c                 S      g | ]}|d d qS    Nr   r   r5   r   r   r   r	   _       )_old_versionsr   r%   r   r+   r   r   r   r   old_versionsV      r_   c                 C   rS   )Nzunsupported-versionsc                 S   rT   r   r   rU   r   r   r   r	   i   rW   z(unsupported_versions.<locals>.<listcomp>r   c                 S   rX   rY   r   r[   r   r   r   r	   m   r\   )_unsupported_versionsr   r%   r   r^   r   r   r   unsupported_versionsd   r`   rb   c           	      C   s  t   tsnztd}dd |dD aW nX tym   g d}zdd l}|j|dd|jd	}|j}W n t	yD   t
d
|}Y nw d }|D ]}|dr]|ddd  d}qI|  dd |D }|aY nw ttt}t  t| | rdd tD S tS )Nzsupported-versionsc                 S   rT   r   r   rU   r   r   r   r	   x   rW   z&supported_versions.<locals>.<listcomp>r   )z/usr/bin/apt-cachez--no-all-versionsshowzpython3-allr   r0   F)bufsizeshellstdout zDepends::c                 S   s   g | ]	}t d d|qS )z\s*(\S+)[ (]?.*z\1)r?   subrU   r   r   r   r	      s    c                 S   rX   rY   r   r[   r   r   r   r	      r\   )default_version_supported_versionsr   r%   r   
subprocessPopenPIPErf   ImportErrorr   popenjoin
startswithr   r   popindex_default_versionsortappend)	r+   r   cmdrl   pfddependslinedefaultr   r   r   r=   r   s>   


r=   c                 C   sP   t stda td}t |tjd|fvrtd| |a | r&t dd  S t 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 %srZ   )ru   r   readlinkr   r   rq   r   )r+   debian_defaultr   r   r   rj      s   
rj   c                 C   s   t   d }t| }tdd}t|dkrH|d |}g }|D ]}|| qz||tdd  }W n   d}Y |	  |rG|| nt
d|sRt
d|rV|S d	d
 |D S )NTr*   r0   r/   rZ   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	      rW   z&requested_versions.<locals>.<listcomp>)rj   rR   r=   r6   intersectionrw   rs   rt   ru   rv   r   )rF   r+   r/   rH   	supportedvlr&   r}   r   r   r   requested_versions   s,   

r   c                    sN   dd l }t   fdd| d| d D }|  | r%dd |D S |S )Nr   c                    s(   g | ]}t j| v rt j|qS r   )r   r   basenamerU   r   r   r   r	      s
    
z&installed_versions.<locals>.<listcomp>z/usr/bin/python3.[0-9]z/usr/bin/python3.[0-9][0-9]c                 S   rX   rY   r   r[   r   r   r   r	      r\   )globr=   rv   )r+   r   r/   r   r   r   installed_versions   s   
r   c                   @      e Zd ZdS )ControlFileValueErrorN__name__
__module____qualname__r   r   r   r   r          r   c                   @   r   )MissingVersionValueErrorNr   r   r   r   r   r      r   r   c                 C   s   d}d}d}t | dd}dd |D }W d   n1 sw   Y  |D ]>}|dkr8|dkr8|dkr5 n/d}q%|dr@d}q%|d	| rJ|}q%| d
rc|dkrYtd|ddd  }q%|du rltd|dkrz|du rxtd|S |S )zFread the debian/control file, extract the X-Python3-Version
    field.Nzutf-8)encodingc                 S   rT   r   r   )r   r|   r   r   r   r	      rW   z/extract_pyversion_attribute.<locals>.<listcomp>r   SourcezSource:z	Package: zx-python3-version:z1attribute X-Python3-Version not in Source sectionrh   r0   znot a control filez$no X-Python3-Version in control file)r   rr   lowerr   r%   r   r   r   )fnpkgr&   sversionsectioncontrolfilelinesr|   r   r   r   extract_pyversion_attribute   s6   
r   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	rt
|dkrz
tt|j W d S  ty } ztd| | td W Y d }~d S d }~ww |jrt
|dkrtdt|j d S |jrt
|dkrtdt|j d S |jr>t
|dkr>t
|dkrd }n|d }zUtj|r|}zt|d!}	t|	|j}
W n4 ty   tjd"||f  td Y n ty
   tjd#|  t|j}
Y nw t||j}
td|
 W d S  ty= } ztjd$||f  td W Y d }~d S d }~ww 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 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+   )r   r}   r   r   z%s:r0   rg   zdebian/controlr   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
)optparser   
add_option
parse_argsr   r   r   sysargvr}   r6   printrj   r+   r   exitr   rq   r=   r   r   r   isfiler   r   r   stderrwriter   )r   r   parseroptsargsprogrammsgr/   r   rF   vsr   r   r   main  s   

r   __main__r   )F)r   r?   r   r   r]   ra   environr   r%   rk   ru   r   r'   rR   r_   rb   r=   rj   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s6   

+


!

A
