o
    fc                  
   @  sT  d Z ddl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
Z
ddlmZmZ ddlmZ edg dZdd	 Ze Zed
ZG dd dZedkrejdd Zeedkrled Zedd ZndZeedkred Zedd ZndZejdd e e_e  Zejdeddd eeddd f dS 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
    )annotationsN)OrderedDict
namedtuple)digitsVersionInfo)majorminormicroreleaselevelserialc                   C  s*   t tdgtjdd tjS )N PATHEXT)	listr   fromkeysosenvirongetlowersplitpathsep r   r   R/var/www/html/dev/env/lib/python3.10/site-packages/virtualenv/discovery/py_info.py_get_path_extensions   s   *r   z\{\w+\}c                   @  s  e Zd ZdZdJd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dKddZe	dd Ze	dd Ze	d d! ZdLd#d$ZdLd%d&Ze	d'd( Zed)d* Zd+d, ZdZdZedMd-d.ZedMdNd/d0Zd1d2 Z d3d4 Z!e		5		5	dOd6d7Z"ed8d9 Z#ed:d; Z$ed<d= Z%i Z&dPd>d?Z'd@dA Z(edBdC Z)dDdE Z*dFdG Z+dHdI Z,dS )Q
PythonInfoz.Contains information for a Python interpreter.returnNonec                   s  dd }t j _t  _ jdkrtt j _tt j  _t jdkr%dnd _	t
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 _   _ztd d}W n ty   d}Y nw | _t j _t   _tt j dd  _!t
" }d|v rd _# fddt
$ D  _%i  _&n4t jd d dkrd|v rd _# fddt
$ D  _%i  _&nd  _#dd t
$ D  _% ' (  _&tt
dtt
dd }dd d| ffD  _
t) } j%* D ]}|+dd t,-|D  q	|.d d d |D  _/ fd!d j/0 D } 1d"| _2 1d#| _3tt d$tt d%d  _4d  _5d S )&Nc                 S  s   | d u rd S t j| S N)r   pathabspath)vr   r   r   abs_path#   s   z%PythonInfo.__init__.<locals>.abs_pathPyPyl        @       py_version_nodotprefixbase_prefixreal_prefixbase_exec_prefixexec_prefixvenvTFencodingc                       i | ]}|t j|d  jdqS F)expandscheme	sysconfigget_pathsysconfig_scheme.0iselfr   r   
<dictcomp>R       z'PythonInfo.__init__.<locals>.<dictcomp>   )   
   
deb_systemposix_prefixc                   r,   r-   r0   r4   r7   r   r   r9   \   r:   c                 S  s   i | ]
}|t j|d dqS )F)r.   )r1   r2   r4   r   r   r   r9   c       get_makefile_filename_get_makefile_filenamec                 S  s   i | ]\}}|d ur||qS r   r   r5   kr   r   r   r   r9   h   s
    makefile_filenamec                 s  s    | ]	}|d d V  qdS )   Nr   )r5   rD   r   r   r   	<genexpr>s   s    z&PythonInfo.__init__.<locals>.<genexpr>PYTHONFRAMEWORKc                 S  s   i | ]
}|t |p
d qS r   )r1   get_config_varr4   r   r   r   r9   v   r@   c                   0   i | ]\}}||d ur|  jr jn|qS r   
startswithr%   system_prefixrC   r7   r   r   r9   x       stdlib
platstdlibmaxsizemaxint)6sysplatformpython_implementationimplementationtuplepypy_version_infor   version_inforS   architecturer1   rK   version_nodotversionr   namegetattrr%   r&   r'   r(   r)   
executableoriginal_executable_fast_get_system_executablesystem_executable
__import__ImportErrorhas_venvr   getfilesystemencodingfile_system_encodingstdoutstdout_encodingget_scheme_namesr3   get_path_namessysconfig_pathsdistutils_install_distutils_installcopysetvaluesupdate_CONF_VAR_REfindalladdsysconfig_varsitemssysconfig_pathsystem_stdlibsystem_stdlib_platformmax_size	_creators)r8   r    hasscheme_namesmakefileconfig_var_keyselementconfsr   r7   r   __init__"   sz   





	


