3
W>p_U                 @   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Zddl	Z	ddl
Z
ddlmZmZ ddlmZ ddlmZ ddlmZ edd	d
dddgZdd Ze ZejdZG dd deZedkree j  dS )z
The PythonInfo contains information about a concrete instance of a Python interpreter

Note: this file is also used to query target interpreters, so can only use standard library methods
    )absolute_importprint_functionN)OrderedDict
namedtuple)dist)SCHEME_KEYS)digitsVersionInfomajorminormicroreleaselevelserialc               C   s*   t tjdgtjjddj jtj S )N PATHEXT)	listr   fromkeysosenvirongetlowersplitpathsep r   r   /home/aldo/Documentos/tesis/tesis/device auto/pruebas pulsar/pruebaclonado/envClonado/lib/python3.6/site-packages/virtualenv/discovery/py_info.py_get_path_extensions   s    r   z\{\w+\}c               @   s~  e Zd ZdZdd Zdd Zedd Zedd	 Z	ed
d Z
edd Zedd Zedd ZdejfddZdGddZedd Zedd Zedd Zdd Zd d! Zd"d# Zed$d% Zed&d' Zd(d) ZdZdZedHd*d+ZedId,d-Zd.d/ Z d0d1 Z!edJd3d4Z"ed5d6 Z#ed7d8 Z$ed9d: Z%i Z&dKd;d<Z'd=d> Z(ed?d@ Z)dAdB Z*dCdD Z+dEdF Z,dS )L
PythonInfoz-Contains information for a Python interpreterc                s  dd dd }t j _tj  _ jdkrNtfddt jD  _ttfddt jD   _t j	d(krzdnd
 _
t j _tj _|tt dd  _|tt dd  _|tt dd  _|tt dd  _|tt dd  _|t j _| j _ j  _ytd d}W n tk
rZ   d}Y nX | _fddt jD  _t j  _tt jdd  _fddt j! D  _"tt dtt dd }fddd| fgD  _ t# }x@ j"j$ D ]2}x*t%j&|D ]}|j'|dd)  qW qW |j'd fdd|D  _( jdkrlt jj)d	krld  j(d!< fd"d j* j+ D  _, fd#d j(j+ D } j-d$| _. j-d%| _/tt d&tt d'd  _0d  _1d S )*Nc             S   s   t | tr| jdS | S )Nzutf-8)
isinstancebytesdecode)vr   r   r   u#   s    zPythonInfo.__init__.<locals>.uc             S   s   | d krd S t jj| S )N)r   pathabspath)r    r   r   r   abs_path&   s    z%PythonInfo.__init__.<locals>.abs_pathPyPyc             3   s   | ]} |V  qd S )Nr   ).0i)r!   r   r   	<genexpr>-   s    z&PythonInfo.__init__.<locals>.<genexpr>c             3   s   | ]} |V  qd S )Nr   )r&   r'   )r!   r   r   r(   0   s           @   prefixbase_prefixreal_prefixbase_exec_prefixexec_prefixZvenvTFc                s   g | ]} |qS r   r   )r&   r'   )r!   r   r   
<listcomp>I   s    z'PythonInfo.__init__.<locals>.<listcomp>encodingc                s$   i | ]} t j|d d |qS )F)expand)	sysconfigget_path)r&   r'   )r!   r   r   
<dictcomp>M   s    z'PythonInfo.__init__.<locals>.<dictcomp>get_makefile_filenameZ_get_makefile_filenamec                s&   i | ]\}}|d k	r | |qS )Nr   )r&   kr    )r!   r   r   r6   P   s   Zmakefile_filename   PYTHONFRAMEWORKc                s$   i | ]} t j|pd  |qS )r   )r4   get_config_var)r&   r'   )r!   r   r   r6   _   s    pythonZimplementation_lowerc                s   i | ]\}} | |qS r   r   )r&   r8   r    )r!   r   r   r6   c   s    c                s(   i | ] \}}|j  jr jn||qS r   )
startswithr,   system_prefix)r&   r8   r    )selfr   r   r6   d   s    stdlib
platstdlibmaxsizeZmaxintl        )2sysplatformpython_implementationimplementationtuplepypy_version_infor	   r   version_inforB   architectureversionr   namegetattrr,   r-   r.   r/   r0   
executableoriginal_executable_fast_get_system_executablesystem_executable
__import__ImportErrorZhas_venvr"   getfilesystemencodingfile_system_encodingstdoutstdout_encodingr4   get_path_namessysconfig_pathssetvalues_CONF_VAR_REfindalladdsysconfig_varsr
   _distutils_installitemsdistutils_installsysconfig_pathZsystem_stdlibZsystem_stdlib_platformmax_size	_creators)r?   r$   ZhasmakefileZconfig_var_keyselementr8   Zconfsr   )r?   r!   r   __init__"   sZ    



