a
    lc<B                     @   s  d 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
mZ ddlmZmZmZ ddlZeddZg d	Zd
dgZdZdejjejjf ZG dd deZG dd dZdd ZG dd deZG dd dZG dd deeZG dd deeZ dCddZ!dd  Z"d!d" Z#d#d$ Z$d%d& Z%ed'd(d)d* Z&dDd+d+d,d-d.Z'dd/d0d1Z(dd/d2d3Z)d+dd4d5d6Z*dEd7d8Z+d9d: Z,d;d< Z-d=d> Z.d?d@ Z/dAdB Z0dS )Fz
Environments are a way to activate different Python versions or Virtualenvs for
static analysis. The Python binary in that environment is going to be executed.
    N)
namedtuple)which)memoize_method
time_cache)CompiledSubprocessInferenceStateSameProcessInferenceStateSubprocessVersionInfozmajor minor micro)z3.10z3.9z3.8z3.7z3.6z/usr/binz/usr/local/binZCONDA_PREFIX%s.%sc                   @   s   e Zd ZdZdS )InvalidPythonEnvironmentz
    If you see this exception, the Python executable or Virtualenv you have
    been trying to use is probably not a correct Python version.
    N)__name__
__module____qualname____doc__ r   r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/jedi/api/environment.pyr      s   r   c                   @   s$   e Zd Zedd Zedd ZdS )_BaseEnvironmentc                 C   s    d| j j| j jf }tj|dS )Nr
   version)version_infomajorminorparsoZload_grammar)selfversion_stringr   r   r   get_grammar"   s    z_BaseEnvironment.get_grammarc                 C   s2   z| j W S  ty,   t| j| _ | j  Y S 0 d S N)_hashAttributeError_calculate_sha256_for_file
executabler   r   r   r   _sha256'   s
    z_BaseEnvironment._sha256N)r   r   r   r   r   propertyr"   r   r   r   r   r   !   s   
r   c                   C   s   t jt jt jd d fS N   )sysr    prefixr   r   r   r   r   	_get_info0   s    r(   c                   @   sB   e Zd ZdZdZdddZdd Zdd Zd	d
 Ze	dd Z
dS )Environmentz
    This class is supposed to be created by internal Jedi architecture. You
    should not create it directly. Please use create_environment or the other
    functions instead. It is then returned by that function.
    Nc                 C   s   || _ || _|   d S r   )_start_executable	_env_vars_get_subprocess)r   r    env_varsr   r   r   __init__@   s    zEnvironment.__init__c              
   C   s   | j d ur| j js| j S z$t| j| jd| _ | j d t}W n6 tyr } ztd| j|f W Y d }~n
d }~0 0 |d | _	|d | _
t|d  | _| j S )Nr-   z,Could not get version information for %r: %rr         )_subprocessZ
is_crashedr   r*   r+   Z_sendr(   	Exceptionr   r    path_VersionInfor   )r   infoexcr   r   r   r,   F   s$    

zEnvironment._get_subprocessc                 C   s*   d dd | jD }d| jj|| jf S )N.c                 s   s   | ]}t |V  qd S r   )str).0ir   r   r   	<genexpr>f       z'Environment.__repr__.<locals>.<genexpr>z<%s: %s in %s>)joinr   	__class__r   r4   )r   r   r   r   r   __repr__e   s    zEnvironment.__repr__c                 C   s   t ||  S r   )r   r,   r   Zinference_stater   r   r   get_inference_state_subprocessi   s    z*Environment.get_inference_state_subprocessc                 C   s   |    S )z
        The sys path for this environment. Does not include potential
        modifications from e.g. appending to :data:`sys.path`.

        :returns: list of str
        )r,   get_sys_pathr!   r   r   r   rC   l   s    zEnvironment.get_sys_path)N)r   r   r   r   r2   r.   r,   r@   rB   r   rC   r   r   r   r   r)   8   s   
