U
    |e4                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ ddlmZ ddl	m
Z
 ddlmZ G dd	 d	eZdd
dZdddZdd ZG dd dZdd Zdd ZG dd de
Zdd	dgZdS )    )annotationsN)IS_WIN   )Discover)
PythonInfo)
PythonSpecc                      sD   e Zd Zdd fddZedd Zdd Zd	dd
dZ  ZS )BuiltinNonereturnc                   s6   t  | |jr|jntjg| _|j| _|j| _d S N)super__init__pythonsys
executablepython_specapp_datatry_first_with)selfoptions	__class__ @/tmp/pip-unpacked-wheel-tuxcr54r/virtualenv/discovery/builtin.pyr      s    zBuiltin.__init__c              
   C  s6   |j ddddtdg dd |j dd	d
tdg dd d S )Nz-pz--pythonr   pyappendzinterpreter based on what to create environment (path/identifier) - by default use the interpreter where the tool is installed - first found wins)destmetavartypeactiondefaulthelpz--try-first-withr   py_exez:try first these interpreters before starting the discovery)add_argumentstr)clsparserr   r   r   add_parser_arguments   s&    zBuiltin.add_parser_argumentsc                 C  s4   | j D ](}t|| j| j| j}|d k	r|  S qd S r   )r   get_interpreterr   r   Z_env)r   r   resultr   r   r   run,   s
    

zBuiltin.runr%   c                 C  s0   t | jdkr| jd n| j}| jj d|S )Nr   r   z discover of python_spec=)lenr   r   __name__)r   specr   r   r   __repr__3   s    zBuiltin.__repr__)	r-   
__module____qualname__r   classmethodr(   r+   r/   __classcell__r   r   r   r   r      s
   
r   c                 C  s   t | }td| t }|d kr*tjn|}t||||D ]R\}}|j|f} | |krXq<td| |	||rt
d| |  S ||  q<d S )Nzfind interpreter for spec %rzproposed %szaccepted %s)r   Zfrom_string_speclogginginfosetosenvironpropose_interpretersZsystem_executableZ	satisfiesdebugadd)keyr   r   envr.   Zproposed_pathsinterpreterZimpl_must_matchr   r   r   r)   8   s    

r)   c              	   c  s  |d krt jn|}|D ]R}t j|}zt | W n tk
rH   Y qX tjt j|||ddfV  q| jd k	rzt | j W n tk
r   | jr Y n"X tjt j| j||ddfV  | jrd S n>t	|dfV  t
rddlm} || ||D ]}|dfV  qt|}t }	t|D ]\}
}t|}tt|
|| t| D ]d\}}t||}|d k	rLt j|}||	krL|	| tj||d|d}|d k	rL||fV  qLq"d S )N)r=   Tr   )r9   F)Zraise_on_errorr=   )r7   r8   pathabspathlstatOSErrorr   Zfrom_exeZis_absZcurrent_systemr   Zwindowsr9   	get_pathsr6   	enumerater%   r4   r:   LazyPathDumppossible_specs
check_pathr;   PathPythonInfo)r.   r   r   r=   r#   r?   r9   r>   pathsZtested_exesposZpath_str	candidatematchfoundZexer   r   r   r9   I   sH     
 




r9   c              	   C  s`   |  dd }|d krBztd}W n ttfk
r@   tj}Y nX |sJg S dd |tjD S )NPATHCS_PATHc                 S  s   g | ]}t j|r|qS r   )r7   r?   exists).0pr   r   r   
<listcomp>   s      zget_paths.<locals>.<listcomp>)getr7   confstrAttributeError
ValueErrordefpathsplitpathsep)r=   r?   r   r   r   rC   {   s    rC   c                   @  s(   e Zd ZddddZddddZdS )	rE   r	   r
   c                 C  s   || _ || _|| _d S r   )rJ   r?   r=   )r   rJ   r?   r=   r   r   r   r      s    zLazyPathDump.__init__r%   c              	   C  s   d| j  d| j }| jdr|d7 }t| jD ]\}z2tj| j|}tj|sdt|tj	shW q4W n t
k
r~   Y nX |d7 }||7 }q4|S )Nzdiscover PATH[z]=Z_VIRTUALENV_DEBUGz with => )rJ   r?   r=   rT   r7   listdirjoinisdiraccessX_OKrB   )r   content	file_name	file_pathr   r   r   r/      s    
zLazyPathDump.__repr__N)r-   r0   r1   r   r/   r   r   r   r   rE      s   rE   c                 C  s\   t j| \}}tjdkr*|dkr*| d } t j| r:| S t j|| } t j| rX| S d S )Nwin32z.exe)r7   r?   splitextr   platformisfiler]   )rK   r?   _extr   r   r   rG      s    rG   c                 c  s   | j dfV  |  E d H  d S )NF)Zstr_specZgenerate_names)r.   r   r   r   rF      s    rF   c                   @  s   e Zd ZdZdS )rH   zpython info from path.N)r-   r0   r1   __doc__r   r   r   r   rH      s   rH   )NN)N)
__future__r   r4   r7   r   Zvirtualenv.infor   Zdiscoverr   Zpy_infor   Zpy_specr   r   r)   r9   rC   rE   rG   rF   rH   __all__r   r   r   r   <module>   s&   *

2