zPythonInfo.__init__c                   s   | j s| jdurg| j| jkrg| j du rettdd}|duretj|kretj|r*|S | j	j
| j	j}}| jdkre||fdkretj|  fddd| d| d| fD D ]}tj|rd|  S qXdS | jS )	z?Try to get the system executable by just looking at properties.N_base_executableposix)r<      c                   s   g | ]	}t j |qS r   )r   r   join)r5   exebase_dirr   r   
<listcomp>   s    z:PythonInfo._fast_get_system_executable.<locals>.<listcomp>python.)r'   r&   r%   r`   rU   ra   r   r   existsr[   r   r   dirnamerb   )r8   base_executabler   r   r   r   r   rc      s&   



z&PythonInfo._fast_get_system_executablec                   sZ   | j |}|d u r+| j| j| j| jf  fdd| j D }| j||d	t
j}|S )Nc                   s"   i | ]\}}|| v rd n|qS rJ   r   rC   prefixesr   r   r9      s   " z+PythonInfo.install_path.<locals>.<dictcomp>)
config_var)ro   r   r%   r)   r&   r(   rx   ry   rz   lstripr   sep)r8   keyresultr   r   r   r   install_path   s   zPythonInfo.install_pathc               	     s   t  . t d zddlm}  ddlm} W n ty*   i  Y W  d    S w W d    n1 s5w   Y  | ddi}t	t
drId t
_t   t d |jdd	d
 W d    n1 sdw   Y  tj _    fdd|D S )Nignorer   )dist)SCHEME_KEYSscript_argsz--no-user-cfg