r)   c                   @   s   e Zd Zdd ZdS )_SameEnvironmentMixinc                 C   s4   t j | _| _t j| _tt jd d  | _d | _d S r$   )r&   r    r*   r'   r4   r5   r   r+   r!   r   r   r   r.   }   s    z_SameEnvironmentMixin.__init__N)r   r   r   r.   r   r   r   r   rD   |   s   rD   c                   @   s   e Zd ZdS )SameEnvironmentN)r   r   r   r   r   r   r   rE      s   rE   c                   @   s   e Zd Zdd Zdd ZdS )InterpreterEnvironmentc                 C   s   t |S r   )r   rA   r   r   r   rB      s    z5InterpreterEnvironment.get_inference_state_subprocessc                 C   s   t jS r   )r&   r4   r!   r   r   r   rC      s    z#InterpreterEnvironment.get_sys_pathN)r   r   r   rB   rC   r   r   r   r   rF      s   rF   VIRTUAL_ENVc                 C   sV   t j| }|rRt j|t jtjkr0t S zt|ddW S  t	yP   Y n0 dS )zGet virtualenv environment from VIRTUAL_ENV environment variable.

    It uses `safe=False` with ``create_environment``, because the environment
    variable is considered to be safe / controlled by the user solely.
    FsafeN)
osenvirongetr4   realpathr&   r'   _try_get_same_envcreate_environmentr   )env_varvarr   r   r   _get_virtual_env_from_var   s    rR   c                    s\   t  }t| d2 t fdddD ]}|| q&W d    n1 sJ0    Y  | S )Nrbc                      s     tjS r   )readfilecmpBUFSIZEr   fr   r   <lambda>   r=   z,_calculate_sha256_for_file.<locals>.<lambda>r=   )hashlibsha256openiterupdate	hexdigest)r4   r[   blockr   rW   r   r      s
    *r   c                  C   s,   t  } | dur| S t t}|dur&|S t S )a|  
    Tries to return an active Virtualenv or conda environment.
    If there is no VIRTUAL_ENV variable or no CONDA_PREFIX variable set
    set it will return the latest Python version installed on the system. This
    makes it possible to use as many new Python features as possible when using
    autocompletion and other functionality.

    :returns: :class:`.Environment`
    N)rR   
_CONDA_VARrN   )virtual_env	conda_envr   r   r   get_default_environment   s    
rd   c                  C   s   t  } tj| j dstjdkr.d}n(dtj	d tj
d f dtj	d  df}|D ],}tjtj|}tj|rZt|  S qZt S | S )	Npythonnt)zScripts\python.exe
python.exezbin/python%s.%sr   r0   zbin/python%sz
bin/python)rE   rJ   r4   basenamer    lower
startswithnamer&   r   r   r>   exec_prefixisfiler)   rF   )envZcheckscheckguessr   r   r   rN      s    
rN   c                  C   sN   t jdpt jt} t }| rJt j| t j|jkrJt  t S |S )NrG   )rJ   rK   rL   ra   _get_cached_default_environmentr4   rM   clear_cache)rQ   environmentr   r   r   get_cached_default_environment   s    rt   iX  )secondsc                   C   s&   zt  W S  ty    t  Y S 0 d S r   )rd   r   rF   r   r   r   r   rq      s    rq   T)rI   use_environment_varsc          	   
   c   s   | du rg } t  }|rXt }|dur6|V  ||j tt}|durX|V  ||j | D ]~}tj|snq\tj|}t|D ]T}tj	||}||v rq|| zt
||d}t|V  W q ty   Y q0 qq\dS )a  
    :param paths: A list of paths in your file system to be scanned for
        Virtualenvs. It will search in these paths and potentially execute the
        Python binaries.
    :param safe: Default True. In case this is False, it will allow this
        function to execute potential `python` environments. An attacker might
        be able to drop an executable in a path this function is searching by
        default. If the executable has not been installed by root, it will not
        be executed.
    :param use_environment_vars: Default True. If True, the VIRTUAL_ENV
        variable will be checked if it contains a valid VirtualEnv.
        CONDA_PREFIX will be checked to see if it contains a valid conda
        environment.

    :yields: :class:`.Environment`
    NrH   )setrR   addr4   ra   rJ   isdirabspathlistdirr>   _get_executable_pathr)   r   )	pathsrI   rv   Z_used_pathsrb   rc   	directoryr4   r    r   r   r   find_virtualenvs  s4    