"

zPythonInfo.__init__c             C   sR   | j s| jdk	rL| j| jkrL| j dkrHttdd}|dk	rHtj|krH|S dS | jS )z>Try to get the system executable by just looking at propertiesNZ_base_executable)r.   r-   r,   rN   rD   rO   rP   )r?   Zbase_executabler   r   r   rQ   j   s    

z&PythonInfo._fast_get_system_executablec                 sR   t jddi} ttdrd t_| jddd tj _ j	   fddt
D }|S )	Nscript_argsz--no-user-cfg
_frameworkinstallT)createc                s.   i | ]&}t  d j|dd jtj|qS )z
install_{}r9   N)rN   formatlstripr   sep)r&   key)r'   r   r   r6      s    z1PythonInfo._distutils_install.<locals>.<dictcomp>)r   DistributionhasattrrD   rk   get_command_objr   rp   r,   finalize_optionsr   )dresultr   )r'   r   ra   y   s    
zPythonInfo._distutils_installc             C   s   dj dd | jdd D S )N.c             s   s   | ]}t |V  qd S )N)str)r&   r'   r   r   r   r(      s    z)PythonInfo.version_str.<locals>.<genexpr>r      )joinrJ   )r?   r   r   r   version_str   s    zPythonInfo.version_strc             C   s   dj dd | jdd D S )Nrx   c             s   s   | ]}t |V  qd S )N)ry   )r&   r'   r   r   r   r(      s    z1PythonInfo.version_release_str.<locals>.<genexpr>r   r)   )r{   rJ   )r?   r   r   r   version_release_str   s    zPythonInfo.version_release_strc             C   s   | j }dj|j|jS )Nzpython{}.{})rJ   rn   r
   r   )r?   rJ   r   r   r   python_name   s    zPythonInfo.python_namec             C   s
   | j d k	S )N)r.   )r?   r   r   r   is_old_virtualenv   s    zPythonInfo.is_old_virtualenvc             C   s   | j d k	o| jjdkS )Nrz   )r-   rJ   r
   )r?   r   r   r   is_venv   s    zPythonInfo.is_venvNc             C   sP   | j | }|d kr| j}n"dd | jj D }|j| |}|jf |jd|S )Nc             S   s   i | ]\}}||qS r   r   )r&   r8   r    r   r   r   r6      s    z-PythonInfo.sysconfig_path.<locals>.<dictcomp>/)rZ   r`   rb   updatern   replace)r?   rq   Z
config_varrp   patternbaser   r   r   rd      s    

