
    cD                         d Z ddlZddlZddlZddlZddlZ ej                    dk    Z ej                    dk    Z	 G d de
          Zd Zd Zd	 Zdd
ddZdS )a  
Allow a globally installed Pylint to lint an (active) virtual or Conda
environment.

If a globally installed Pylint is invoked from an active virtualenv or Conda
environment, add the environment's paths to Pylint.

If no virtualenv is active but the CWD contains a virtualenv in a  ``.venv``
folder, activate that env and add its paths to Pylint.

Do nothing if no virtualenv is active or if Pylint is installed within the
active virtualenv.

Activation logic taken from https://github.com/pypa/virtualenv

Usage:

- Add an init hook to your ``.pylintrc``::
    init-hook=
        try: import pylint_venv
        except ImportError: pass
        else: pylint_venv.inithook()

- Add the init hook as command line argument::

    pylint --init-hook="import pylint_venv; pylint_venv.inithook()"

- Add the init hook as command line argument and explicitly pass a venv::

    pylint --init-hook="import pylint_venv; pylint_venv.inithook('$(pwd)/env')"

- If Pylint itself is installed in a virtualenv, then you can ignore it by passing
``force_venv_activation=True`` to force activation of a different virtualenv::

    pylint --init--hook="import pylint_venv; pylint_venv.inithook(force_venv_activation=True)"

    NWindowsPyPyc                       e Zd ZdS )IncompatibleVenvErrorN)__name__
__module____qualname__     +lib/python3.11/site-packages/pylint_venv.pyr   r   2   s        Dr   r   c                      t          t          dt          j                  t          j        k    } t          t          d          }| p|S )z;Return *True* if a virtual environment is currently active.base_prefixreal_prefix)getattrsysprefixhasattr)is_conda_envis_virtualenvs     r   is_venvr   6   s6    3sz::cjHLC//M(=(r   c            	         dD ]} t          j        | d          }|r|c S t          j        dd                              d          }t          j                    }t          rdnd}|D ]c}t           j                            t           j                            |||d                    r"t           j                            ||          c S dd	S )
z#Check for a virtualenv or Conda env)VIRTUAL_ENVCONDA_PREFIX PYLINT_VENV_PATHz.venv:ScriptsbinactivateN)osgetenvsplitgetcwdIS_WINpathisfilejoin)varvenv
venv_pathscwd	exec_pathvenv_dirs         r   detect_venvr.   =   s    .  yb!! 	KKK	
 -w77==cBBJ
)++C#.		I / /7>>"',,sHiLLMM 	/7<<X.....	/ 4r   c                 <   t           r!t          j                            | d          }nt          r"t          j                            | dd          }nt          j                            | d          }d t          j        |          D             }t          |          dk    rt          d| d          t          |          dk    rt          d| d	          t          j                            ||d         d          }t          t          j                  t          j        |           t          j        t          _        | t          _        t          t          j                  }fd
|D             }fd|D             }||z   t          j        dd<   dS )z3Activate the virtual environment with prefix *venv*zsite-packagesLiblibc                 <    g | ]}t          j        d |          |S )zpython\d+.\d+)rematch).0ds     r   
<listcomp>z!activate_venv.<locals>.<listcomp>X   s+    WWWQBRTU9V9VWqWWWr   r   zThe virtual environment z& is missing a lib/pythonX.Y directory.   z( has multiple lib/pythonX.Y directories.c                     g | ]}|v|	S r
   r
   r5   iprevs     r   r7   z!activate_venv.<locals>.<listcomp>j   s"    111q1D=1111r   c                     g | ]}|v |	S r
   r
   r:   s     r   r7   z!activate_venv.<locals>.<listcomp>k   s"    ...AI.!...r   N)IS_PYPYr    r%   r'   r$   listdirlenr   setr   site
addsitedirr   r   list)r)   site_packageslib_dirpython_dirsnew	new_paths
kept_pathsr<   s          @r   activate_venvrK   P   s    OT?;;	 OT5/BB',,tU++WW"*W"5"5WWW{q  	'Y4YYY   {a 	'[4[[[   Wk!noNNsx==DOM"""jCOCJ sx..C1111C111I....S...Jj(CHQQQKKKr   F)quietc                    |st                      rdS | t                      } | dS |st          d|  t          j                   t          |            dS )a-  Add virtualenv's paths and site_packages to Pylint.

    Use environment with prefix *venv* if provided else try to auto-detect an active virtualenv.
    Pass *force_venv_activation=True* if Pylint itself is installed in a different virtualenv.

    Passing in *quiet=True* suppresses all output.
    NzUsing env: )file)r   r.   printr   stderrrK   )r)   force_venv_activationrL   s      r   inithookrR   o   st     ! WYY  }}  5"D""4444$r   )NF)__doc__r    platformr3   rB   r   systemr$   python_implementationr>   	Exceptionr   r   r.   rK   rR   r
   r   r   <module>rX      s   $ $L 
			  				  



 
		i	'
((
(
*
*f
4	 	 	 	 	I 	 	 	) ) )  &) ) )>e       r   