r   r/   c              	   c   s4   t D ]*}zt|| dV  W q ty,   Y q0 qdS )a&  
    Ignores virtualenvs and returns the Python versions that were installed on
    your system. This might return nothing, if you're running Python e.g. from
    a portable version.

    The environments are sorted from latest to oldest Python version.

    :yields: :class:`.Environment`
    r/   N)_SUPPORTED_PYTHONSget_system_environmentr   )r-   r   r   r   r   find_system_environments:  s
    
r   c             	   C   sv   t d|  }|r(|tjkr t S t|S tjdkrft| D ]*}zt||dW   S  tyb   Y q:0 q:td|  dS )z
    Return the first Python environment found for a string of the form 'X.Y'
    where X and Y are the major and minor versions of Python.

    :raises: :exc:`.InvalidPythonEnvironment`
    :returns: :class:`.Environment`
    re   rf   r/   z Cannot find executable python%s.N)	r   r&   r    rE   r)   rJ   rk   &_get_executables_from_windows_registryr   )r   r-   exer   r   r   r   M  s    

r   )rI   r-   c                C   s6   t j| r"t| | t| |dS tt| |d|dS )z
    Make it possible to manually create an Environment object by specifying a
    Virtualenv path or an executable path and optional environment variables.

    :raises: :exc:`.InvalidPythonEnvironment`
    :returns: :class:`.Environment`
    r/   rH   )rJ   r4   rm   _assert_safer)   r|   )r4   rI   r-   r   r   r   rO   d  s    
rO   c                 C   sR   t jdkrt j| dd}nt j| dd}t j|sDtd| t|| |S )z:
    Returns None if it's not actually a virtual env.
    rf   Scriptsrg   binre   z%s seems to be missing.)rJ   rk   r4   r>   existsr   r   )r4   rI   re   r   r   r   r|   r  s    

r|   c              
   c   s   dd l }g d}|j|jfD ]}|D ]}|j| d}z`|||@}||dd }tj|d}tj	|rt|V  W d    n1 s0    Y  W q$ t
y   Y q$0 q$qd S )Nr   )z0SOFTWARE\Python\PythonCore\{version}\InstallPathz<SOFTWARE\Wow6432Node\Python\PythonCore\{version}\InstallPathz3SOFTWARE\Python\PythonCore\{version}-32\InstallPathz?SOFTWARE\Wow6432Node\Python\PythonCore\{version}-32\InstallPathr    rg   )winregHKEY_CURRENT_USERHKEY_LOCAL_MACHINEformatOpenKeyQueryValueExrJ   r4   r>   rm   ZWindowsError)r   r   Zsub_keysZroot_keyZsub_keykeyr'   r   r   r   r   r     s    (r   c                 C   s   |rt | stdd S )Nz(The python binary is potentially unsafe.)_is_safer   )executable_pathrI   r   r   r   r     s    r   c                 C   sL   t j| }t|rdS t D ](}|j|kr2 dS |jt|kr dS qdS )NTF)rJ   r4   rM   _is_unix_safe_simpler   r    r"   r   )r   	real_pathrs   r   r   r   r     s    


r   c                    s0   t  rt fddtD S t j}|dkS )Nc                 3   s   | ]}  |V  qd S r   )rj   )r:   pr   r   r   r<     r=   z'_is_unix_safe_simple.<locals>.<genexpr>r   )_is_unix_adminany_SAFE_PATHSrJ   statst_uid)r   uidr   r   r   r     s    
r   c                   C   s(   zt  dkW S  ty"   Y dS 0 d S )Nr   F)rJ   getuidr   r   r   r   r   r     s    r   )rG   )N)T)1r   rJ   r&   rZ   rU   collectionsr   shutilr   Z
jedi.cacher   r   Z"jedi.inference.compiled.subprocessr   r   r   r   r5   r   r   ra   r   r   r   Z_CURRENT_VERSIONr3   r   r   r(   r)   rD   rE   rF   rR   r   rd   rN   rt   rq   r   r   r   rO   r|   r   r   r   r   r   r   r   r   r   <module>   sL   
D
(
6