zPythonInfo.sysconfig_pathFc             C   s0   | j d ks|dkr*ddlm} |j| | _ | j S )NTr   )CreatorSelector)rf   Zvirtualenv.run.plugin.creatorsr   Zfor_interpreter)r?   Zrefreshr   r   r   r   creators   s    zPythonInfo.creatorsc                s^    j d fdd jj D }tjj|sZtjj jtjj j	d }tjj|rZ|}|S )Nincludec                s(   i | ] \}}|j  jr jn||qS r   )r=   r,   r>   )r&   r8   r    )r?   r   r   r6      s    z-PythonInfo.system_include.<locals>.<dictcomp>headers)
rd   r`   rb   r   r"   existsr{   r,   dirnamerc   )r?   r"   fallbackr   )r?   r   system_include   s    zPythonInfo.system_includec             C   s   | j p| jp| jS )N)r.   r-   r,   )r?   r   r   r   r>      s    zPythonInfo.system_prefixc             C   s   | j p| jp| jS )N)r.   r/   r0   )r?   r   r   r   system_exec_prefix   s    zPythonInfo.system_exec_prefixc             C   s    t | }tjdkr|jd}|S )Nr)   zutf-8)reprrD   rJ   r   )r?   contentr   r   r   __unicode__   s    

zPythonInfo.__unicode__c             C   s    dj | jjdd | jj D S )Nz{}({!r})c             S   s    i | ]\}}|j d s||qS )_)r=   )r&   r8   r    r   r   r   r6      s    z'PythonInfo.__repr__.<locals>.<dictcomp>)rn   	__class____name____dict__rb   )r?   r   r   r   __repr__   s    zPythonInfo.__repr__c             C   s   dj | jjdjdd d| jf| jd k	r6| j| jkr6dnd | jf| j| jkrZ| j| jkrZdnd | jfd| jfd	| jfd
t	| j
fddj | j| jffD }|S )Nz{}({})z, c             s   s&   | ]\}}|d k	rdj ||V  qd S )Nz{}={})rn   )r&   r8   r    r   r   r   r(      s   z%PythonInfo.__str__.<locals>.<genexpr>specsystemoriginalexerE   rL   Zencoding_fs_ioz{}-{})rn   r   r   r{   r   rR   rO   rP   rE   r   rL   rV   rX   )r?   r   r   r   r   __str__   s"    zPythonInfo.__str__c             C   s$   dj | jdjdd | jD | jS )Nz{}{}-{}rx   c             s   s   | ]}t |V  qd S )N)ry   )r&   r'   r   r   r   r(      s    z"PythonInfo.spec.<locals>.<genexpr>)rn   rG   r{   rJ   rK   )r?   r   r   r   r      s    zPythonInfo.specc             C   s"   ddl m} || | jj  d S )Nr   )clear)#virtualenv.discovery.cached_py_infor   _cache_exe_discovery)clsapp_datar   r   r   r   clear_cache   s    zPythonInfo.clear_cachec             C   s  |j rz| jtj j|j krdS |jsztj j| j}|j }tjdkrntj j	|\}}|j
|rn|dt|  }||krzdS |r|jdk	r|jj | jj krdS |jdk	r|j| jkrdS xHt| jdd |j|j|jfD ]$\}}|dk	o|dk	o||krdS qW dS )zWcheck if a given specification can be satisfied by the this python interpreter instanceTwin32NFr   rz   )r"   rO   r   r#   Zis_absbasenamerP   rD   rE   splitextendswithlenrG   r   rK   ziprJ   r
   r   r   )r?   r   Zimpl_must_matchr   Z	spec_pathsuffixZourreqr   r   r   	satisfies   s*    

*zPythonInfo.satisfiesc             C   s&   | j dkr | jtj|ddd| _ | j S )z
        This locates the current host interpreter information. This might be different than what we run into in case
        the host python has been upgraded from underneath us.
        NTF)raise_on_errorresolve_to_host)_currentfrom_exerD   rO   )r   r   r   r   r   current  s    
zPythonInfo.currentc             C   s&   | j dkr | jtj|ddd| _ | j S )z
        This locates the current host interpreter information. This might be different than what we run into in case
        the host python has been upgraded from underneath us.
        NT)r   r   )_current_systemr   rD   rO   )r   r   r   r   r   current_system!  s    
zPythonInfo.current_systemc             C   s   t j| j ddS )Nr)   )indent)jsondumps_to_dict)r?   r   r   r   _to_json+  s    zPythonInfo._to_jsonc                s*    fddt  D }|d j |d< |S )Nc                s$   i | ]}|dkrt  |nd|qS )rf   N)rf   )rN   )r&   var)r?   r   r   r6   0  s    z'PythonInfo._to_dict.<locals>.<dictcomp>rJ   )vars_asdict)r?   datar   )r?   r   r   /  s    zPythonInfo._to_dictTc       	      C   s   ddl m} || ||||d}t|tr||r|y|j||}W n> tk
rz } z"|rV|tjd|j| d}W Y dd}~X nX |S )z8Given a path to an executable get the python informationr   )r   )r   ignore_cachez-ignore %s due cannot resolve system due to %rN)	r   r   r   r   _resolve_to_system	ExceptionlogginginforP   )	r   r   r   r   r   r   r   Zproposed	exceptionr   r   r   r   5  s    zPythonInfo.from_exec             C   s"   t j|}| jdd |j D S )Nc             S   s   i | ]\}}||qS r   r   )r&   r8   r    r   r   r   r6   K  s    z)PythonInfo._from_json.<locals>.<dictcomp>)r   loads
_from_dictrb   )r   payloadrawr   r   r   
_from_jsonG  s    
zPythonInfo._from_jsonc             C   s0   t f |d |d< |  }dd |j D |_|S )NrJ   c             S   s   i | ]\}}||qS r   r   )r&   r8   r    r   r   r   r6   Q  s    z)PythonInfo._from_dict.<locals>.<dictcomp>)r	   rb   r   )r   r   rw   r   r   r   r   M  s    zPythonInfo._from_dictc       	      C   s   |j }t }x|jd kr|jp(|jp(|j}||krt|dkrTtjd| |j |_P x0t	|j
 ddD ]\}\}}tjd||| qfW tjdt|d || tdjdj|j |||< |j||dd}qW |j |jkr| j|j|}||_ |S )	Nr9   z$%r links back to itself via prefixes)startz%d: prefix=%s, info=%rz prefixes are causing a circle {}|F)r,   exact)rO   r   rR   r.   r-   r,   r   r   r   	enumeraterb   errorRuntimeErrorrn   r{   keysdiscover_exer   )	r   r   targetZstart_executableprefixesr,   atptr   r   r   r   T  s&    zPythonInfo._resolve_to_systemc             C   s  ||f}|| j kr6|r6tjd||| j |  | j | S tjd| | | j }| j|}g }xB|D ]:}x4|D ],}	| j|||	||}
|
d k	rj|
| j |< |
S qjW q`W |dkr|r| j|| }
tjj	|}|
| j |< tjd|
| |
S dj
dj	|tjj	|}t|d S )Nz)discover exe from cache %s - exact %s: %rzdiscover exe for %s in %sFzFno exact match found, chosen most similar of %s within base folders %szfailed to detect {} in {}r   )r   r   debug_find_possible_exe_names_find_possible_folders
_check_exe_select_most_likelyr   r   r{   rn   r   )r?   r   r,   r   rq   Zpossible_namesZpossible_folders
discoveredfolderrM   r   foldersmsgr   r   r   r   m  s,    





