ó
’›V]c           @   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 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 d	 d
 d d d d g Z d d g Z d e j j e j j f Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ  d e e f d „  ƒ  YZ! d „  Z" d  „  Z# d! „  Z$ d" „  Z% d# „  Z& e d$ d2 ƒ d' „  ƒ Z' d d( „ Z) d) „  Z* d* „  Z+ e, d+ „ Z- e, d, „ Z. d- „  Z/ d. „  Z0 d/ „  Z1 d0 „  Z2 d1 „  Z3 d S(3   s¡   
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.
iÿÿÿÿN(   t
   namedtuple(   t   highest_pickle_protocolt   which(   t   memoize_methodt
   time_cache(   t   CompiledSubprocesst   EvaluatorSameProcesst   EvaluatorSubprocesst   VersionInfos   major minor micros   3.8s   3.7s   3.6s   3.5s   3.4s   2.7s   /usr/bins   /usr/local/bins   %s.%st   InvalidPythonEnvironmentc           B   s   e  Z d  Z RS(   s’   
    If you see this exception, the Python executable or Virtualenv you have
    been trying to use is probably not a correct Python version.
    (   t   __name__t
   __module__t   __doc__(    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR	      s   t   _BaseEnvironmentc           B   s&   e  Z e d  „  ƒ Z e d „  ƒ Z RS(   c         C   s,   d |  j  j |  j  j f } t j d | ƒ S(   Ns   %s.%st   version(   t   version_infot   majort   minort   parsot   load_grammar(   t   selft   version_string(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   get_grammar!   s    c         C   s9   y |  j  SWn' t k
 r4 t |  j ƒ |  _  |  j  SXd  S(   N(   t   _hasht   AttributeErrort   _calculate_sha256_for_filet
   executable(   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   _sha256&   s
    (   R
   R   R   R   t   propertyR   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR       s   c           C   s   t  j t  j t  j d  f S(   Ni   (   t   sysR   t   prefixR   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt	   _get_info/   s    t   Environmentc           B   sG   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z e	 d „  ƒ Z
 RS(   sÜ   
    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.
    c         C   s   | |  _  |  j ƒ  d  S(   N(   t   _start_executablet   _get_subprocess(   R   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   __init__?   s    	c         C   s  |  j  d  k	 r# |  j  j r# |  j  Sy+ t |  j ƒ |  _  |  j  j d  t ƒ } Wn, t k
 r| } t d |  j | f ƒ ‚ n X| d |  _	 | d |  _
 t | d Œ  |  _ |  j j d k rã |  j	 j ƒ  |  _	 |  j
 j ƒ  |  _
 n  t t j |  j g ƒ |  j  _ |  j  S(   Ns,   Could not get version information for %r: %ri    i   i   (   t   _subprocesst   Nonet
   is_crashedR   R!   t   _sendR   t	   ExceptionR	   R   t   patht   _VersionInfoR   R   t   decodeR   R   t   _pickle_protocol(   R   t   infot   exc(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR"   D   s&    c         C   s6   d j  d „  |  j Dƒ ƒ } d |  j j | |  j f S(   Nt   .c         s   s   |  ] } t  | ƒ Vq d  S(   N(   t   str(   t   .0t   i(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pys	   <genexpr>m   s    s   <%s: %s in %s>(   t   joinR   t	   __class__R
   R)   (   R   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   __repr__l   s    c         C   s   t  | |  j ƒ  ƒ S(   N(   R   R"   (   R   t	   evaluator(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   get_evaluator_subprocessp   s    c         C   s   |  j  ƒ  j ƒ  S(   sž   
        The sys path for this environment. Does not include potential
        modifications like ``sys.path.append``.

        :returns: list of str
        (   R"   t   get_sys_path(   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR8   s   s    N(   R
   R   R   R%   R$   R#   R"   R5   R7   R   R8   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR    7   s   		(		t   _SameEnvironmentMixinc           B   s   e  Z d  „  Z RS(   c         C   s9   t  j |  _ |  _ t  j |  _ t t  j d  Œ  |  _ d  S(   Ni   (   R   R   R!   R   R)   R*   R   (   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR#   „   s    (   R
   R   R#   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR9   ƒ   s   t   SameEnvironmentc           B   s   e  Z RS(    (   R
   R   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR:   Š   s   t   InterpreterEnvironmentc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s
   t  | ƒ S(   N(   R   (   R   R6   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR7      s    c         C   s   t  j S(   N(   R   R)   (   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR8   ’   s    (   R
   R   R7   R8   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR;   Ž   s   	c          C   su   t  j j d ƒ }  |  rq t  j j |  ƒ t  j j t j ƒ k rF t ƒ  Sy t |  d t	 ƒSWqq t
 k
 rm qq Xn  d S(   sÜ   Get 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.
    t   VIRTUAL_ENVt   safeN(   t   ost   environt   getR)   t   realpathR   R   t   _try_get_same_envt   create_environmentt   FalseR	   (   t   var(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   _get_virtual_env_from_var–   s    'c            s^   t  j ƒ  } t |  d ƒ 7 ‰  x- t ‡  f d †  d ƒ D] } | j | ƒ q7 WWd  QX| j ƒ  S(   Nt   rbc              s   ˆ  j  t j ƒ S(   N(   t   readt   filecmpt   BUFSIZE(    (   t   f(    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   <lambda>®   t    RM   (   t   hashlibt   sha256t   opent   itert   updatet	   hexdigest(   R)   RO   t   block(    (   RK   s3   lib/python2.7/site-packages/jedi/api/environment.pyR   «   s
    c          C   s    t  ƒ  }  |  d k	 r |  St ƒ  S(   sB  
    Tries to return an active Virtualenv. If there is no VIRTUAL_ENV variable
    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(   RF   R%   RB   (   t   virtual_env(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   get_default_environment³   s    		c          C   sÇ   t  ƒ  }  t j j |  j ƒ j ƒ  j d ƒ sÃ t j d k rE d
 } n2 d t j	 d t j
 d f d t j	 d d	 f } xB | D]: } t j j t j | ƒ } t j j | ƒ r~ t | ƒ Sq~ Wt ƒ  S|  S(   Nt   pythont   nts   Scripts\python.exes
   python.exes   bin/python%s.%si    i   s   bin/python%ss
   bin/python(   s   Scripts\python.exes
   python.exe(   R:   R>   R)   t   basenameR   t   lowert
   startswitht   nameR   R   R   R3   t   exec_prefixt   isfileR    R;   (   t   envt   checkst   checkt   guess(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRB   Ã   s    	$		c          C   s]   t  j j d ƒ }  t ƒ  } |  rY t  j j |  ƒ t  j j | j ƒ k rY t j ƒ  t ƒ  S| S(   NR<   (   R>   R?   R@   t   _get_cached_default_environmentR)   RA   t   clear_cache(   RE   t   environment(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   get_cached_default_environmentë   s    	-
t   secondsi
   i<   c           C   s   t  ƒ  S(   N(   RV   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRc   ù   s    c         K   s   d t d „ } | |  |  S(   s{  
    :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. Also the VIRTUAL_ENV variable will be checked if it
        contains a valid Virtualenv.
    :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.

    :yields: :class:`Environment`
    c         s   s  |  d  k r g  }  n  t ƒ  } t ƒ  } | d  k	 rK | V| j | j ƒ n  x» |  D]³ } t j j | ƒ sp qR n  t j j | ƒ } x€ t j | ƒ D]o } t j j	 | | ƒ } | | k r¿ q’ n  | j | ƒ y! t
 | d | ƒ} t | ƒ VWq’ t k
 r q’ Xq’ WqR Wd  S(   NR=   (   R%   t   setRF   t   addR)   R>   t   isdirt   abspatht   listdirR3   t   _get_executable_pathR    R	   (   t   pathsR=   t   _used_pathsRU   t	   directoryR)   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt	   py27_comp  s*    			N(   R%   t   True(   Rn   t   kwargsRq   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   find_virtualenvsþ   s    c          c   s8   x1 t  D]) }  y t |  ƒ VWq t k
 r/ q Xq Wd S(   s%  
    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`
    N(   t   _SUPPORTED_PYTHONSt   get_system_environmentR	   (   R   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   find_system_environments.  s
    
c         C   s•   t  d |  ƒ } | r6 | t j k r, t ƒ  St | ƒ St j d k r x9 t |  ƒ D]( } y t | ƒ SWqR t k
 ry qR XqR Wn  t d |  ƒ ‚ d S(   sâ   
    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`
    RW   RX   s    Cannot find executable python%s.N(	   R   R   R   R:   R    R>   R\   t&   _get_executables_from_windows_registryR	   (   R   t   exe(    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRv   A  s    
c         C   s?   t  j j |  ƒ r) t |  | ƒ t |  ƒ St t |  d | ƒƒ S(   sÐ   
    Make it possible to manually create an Environment object by specifying a
    Virtualenv path or an executable path.

    :raises: :exc:`.InvalidPythonEnvironment`
    :returns: :class:`Environment`
    R=   (   R>   R)   R^   t   _assert_safeR    Rm   (   R)   R=   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRC   X  s    
c         C   sx   t  j d k r* t  j j |  d d ƒ } n t  j j |  d d ƒ } t  j j | ƒ sg t d | ƒ ‚ n  t | | ƒ | S(   s:   
    Returns None if it's not actually a virtual env.
    RX   t   Scriptss
   python.exet   binRW   s   %s seems to be missing.(   R>   R\   R)   R3   t   existsR	   Rz   (   R)   R=   RW   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRm   f  s    c      	   c   sþ   y d d  l  } Wn t k
 r/ d d  l } n Xd d d d g } xµ | j | j g D]¡ } x˜ | D] } | j d |  ƒ } yd | j | | ƒ L } | j | d ƒ d } t j	 j
 | d	 ƒ } t j	 j | ƒ r× | Vn  Wd  QXWqb t k
 rñ qb Xqb WqU Wd  S(
   Niÿÿÿÿs0   SOFTWARE\Python\PythonCore\{version}\InstallPaths<   SOFTWARE\Wow6432Node\Python\PythonCore\{version}\InstallPaths3   SOFTWARE\Python\PythonCore\{version}-32\InstallPaths?   SOFTWARE\Wow6432Node\Python\PythonCore\{version}-32\InstallPathR   RM   i    s
   python.exe(   t   winregt   ImportErrort   _winregt   HKEY_CURRENT_USERt   HKEY_LOCAL_MACHINEt   formatt   OpenKeyt   QueryValueExR>   R)   R3   R^   t   WindowsError(   R   R~   t   sub_keyst   root_keyt   sub_keyt   keyR   Ry   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRx   v  s&    	c         C   s&   | r" t  |  ƒ r" t d ƒ ‚ n  d  S(   Ns(   The python binary is potentially unsafe.(   t   _is_safeR	   (   t   executable_pathR=   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyRz   ‘  s    c         C   sf   t  j j |  ƒ } t | ƒ r" t Sx= t ƒ  D]2 } | j | k rE t S| j t | ƒ k r, t Sq, Wt	 S(   N(
   R>   R)   RA   t   _is_unix_safe_simpleRr   Rw   R   R   R   RD   (   RŒ   t	   real_pathRe   (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR‹   —  s    
c            s?   t  ƒ  r# t ‡  f d †  t Dƒ ƒ St j ˆ  ƒ j } | d k S(   Nc         3   s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   R[   (   R1   t   p(   RŽ   (    s3   lib/python2.7/site-packages/jedi/api/environment.pys	   <genexpr>·  s    i    (   t   _is_unix_admint   anyt   _SAFE_PATHSR>   t   statt   st_uid(   RŽ   t   uid(    (   RŽ   s3   lib/python2.7/site-packages/jedi/api/environment.pyR   ³  s    	
c           C   s-   y t  j ƒ  d k SWn t k
 r( t SXd  S(   Ni    (   R>   t   getuidR   RD   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyR   Æ  s    iX  (4   R   R>   R   RN   RI   t   collectionsR    t   jedi._compatibilityR   R   t
   jedi.cacheR   R   t!   jedi.evaluate.compiled.subprocessR   R   R   R   R*   Ru   R’   R   R   R   t   _CURRENT_VERSIONR(   R	   t   objectR   R   R    R9   R:   R;   RF   R   RV   RB   Rf   Rc   R%   Rt   Rw   Rv   Rr   RC   Rm   Rx   Rz   R‹   R   R   (    (    (    s3   lib/python2.7/site-packages/jedi/api/environment.pyt   <module>   sH   	L				(	0						