_frameworkinstallT)createc                   s.   i | ]}|t  d | dd tjqS )install_rF   N)r`   r   r   r   )r5   r   r6   r   r   r9      s   . z1PythonInfo._distutils_install.<locals>.<dictcomp>)warningscatch_warningssimplefilter	distutilsr   distutils.command.installr   rf   DistributionhasattrrU   r   get_command_objr   r   r%   finalize_options)r   r   dr   r   r   rp      s*   




zPythonInfo._distutils_installc                 C     d dd | jdd D S )Nr   c                 s      | ]}t |V  qd S r   strr4   r   r   r   rH          z)PythonInfo.version_str.<locals>.<genexpr>r   r<   r   r[   r7   r   r   r   version_str      zPythonInfo.version_strc                 C  r   )Nr   c                 s  r   r   r   r4   r   r   r   rH      r   z1PythonInfo.version_release_str.<locals>.<genexpr>r   r;   r   r7   r   r   r   version_release_str   r   zPythonInfo.version_release_strc                 C  s   | j }d|j d|j S )Nr   r   )r[   r   r   )r8   r[   r   r   r   python_name   s   zPythonInfo.python_namec                 C  
   | j d uS r   )r'   r7   r   r   r   is_old_virtualenv      
zPythonInfo.is_old_virtualenvc                 C  r   r   )r&   r7   r   r   r   is_venv   r   zPythonInfo.is_venvNc                 C  sJ   | j | }|d u r| j}n| j }|| |}|jdi |d|S )N/r   )rn   rx   rq   rt   formatreplace)r8   r   r   r   patternbaser   r   r   rz      s   


zPythonInfo.sysconfig_pathFc                 C  s0   | j d u s	|du rddlm} || | _ | j S )NTr   )CreatorSelector)r~   virtualenv.run.plugin.creatorsr   for_interpreter)r8   refreshr   r   r   r   creators   s   zPythonInfo.creatorsc                   s^     d fdd j D }tj|s-tj jtj 	d}tj|r-|}|S )Nincludec                   rL   r   rM   rC   r7   r   r   r9      rP   z-PythonInfo.system_include.<locals>.<dictcomp>headers)
rz   rx   ry   r   r   r   r   r%   r   r   )r8   r   fallbackr   r7   r   system_include   s   
zPythonInfo.system_includec                 C     | j p| jp| jS r   )r'   r&   r%   r7   r   r   r   rO         zPythonInfo.system_prefixc                 C  r   r   )r'   r(   r)   r7   r   r   r   system_exec_prefix   r   zPythonInfo.system_exec_prefixr   c                 C  s    d | jjdd | j D S )Nz{}({!r})c                 S  s    i | ]\}}| d s||qS )_)rN   rC   r   r   r   r9     s     z'PythonInfo.__repr__.<locals>.<dictcomp>)r   	__class____name____dict__ry   r7   r   r   r   __repr__   s   zPythonInfo.__repr__c                 C  s   d | jjddd d| jf| jd ur| j| jkrdnd | jf| j| j| jhvr*dnd | jfd| jfd	| jfd
t	| j
fd| j d| j ffD S )Nz{}({})z, c                 s  s*    | ]\}}|d ur| d| V  qd S )N=r   rC   r   r   r   rH   	  s    z%PythonInfo.__str__.<locals>.<genexpr>specsystemoriginalr   rV   r^   encoding_fs_io-)r   r   r   r   r   rd   ra   rb   rV   reprr^   ri   rk   r7   r   r   r   __str__  s*   
zPythonInfo.__str__c                 C  s$   d | jddd | jD | jS )Nz{}{}-{}r   c                 s  r   r   r   r4   r   r   r   rH   $  r   z"PythonInfo.spec.<locals>.<genexpr>)r   rX   r   r[   r\   r7   r   r   r   r   "  s   $zPythonInfo.specc                 C  s"   ddl m} || | 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 r=| jtj |j krdS |js=tj | j}|j }tjdkr7tj 	|\}}|
|r7|dt|  }||kr=dS |rP|jdurP|j | j krPdS |jdur]|j| jkr]dS t| jdd |j|j|jfD ]\}}|dur|dur||kr dS qmdS )zXCheck if a given specification can be satisfied by the this python interpreter instance.Twin32NFr   r<   )r   ra   r   r   is_absbasenamerb   rU   rV   splitextendswithlenrX   r   r\   zipr[   r   r   r	   )r8   r   impl_must_matchr   	spec_pathsuffixourreqr   r   r   	satisfies.  s0   


(zPythonInfo.satisfiesc                 C  s&   | j du r| jtj|ddd| _ | j S )
        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_exerU   ra   r   r   r   r   r   currentP     
zPythonInfo.currentc                 C  s&   | j du r| jtj|ddd| _ | j S )r   NTr   )_current_systemr   rU   ra   r   r   r   r   current_systemZ  r   zPythonInfo.current_systemc                 C  s   t j|  ddS )Nr;   )indent)jsondumps_to_dictr7   r   r   r   _to_jsond  r   zPythonInfo._to_jsonc                   s*    fddt  D }|d  |d< |S )Nc                   s$   i | ]}||d krt  |ndqS )r~   N)r`   )r5   varr7   r   r   r9   i  s   $ z'PythonInfo._to_dict.<locals>.<dictcomp>r[   )vars_asdict)r8   datar   r7   r   r   h  s   zPythonInfo._to_dictTc           
   
   C  s   ddl m} |du rtjn|}|| |||||d}t|trJ|rJz	|||}W |S  tyI }	 z|r4 t	d|j
|	 d}W Y d}	~	|S d}	~	ww |S )z9Given a path to an executable get the python information.r   )r   N)envr   ignore_cachez-ignore %s due cannot resolve system due to %r)r   r   r   r   
isinstancer   _resolve_to_system	Exceptionlogginginforb   )
r   r   r   r   r  r   r  r   proposed	exceptionr   r   r   r   n  s    zPythonInfo.from_exec                 C  s   t |}| | S r   )r   loads
_from_dictrq   )r   payloadrawr   r   r   
_from_json  s   
zPythonInfo._from_jsonc                 C  s*   t di |d |d< |  }| |_|S )Nr[   r   )r   rq   r   )r   r   r   r   r   r   r    s   
zPythonInfo._from_dictc           
      C  s   |j }t }|jd u rk|jp|jp|j}||v rZt|dkr)td| |j |_nBt	|
 ddD ]\}\}}td||| q1tdt|d || dd| }	t|	|||< |j||dd}|jd u s|j |jkrx| |j|}||_ |S )	NrF   z$%r links back to itself via prefixes)startz%d: prefix=%s, info=%rz prefixes are causing a circle {}|F)r%   exact)ra   r   rd   r'   r&   r%   r   r  r  	enumeratery   errorr   r   keysRuntimeErrordiscover_exer   )