zPythonInfo.discover_exec             C   s   t jj||}t jj|sd S | j||ddd}|d kr<d S xd
D ]~}t||}	t| |}
|	|
krB|dkrdjdd |	D djdd |
D  }	}
|j}tjd	|||	|
 |dkr|j	| P qBW |S d S )NF)r   r   rG   rK   rJ   rx   c             s   s   | ]}t |V  qd S )N)ry   )r&   r'   r   r   r   r(     s    z(PythonInfo._check_exe.<locals>.<genexpr>z2refused interpreter %s because %s differs %s != %s)rG   rK   rJ   )
r   r"   r{   r   r   rN   rO   r   r   append)r?   r   r   rM   r   r   Zexe_pathr   itemfoundZsearchedrO   r   r   r   r     s&    


*
zPythonInfo._check_exec                s&    fdd}t | |dd}|d }|S )Nc                s|   | j  j k| jj jjk| jj jjk| j jk| jj jjk| jj jjk| jj jjkg}tdd t	t
|D }|S )Nc             s   s"   | ]\}}|rd |> ndV  qdS )r9   r   Nr   )r&   posmatchr   r   r   r(     s    zBPythonInfo._select_most_likely.<locals>.sort_by.<locals>.<genexpr>)rG   rJ   r
   r   rK   r   r   r   sumr   reversed)r   matchespriority)r   r   r   sort_by  s    

z/PythonInfo._select_most_likely.<locals>.sort_byT)rq   reverser   )sorted)r   r   r   Zsorted_discoveredZmost_likelyr   )r   r   r     s    zPythonInfo._select_most_likelyc             C   s   t  }t  }d |tjj| j< d || j< d |tjj| j< d || j< xJ|j D ]>}tjj|}|j| j	rN|t
| j	d  }d |dj||< qNW d ||< tdd |j D S )Nz{}{}c             s   s   | ]}t jj|r|V  qd S )N)r   r"   r   )r&   r'   r   r   r   r(     s    z4PythonInfo._find_possible_folders.<locals>.<genexpr>)r   r   r"   realpathrO   rP   r   r   r=   r,   r   rn   r   )r?   Zinside_folderZcandidate_folderZexecutablesr   r   Zrelativer   r   r   r     s    

z!PythonInfo._find_possible_foldersc          	   C   s   t  }x|| j D ]p}xjdD ]b}djdd | jd | D }x>dj| jd	gD ]*}x$tD ]}d
j||||}d ||< qXW qNW qW qW t|j S )Nrz   r)   r9   r   rx   c             s   s   | ]}t |V  qd S )N)ry   )r&   r'   r   r   r   r(     s    z6PythonInfo._find_possible_exe_names.<locals>.<genexpr>z-{}r   z{}{}{}{})rz   r)   r9   r   )	r   _possible_baser{   rJ   rn   rK   
EXTENSIONSr   r   )r?   Zname_candidaterM   r   rL   archext	candidater   r   r   r     s    

z#PythonInfo._find_possible_exe_namesc             c   s   t  }tjjtjj| jd jt}d ||< d || j< d|krF|d= d |d< xP|D ]H}|j	 }|V  ddl
m} | rT||kr|V  |j }||krT|V  qTW d S )Nr   r<   )fs_is_case_sensitive)r   r   r"   r   r   rO   rstripr   rG   r   Zvirtualenv.infor   upper)r?   Zpossible_baser   r   r   r   r   r   r   r   r     s"     

zPythonInfo._possible_base)F)N)N)NTFT)T)-r   
__module____qualname____doc__ri   rQ   staticmethodra   propertyr|   r}   r~   r   r   r   rp   rd   r   r   r>   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sP   H

 		
r   __main__)r   
__future__r   r   r   r   r   rE   rerD   r4   collectionsr   r   	distutilsr   distutils.command.installr   stringr   r	   r   r   compiler]   objectr   r   printr   r   r   r   r   <module>   s,   
   J