o
    UhHf                  
   @  sf  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ZddlmZmZ ddlmZ edg dZee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rued Zedd ZndZeedkre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   p/var/www/html/construction_image-detection-poc/venv/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	ddkr'dnd _
td _t j _tj _td	d
k _|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|0d" d#d |D  _1 fd$d j12 D } 3d%| _4 3d&| _5tt d'tt d(d  _6d  _7d 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PyPyP       @   py_version_nodotPy_GIL_DISABLED   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>X       z'PythonInfo.__init__.<locals>.<dictcomp>   )   
   
deb_systemposix_prefixc                   r0   r1   r4   r8   r;   r   r   r=   b   r>   c                 S  s   i | ]
}|t j|d dqS )F)r2   )r5   r6   r8   r   r   r   r=   i       get_makefile_filename_get_makefile_filenamec                 S  s   i | ]\}}|d ur||qS r   r   r9   kr   r   r   r   r=   n   s
    makefile_filenamec                 s  s    | ]	}|d d V  qdS )r(   Nr   )r9   rH   r   r   r   	<genexpr>y   s    z&PythonInfo.__init__.<locals>.<genexpr>PYTHONFRAMEWORKc                 S  s   i | ]
}|t |p
d qS r   )r5   get_config_varr8   r   r   r   r=   |   rD   c                   0   i | ]\}}||d ur|  jr jn|qS r   
startswithr)   system_prefixrG   r;   r   r   r=   ~       stdlib
platstdlibmaxsizemaxint)8sysplatformpython_implementationimplementationtuplepypy_version_infor   version_infostructcalcsizearchitecturer5   rN   version_nodotversionr   namefree_threaded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_namesr7   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)r<   r    hasscheme_namesmakefileconfig_var_keyselementconfsr   r;   r   __init__$   s|   





	


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)r9   exebase_dirr   r   
<listcomp>   s    z:PythonInfo._fast_get_system_executable.<locals>.<listcomp>python.)r+   r*   r)   rf   rX   rg   r   r   existsr^   r   r   dirnamerh   )r<   base_executabler   r   r   r   r   ri      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 rM   r   rG   prefixesr   r   r=      s   " z+PythonInfo.install_path.<locals>.<dictcomp>)
config_var)ru   r   r)   r-   r*   r,   r~   r   r   lstripr   sep)r<   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_r(   N)rf   r   r   r   )r9   r   r:   r   r   r=      s   . z1PythonInfo._distutils_install.<locals>.<dictcomp>)warningscatch_warningssimplefilter	distutilsr   distutils.command.installr   rl   DistributionhasattrrX   r   get_command_objr   r   r)   finalize_options)r   r   dr   r   r   rv      s*   




zPythonInfo._distutils_installc                 C     d dd | jdd D S )Nr   c                 s      | ]}t |V  qd S r   strr8   r   r   r   rK          z)PythonInfo.version_str.<locals>.<genexpr>r   r@   r   r^   r;   r   r   r   version_str      zPythonInfo.version_strc                 C  r   )Nr   c                 s  r   r   r   r8   r   r   r   rK      r   z1PythonInfo.version_release_str.<locals>.<genexpr>r   r?   r   r;   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   )r<   r^   r   r   r   python_name   s   zPythonInfo.python_namec                 C  
   | j d uS r   )r+   r;   r   r   r   is_old_virtualenv      
zPythonInfo.is_old_virtualenvc                 C  r   r   )r*   r;   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   )rt   r~   rw   rz   formatreplace)r<   r   r   r   patternbaser   r   r   r      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)r<   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                   rO   r   rP   rG   r;   r   r   r=      rS   z-PythonInfo.system_include.<locals>.<dictcomp>headers)
r   r~   r   r   r   r   r   r)   r   r   )r<   r   fallbackr   r;   r   system_include   s   
zPythonInfo.system_includec                 C     | j p| jp| jS r   )r+   r*   r)   r;   r   r   r   rR         zPythonInfo.system_prefixc                 C  r   r   )r+   r,   r-   r;   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 )_)rQ   rG   r   r   r   r=   	  s     z'PythonInfo.__repr__.<locals>.<dictcomp>)r   	__class____name____dict__r   r;   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   rG   r   r   r   rK     s    z%PythonInfo.__str__.<locals>.<genexpr>specsystemoriginalr   rY   rc   encoding_fs_io-)r   r   r   r   r   rj   rg   rh   rY   reprrc   ro   rq   r;   r   r   r   __str__  s*   
zPythonInfo.__str__c                 C  s6   d | jddd | jD | jrd| jS d| jS )Nz	{}{}{}-{}r   c                 s  r   r   r   r8   r   r   r   rK   ,  r   z"PythonInfo.spec.<locals>.<genexpr>tr   )r   r[   r   r^   re   ra   r;   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_cache1  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 |jdurj|j| jkrjdS t| jdd |j|j|jfD ]\}}|dur|dur||kr dS qzdS )zXCheck if a given specification can be satisfied by the this python interpreter instance.Twin32NFr   r@   )r   rg   r   r   is_absbasenamerh   rX   rY   splitextendswithlenr[   r   ra   re   zipr^   r   r   r	   )r<   r   impl_must_matchr   	spec_pathsuffixourreqr   r   r   	satisfies9  s4   


