o
    d=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 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).0Zver r   !/usr/share/python3/py3versions.py
<listcomp>
   s    r   Z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_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                 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)ZcurrentZ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Z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)vstringr-   Z	operatorsvinfoZexact_versionsZversion_rangeZ
relop_seenZfieldZvemopZvmajZvminr   r*   r   parse_versions+   sR   

r@   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   r+   r   r   r   r   _       )_old_versionsr   r   r   r$   r   r   r   r   old_versionsV      rM   c                 C   rA   )Nzunsupported-versionsc                 S   rB   r   r   rC   r   r   r   r   i   rE   z(unsupported_versions.<locals>.<listcomp>r
   c                 S   rF   rG   r   rI   r   r   r   r   m   rJ   )_unsupported_versionsr   r   r   rL   r   r   r   unsupported_versionsd   rN   rP   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   rB   r   r   rC   r   r   r   r   x   rE   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)r5   subrC   r   r   r   r      s    c                 S   rF   rG   r   rI   r   r   r   r      rJ   )default_version_supported_versionsr   r   r   
subprocessPopenPIPErS   ImportErrorr   popenjoin
startswithr   r   popindex_default_versionsortappend)	r$   r   cmdrY   pfdZdependslinedefaultr   r   r   r3   r   s>   


r3   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 %srH   )rb   r   readlinkr   r   r^   r   )r$   Zdebian_defaultr   r   r   rW      s   
rW   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#   r'   r&   rH   r	   z/No supported python3 versions in version stringzempty set of versionsc                 S   s   g | ]}d | qS r   r   rI   r   r   r   r      rE   z&requested_versions.<locals>.<listcomp>)rW   r@   r3   r,   intersectionrd   r`   ra   rb   rc   r   )r<   r$   r&   r=   	supportedZvlr   ri   r   r   r   requested_versions   s,   

rm   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   basenamerC   rl   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   rF   rG   r   rI   r   r   r   r      rJ   )globr3   rc   )r$   rp   r&   r   ro   r   installed_versions   s   
rq   c                   @      e Zd ZdS )ControlFileValueErrorN__name__
__module____qualname__r   r   r   r   rs          rs   c                   @   rr   )MissingVersionValueErrorNrt   r   r   r   r   ry      rx   ry   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   rB   r   r   )r   rh   r   r   r   r      rE   z/extract_pyversion_attribute.<locals>.<listcomp>r	   SourcezSource:z	Package: zx-python3-version:z1attribute X-Python3-Version not in Source sectionrU   r'   znot a control filez$no X-Python3-Version in control file)r   r_   lowerr   r   r   rs   ry   )fnZpkgr   ZsversionsectionZcontrolfilelinesrh   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_trueri   )helpactiondestz-sz--supportedz$print the supported python3 versionsrl   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   ri   r   r   z%s:r'   rT   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
)Zoptparser   Z
add_option
parse_argsr   r   rn   sysargvri   r,   printrW   r$   r   exitrl   r^   r3   r   rq   r   isfiler   rm   rs   stderrwritery   )r   r   parserZoptsargsZprogrammsgr&   r}   r<   Zvsr   r   r   main  s   

r   __main__r   )F)r   r5   r   r   rK   rO   environr   r   rX   rb   r   r    r@   rM   rP   r3   rW   rm   rq   r   rs   ry   r   r   ru   r   r   r   r   <module>   s6   

+


!

A
