o
    $¼"bÀ  ã                   @   sæ   d Z ddlmZmZ ddl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 ddlmZ dd	lmZ dd
lmZmZ e	ƒ Zeƒ eeejƒ< ddd„Zddd„Zdd„ Zdd„ ZG dd„ deƒZdd„ ZdZ dS )zê

We acquire the python information by running an interrogation script via subprocess trigger. This operation is not
cheap, especially not on Windows. To not have to pay this hefty cost every time we apply multiple levels of
caching.
é    )Úabsolute_importÚunicode_literalsN)ÚOrderedDict)ÚAppDataDisabled)Ú
PythonInfo)ÚPY2)ÚPath)Úensure_text)ÚPopenÚ
subprocessTFc                 C   sN   |d u rt jn|}t| ||||d�}t|tƒr%|r|‚t dt|ƒ¡ d }|S )N)Úignore_cachez%s)ÚosÚenvironÚ_get_from_cacheÚ
isinstanceÚ	ExceptionÚloggingÚinfoÚstr)ÚclsÚapp_dataÚexeÚenvÚraise_on_errorr   Úresult© r   úE/usr/lib/python3/dist-packages/virtualenv/discovery/cached_py_info.pyÚfrom_exe   s   
r   c                 C   sN   t |ƒ}|s|tv rt| }nt| ||||ƒ}| }t|< t|tƒr%||_|S ©N)r   Ú_CACHEÚ_get_via_file_cacher   r   Ú
executable)r   r   r   r   r   Úexe_pathr   Úpy_infor   r   r   r   &   s   

r   c                 C   sd  t t|ƒƒ}z| ¡ j}W n ty   d}Y nw |d u r tƒ }d | |¡}}| ¡ �| | ¡ ro| 	¡ }	|	d |	d |	d }
}}|
|krk||krk|  
dd„ | ¡ D ƒ¡}|j}|d urjtj |¡sj| ¡  d }n| ¡  |d u r˜t| |||ƒ\}}|d u rŽ||| ¡ dœ}	| |	¡ n|}W d   ƒ |S W d   ƒ |S W d   ƒ |S 1 s«w   Y  |S )NéÿÿÿÿÚpathÚst_mtimeÚcontentc                 S   s   i | ]\}}||“qS r   r   )Ú.0ÚkÚvr   r   r   Ú
<dictcomp>C   s    z'_get_via_file_cache.<locals>.<dictcomp>)r&   r%   r'   )r	   r   Ústatr&   ÚOSErrorr   r#   ÚlockedÚexistsÚreadÚ
_from_dictÚitemsÚsystem_executabler   r%   ÚremoveÚ_run_subprocessÚ_to_dictÚwrite)r   r   r%   r   r   Ú	path_textÚpath_modifiedr#   Úpy_info_storeÚdataÚof_pathÚof_st_mtimeÚ
of_contentÚsys_exeÚfailurer   r   r   r    5   sJ   ÿ
€
îù
ôý
ðír    c                 C   s>  t tj t¡ƒjd }| |¡�U}|t|ƒg}| ¡ }| 	dd ¡ t
 dt|ƒ¡ zt|dtjtjtj|d�}| ¡ \}}|j}	W n ty] }
 zd|
j|
j}}}	W Y d }
~
nd }
~
ww W d   ƒ n1 shw   Y  d\}}|	dkr�|  |¡}||_||fS d	 ||	|rŒd
 |¡nd|r”d |¡nd¡}t|ƒ}||fS )Nz
py_info.pyÚ__PYVENV_LAUNCHER__z get interpreter info via cmd: %sT)Úuniversal_newlinesÚstdinÚstderrÚstdoutr   Ú )NNr   z#failed to query {} with code {}{}{}z
 out: {!r}z
 err: {!r})r   r   r%   ÚabspathÚ__file__ÚparentÚensure_extractedr   ÚcopyÚpopr   ÚdebugÚLogCmdr
   r   ÚPIPEÚcommunicateÚ
returncoder-   ÚstrerrorÚerrnoÚ
_from_jsonr!   ÚformatÚRuntimeError)r   r   r   r   Úpy_info_scriptÚcmdÚprocessÚoutÚerrÚcodeÚos_errorr   r@   Úmsgr   r   r   r5   T   sH   ú
 €ÿ€ï
	ùür5   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	rN   Nc                 C   s   || _ || _d S r   )rX   r   )ÚselfrX   r   r   r   r   Ú__init__y   s   
zLogCmd.__init__c                    sV   dd„ ‰ ˆ dƒ  ‡ fdd„| jD ƒ¡}| jd ur"|ˆ dƒ | j¡7 }tr)| d¡S |S )Nc                 S   s   t | tƒr
|  d¡S | S ©Núutf-8)r   ÚbytesÚdecode)r*   r   r   r   Úe~   s   zLogCmd.__repr__.<locals>.eú c                 3   s   � | ]
}t  ˆ |ƒ¡V  qd S r   )ÚpipesÚquote)r(   Úc©re   r   r   Ú	<genexpr>�   s   € z"LogCmd.__repr__.<locals>.<genexpr>z env of {!r}rb   )ÚjoinrX   r   rU   r   Úencode)r_   Úcmd_reprr   rj   r   Ú__repr__}   s   

zLogCmd.__repr__c                 C   s   t | ƒ}tr| d¡S |S ra   )Úreprr   rd   )r_   Úrawr   r   r   Ú__unicode__ˆ   s   
zLogCmd.__unicode__r   )Ú__name__Ú
__module__Ú__qualname__r`   ro   rr   r   r   r   r   rN   x   s    
rN   c                 C   s   |   ¡  t ¡  d S r   )Úpy_info_clearr   Úclear)r   r   r   r   rw   �   s   rw   )r   rw   rN   )NTF)T)!Ú__doc__Ú
__future__r   r   r   r   rg   ÚsysÚcollectionsr   Úvirtualenv.app_datar   Úvirtualenv.discovery.py_infor   Úvirtualenv.infor   Úvirtualenv.util.pathr   Úvirtualenv.util.sixr	   Úvirtualenv.util.subprocessr
   r   r   r!   r   r   r    r5   ÚobjectrN   rw   Ú	___all___r   r   r   r   Ú<module>   s,    

$