(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_exerX   rg   r   r   r   r   r   current^     
zPythonInfo.currentc                 C  s&   | j du r| jtj|ddd| _ | j S )r   NTr   )_current_systemr   rX   rg   r   r   r   r   current_systemh  r   zPythonInfo.current_systemc                 C  s   t j|  ddS )Nr?   )indent)jsondumps_to_dictr;   r   r   r   _to_jsonr  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)rf   )r9   varr;   r   r   r=   w  s   $ z'PythonInfo._to_dict.<locals>.<dictcomp>r^   )vars_asdict)r<   datar   r;   r   r  v  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LOGGERinforh   )
r   r   r   r   r	  r   r  r   proposed	exceptionr   r   r   r   |  s    zPythonInfo.from_exec                 C  s   t |}| | S r   )r   loads
_from_dictrw   )r   payloadrawr   r   r   
_from_json  s   
zPythonInfo._from_jsonc                 C  s*   t di |d |d< |  }| |_|S )Nr^   r   )r   rw   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 )	Nr(   z$%r links back to itself via prefixes)startz%d: prefix=%s, info=%rz prefixes are causing a circle {}|F)r)   exact)rg   r   rj   r+   r*   r)   r   r  r  	enumerater   errorr   r   keysRuntimeErrordiscover_exer   )
r   r   targetstart_executabler   r)   atpr   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  )r<   r   r)   r  r  r   possible_namespossible_folders
discoveredfolderrd   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  )r[   ra   r^   r^   r   c                 s  r   r   r   r8   r   r   r   rK     r   z(PythonInfo._check_exe.<locals>.<genexpr>z2refused interpreter %s because %s differs %s != %s)
r   r   r   r   r   rf   rg   r  r#  append)r<   r   r+  rd   r  r*  r  exe_pathr  itemfoundsearchedrg   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 )r(   r   Nr   )r9   posmatchr   r   r   rK     s   " zBPythonInfo._select_most_likely.<locals>.sort_by.<locals>.<genexpr>)r[   r^   r   r   ra   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  r8  sorted_discoveredr   r7  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   r8   r   r   r   r     rD   z5PythonInfo._find_possible_folders.<locals>.<listcomp>)
r   r   r   realpathrg   rh   r   rQ   r)   r   )r<   inside_foldercandidate_folderexecutablesr   r   relativer   r   r   r%    s   

z!PythonInfo._find_possible_foldersc           
   
   C  s   t  }|  D ]F}dD ]A}ddd | jd | D }dg}| jr'|d |D ]"}d| j dfD ]}tD ]}| | | | | }	d ||	< q7q3q)qqt|	 S )N)r@   r?   r(   r   r   c                 s  r   r   r   r8   r   r   r   rK     r   z6PythonInfo._find_possible_exe_names.<locals>.<genexpr>r   r   r   )
r   _possible_baser   r^   re   r-  ra   
EXTENSIONSr   r  )
r<   name_candidaterd   r   rc   modsmod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   rg   rstripr   r[   r   virtualenv.inforI  upper)r<   possible_baser   r   r   rI  rL  r   r   r   rA  $  s(    
zPythonInfo._possible_base)r   r   )F)r   r   r   )r   r   )NTFTN)TN)-r   
__module____qualname____doc__r   ri   r   staticmethodrv   propertyr   r   r   r   r   r   r   r   r   r   rR   r   r   r   r   classmethodr   r   r   r   r   r   r  r  r   r  r  r  r   r  r&  r'  r%  r$  rA  r   r   r   r   r   !   sz    
c	














"		




r   __main__r(   r   rJ   )#rP  
__future__r   r   loggingr   rY   rer_   rX   r5   r   collectionsr   r   stringr   r   	getLoggerr   r  r   rB  compiler{   r   argvr   start_cookie
end_cookier  r  rp   writer   r   r   r   r   <module>   sJ    

    
0