r   r   targetstart_executabler   r%   atptmsgr   r   r   r    s*   

zPythonInfo._resolve_to_systemc              
   C  s  ||f}|| j v r|rtd||| j |  | j | S td| | |  }| |}g }|d u r4tjn|}|D ] }	|D ]}
| ||	|
|||}|d urW|| j |< |    S q<q8|du ry|ry| || }tj	
|}|| j |< td|| |S dd
|tj	
|}t|)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_foldersr   r   
_check_exe_select_most_likelyr   r   r   r  )r8   r   r%   r  r  r   possible_namespossible_folders
discoveredfolderr_   r  foldersr  r   r   r   r    s2   



zPythonInfo.discover_exec                 C  s   t j||}t j|sd S | j||dd|d}|d u rd S dD ]A}	t||	}
t| |	}|
|krb|	dkrJddd |
D ddd |D }
}|j}td||	|
| |du r_|	|  d S q!|S )	NF)r   r   r  )rX   r\   r[   r[   r   c                 s  r   r   r   r4   r   r   r   rH     r   z(PythonInfo._check_exe.<locals>.<genexpr>z2refused interpreter %s because %s differs %s != %s)
r   r   r   r   r   r`   ra   r  r  append)r8   r   r%  r_   r  r$  r  exe_pathr  itemfoundsearchedra   r   r   r   r     s(   

*
	zPythonInfo._check_exec                   s"    fdd}t | |dd}|d S )Nc                   sx   | 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 )rF   r   Nr   )r5   posmatchr   r   r   rH     s   " zBPythonInfo._select_most_likely.<locals>.sort_by.<locals>.<genexpr>)rX   r[   r   r   r\   r	   r
   r   sumr  reversed)r  matchesr  r   r   sort_by  s   

	z/PythonInfo._select_most_likely.<locals>.sort_byT)r   reverser   )sorted)r$  r  r2  sorted_discoveredr   r1  r   r!    s   zPythonInfo._select_most_likelyc                 C  s   t  }t  }d |tj| j< d || j< d |tj| j< d || j< |D ]}tj|}|| jrC|t	| jd  }d || | < q$d ||< dd |D S )Nc                 S  s   g | ]
}t j|r|qS r   )r   r   r   r4   r   r   r   r     r@   z5PythonInfo._find_possible_folders.<locals>.<listcomp>)
r   r   r   realpathra   rb   r   rN   r%   r   )r8   inside_foldercandidate_folderexecutablesr   r   relativer   r   r   r    s   

z!PythonInfo._find_possible_foldersc                 C  s   t  }|  D ]4}dD ]/}ddd | jd | D }d| j dfD ]}tD ]}| | | | }d ||< q(q$qqt| S )N)r<   r;   rF   r   r   c                 s  r   r   r   r4   r   r   r   rH     r   z6PythonInfo._find_possible_exe_names.<locals>.<genexpr>r   r   )r   _possible_baser   r[   r\   
EXTENSIONSr   r  )r8   name_candidater_   r  r^   archext	candidater   r   r   r    s   
z#PythonInfo._find_possible_exe_namesc                 c  s    t  }tjtj| jd t}d ||< d || j< d|v r$|d= d |d< |D ]$}|	 }|V  ddl
m} | rN||krC|V  | }||krN|V  q*d S )Nr   r   )fs_is_case_sensitive)r   r   r   r   r   ra   rstripr   rX   r   virtualenv.inforA  upper)r8   possible_baser   r   r   rA  rD  r   r   r   r;    s(    
zPythonInfo._possible_base)r   r   )F)r   r   r   )r   r   )NTFTN)TN)-r   
__module____qualname____doc__r   rc   r   staticmethodrp   propertyr   r   r   r   r   r   r   rz   r   r   rO   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      sz    
_	














		




r   __main__rF   r   rG   ) rH  
__future__r   r   r  r   rV   rerU   r1   r   collectionsr   r   stringr   r   r   r<  compileru   r   r   argvr   start_cookie
end_cookier   r  rj   writer   r   r   r   r   <module>   sF    
    
0