
    Rh{g                     2   d 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Z	 ej                  e      Z G d d      Z	 	 ddZddZedk(  rd	Z	  e        dZ ej*                  e       yy# e$ r!Z ed
ez  ej(                         Y dZ[5dZ[ww xY w)z
Virtual environment (venv) package for Python. Based on PEP 405.

Copyright (C) 2011-2014 Vinay Sajip.
Licensed to the PSF under a contributor agreement.
    N)pipc                       e Zd ZdZ	 	 	 ddZd Zd Zd Zd Zd Z	e
j                  d	k7  rdd
ZnddZd Zd Zd Zd Zd Zd Zd Zd Zy)
EnvBuildera  
    This class exists to allow virtual environment creation to be
    customized. The constructor parameters determine the builder's
    behaviour when called upon to create a virtual environment.

    By default, the builder makes the system (global) site-packages dir
    *un*available to the created environment.

    If invoked using the Python -m option, the default is to use copying
    on Windows platforms but symlinks elsewhere. If instantiated some
    other way, the default is to *not* use symlinks.

    :param system_site_packages: If True, the system (global) site-packages
                                 dir is available to created environments.
    :param clear: If True, delete the contents of the environment directory if
                  it already exists, before environment creation.
    :param symlinks: If True, attempt to symlink rather than copy files into
                     virtual environment.
    :param upgrade: If True, upgrade an existing virtual environment.
    :param with_pip: If True, ensure pip is installed in the virtual
                     environment
    :param prompt: Alternative terminal prefix for the environment.
    :param upgrade_deps: Update the base venv modules to the latest on PyPI
    Nc                     || _         || _        || _        || _        || _        || _        |dk(  r1t        j                  j                  t        j                               }|| _
        || _        y )N.)system_site_packagesclearsymlinksupgradewith_piporig_promptospathbasenamegetcwdpromptupgrade_deps)selfr   r	   r
   r   r   r   r   s           $/usr/lib/python3.12/venv/__init__.py__init__zEnvBuilder.__init__/   s`     %9!
  !S=WW%%biik2F(    c                    t         j                  j                  |      }| j                  |      }| j                  }d| _        | j                  |       | j                  |       | j                  r| j                  |       | j                  s"| j                  |       | j                  |       |rd| _        | j                  |       | j                  r| j                  |       yy)z
        Create a virtual environment in a directory.

        :param env_dir: The target directory to create an environment in.

        FTN)r   r   abspathensure_directoriesr   create_configurationsetup_pythonr   
_setup_pipr   setup_scripts
post_setupr   upgrade_dependencies)r   env_dircontexttrue_system_site_packagess       r   createzEnvBuilder.create=   s     ''//'*))'2 %)$=$=!$)!!!'*'"==OOG$||w'OOG$$ )-D%%%g.%%g. r   c                    t        j                  |      D ]  }t         j                  j                  ||      }t         j                  j	                  |      st         j                  j                  |      rt        j                  |       wt         j                  j                  |      st        j                  |        y N)
r   listdirr   joinislinkisfileremoveisdirshutilrmtree)r   r   fns      r   clear_directoryzEnvBuilder.clear_directoryY   st    **T" 	"BdB'Bww~~b!RWW^^B%7		"r"b!	"r   c                 @    ||||d}t        j                  |d|      S )N)baseplatbaseinstalled_baseinstalled_platbasevenv)schemevars)	sysconfigget_path)r   r!   namer8   s       r   
_venv_pathzEnvBuilder._venv_patha   s,    %")	
 !!$vDAAr   c                    d }t         j                  t        j                  |      v r t        d| dt         j                   d      t         j                  j                  |      r| j                  r| j                  |       t        j                         }||_
        t         j                  j                  |      d   |_        | j                  | j                  n|j                  }d|z  |_         ||       t        j                  }|st        d      t         j                  j                  t         j                  j!                  |            \  }}||_        ||_        ||_        | j)                  |d      }| j)                  |d	      }	| j)                  |d
      }
|	|_         ||	       |
|_         ||
       t        j.                  dkD  r{t         j0                  dk(  rht        j2                  dk7  rUt         j                  j5                  |d      }t         j                  j                  |      st        j6                  d|       ||_        t         j                  j;                  ||      |_        t         j                  j5                  ||      |_         ||       |j>                  |_         t        j2                  dk(  rt         j                  jC                  |j>                        }t         j                  jE                  |      t         j                  jE                  |j>                        k7  r(tF        jI                  d|j>                  |       ||_         |S )z
        Create the directories for the environment.

        Returns a context object which holds paths in the environment,
        for use by subsequent logic.
        c                    t         j                  j                  |       st        j                  |        y t         j                  j	                  |       st         j                  j                  |       rt        d| z        y )NzUnable to create directory %r)r   r   existsmakedirsr)   r*   
ValueError)ds    r   create_if_neededz7EnvBuilder.ensure_directories.<locals>.create_if_neededr   sR    77>>!$A"bggnnQ&7 !@1!DEE '8r   zRefusing to create a venv in z( because it contains the PATH separator r      z(%s) zUnable to determine path to the running Python interpreter. Provide an explicit path or check that your PATH environment variable is correctly set.scriptsincludepurelibl        posixdarwinlib64libwin32zActual environment location may have moved due to redirects, links or junctions.
  Requested location: "%s"
  Actual location:    "%s")%r   pathsepfspathrA   r   r?   r	   r0   typesSimpleNamespacer!   splitenv_namer   sys_base_executabler   
executable
python_dir
python_exer<   inc_pathlib_pathmaxsizer;   platformr(   symlinkbin_pathrelpathbin_nameenv_exeenv_exec_cmdrealpathnormcaseloggerwarning)r   r!   rC   r"   r   rU   dirnameexenamebinpathincpathlibpath	link_pathreal_env_exes                r   r   zEnvBuilder.ensure_directoriesj   s   	F ::7++<WI F??Azzl!M N N77>>'"tzz  )'')!77==1!4 $ 7W=M=M 6)!))
 . / / 77==)DE'$$//'95//'95//'95"!"![[5 rww''9\\X%Wg6I77>>),

5),"77??7G<'',,w8!  '<<7" 77++GOO<Lww-1A1A'//1RR  <  '	>
 (4$r   c                    t         j                  j                  |j                  d      x|_        }t        |dd      5 }|j                  d|j                  z         | j                  rd}nd}|j                  d|z         |j                  d	t        j                  d
d z         | j                  |j                  d| j                  d       |j                  dt         j                  j                  t        j                        z         g }t         j                  dk(  }|r| j                  r|j!                  d       |s| j                  s|j!                  d       | j"                  s|j!                  d       | j                  r|j!                  d       | j$                  r|j!                  d       | j&                  r|j!                  d       | j(                  r|j!                  d       | j*                  |j!                  d| j*                   d       |j!                  |j                         dj                  |      }|j                  dt        j                   d| d       d
d
d
       y
# 1 sw Y   y
xY w)aA  
        Create a configuration file indicating where the environment's Python
        was copied from, and whether the system site-packages should be made
        available in the environment.

        :param context: The information for the environment creation request
                        being processed.
        z
pyvenv.cfgwutf-8)encodingz
home = %s
truefalsez"include-system-site-packages = %s
zversion = %d.%d.%d
N   z	prompt = 
zexecutable = %s
nt
--symlinks--copies--without-pip--system-site-packages--clear	--upgrade--upgrade-depsz
--prompt="" z
command = z	 -m venv )r   r   r(   r!   cfg_pathopenwriterV   r   rS   version_infor   rb   rU   r;   r
   appendr   r	   r   r   r   )r   r"   r   finclargsru   s          r   r   zEnvBuilder.create_configuration   s    #%'',,w"MM4$g.  	D!GGMG$6$667((GG9D@AGG*S-=-=bq-AAB{{&)DKK?"56GG'"''*:*:3>>*JJKDDBdmmL)dmmJ'==O,((45zzI&||K(  ,-+j)9)9(:!<=KK(88D>DGGj 0	$rBCA 	D  	D  	Ds    H4I==Jru   c                    | j                    }|s	 t        j                  j                  |      s|rut        j                  j	                  |      t        j                  j	                  |      k(  sJ t        j
                  t        j                  j                  |      |       nt        j
                  ||       |rt        j                  ||       yy# t        $ r t        j                  d||       d}Y >w xY w)Y
            Try symlinking a file, and if that fails, fall back to copying.
            Unable to symlink %r to %rTN)r
   r   r   r)   rf   r\   r   	Exceptionrd   re   r-   copyfile)r   srcdstrelative_symlinks_ok
force_copys        r   symlink_or_copyzEnvBuilder.symlink_or_copy   s     "]]*J	&77>>#./#%77??3#7277??3;O#OO#OJJrww'7'7'<cBJJsC0 S)  ! &NN#?cJ!%J&s   B,C "C<;C<c                 2   t         j                  j                  |      xr  t         j                  j                  |       }| j                  r|st         j                  j                  |      s	 |rut         j                  j                  |      t         j                  j                  |      k(  sJ t        j                  t         j                  j                  |      |       yt        j                  ||       yt         j                  j                  t         j                  j                  |            \  }}t         j                  j                  t         j                  j                  t              dd||z         }t        j                          st         j                  j#                  |      sk|j%                  d      r
d|z   }|dd }|dk(  rd}n|d	k(  rd
}t         j                  j                  t         j                  j                  |      ||z         }n|}t         j                  j                  |      s|st        j                  d|       yt'        j(                  ||       y# t        $ r t        j                  d||       Y w xY w)r   Nr   rE   ru   _dpythonvenvlauncherpythonwvenvwlauncherzUnable to copy %r)r   r   lexistsr?   r
   r)   rf   r\   r   r   rd   re   splitextr(   __file__r9   is_python_buildr*   endswithr-   r   )r   r   r   r   bad_srcr   extsrcfns           r   r   zEnvBuilder.symlink_or_copy   s    ggooc*F277>>#3F/FG}}WRWW^^C5HK+!wws3rwws7KKKK

277#3#3C#8#>  

3, GG,,RWW-=-=c-BCMHcGGLL!:!*!%!)C1E ((*"''..2G$$T**C'}Hx'-H*.Hggll277??3#7CH77>>#&NN#6<OOC%9 ! KNN#?cJKs   0A6I2 'I2 2 JJc                    |j                   }|j                  }| j                  }|j                  }t        j
                  dk7  r ||j                  |       t        j                  j                  |      st	        j                  |d       dddt        j                  d    fD ]  }t        j                  j                  ||      }t        j                  j                  |      rC ||j                  |d       t        j                  j                  |      rxt	        j                  |d        y| j                  rt	        j                  |      D cg c]E  }t        j                  j!                  t        j                  j#                  |      d         d	v r|G }}t%        j&                         r||D cg c]2  }t        j                  j!                  |      j)                  d
      r|4 }}n>h d}t        j                  j+                  |j                        }	|j-                  |	       |D ]i  }t        j                  j                  ||      }
t        j                  j/                  |
      sC ||
t        j                  j                  ||             k t%        j&                         rt	        j0                  |j                        D ]  \  }}}d|v st        j                  j+                  |      }t        j                  j                  |j2                  d|      }t        j                  j                  |      st	        j4                  |       t        j                  j                  |d      }
t        j                  j                  |d      }t7        j8                  |
|        y yyc c}w c c}w )z
        Set up a Python executable in the environment.

        :param context: The information for the environment creation request
                        being processed.
        ru   i  r   python3zpython3.rD   T)r   ).exez.dll)r   	vcruntime>   
python.exepythonw.exepython_d.exepythonw_d.exezinit.tclLibN)r]   r`   r   rV   r   r;   rU   r   r)   chmodrS   r   r(   r?   r
   r'   rc   r   r9   r   
startswithr   addr   walkr!   r@   r-   r   )r   r"   rh   r   copierrf   suffixr   suffixesbase_exer   rootdirsfilestcldirr   s                   r   r   zEnvBuilder.setup_python  s    ""%%$$77d?7%%t,77>>$'u%#Y(3;K;KA;N:O0PQ .ww||GV4ww~~d+ 7??DtL77>>$/u-. }}  "zz'2GG$$RWW%5%5a%8%;<@PP   ,,.#+ ((+667NO  H  
 Z77++GOO<X&" ?ggll7F377??3'3Wf =>?
 ((*)+1C1C)D 	%D$!U*!#!1!1$!7!#goouf!M!ww~~f5KK/ ggll4< ggll6:>S1	 +'
 s   A
N<=7Oc                 6   |j                   g|}t        j                  j                         x|d<   }|j                  |d<   |j                  dd       |j                  dd       |j                  |d<   |j                   |d<   t        j                  |fi | y)z8Executes the newly created Python using safe-ish optionsenvVIRTUAL_ENV
PYTHONHOMEN
PYTHONPATHcwdrU   )ra   r   environcopyr!   pop
subprocesscheck_output)r   r"   py_argskwargsr   r   s         r   _call_new_pythonzEnvBuilder._call_new_pythonW  s     $$/w/ jjoo//u$__Md#d#u&33|//r   c                    	 | j                  |ddddt        j                         y
# t        j                  $ r}t	        j
                  d      }t        j                  j                  | d      sLt        dj                  t	        j                         |j                               t        j                  d	        d
}~ww xY w)z1Installs or upgrades pip in a virtual environment-m	ensurepipr{   z--default-pip)stderrstdlibz/ensurepip/__main__.pyan  The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python{}-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: {}
rD   N)r   r   STDOUTCalledProcessErrorr9   r:   r   r   r?   printformatget_python_versionra   rS   exit)r   r"   errr   s       r   r   zEnvBuilder._setup_pipf  s    	!!'4k"1*:K:K " M,, 	''1F77>>VH,B"CD  F9'')7+?+?@B !	s   %( CBB??Cc                     t         j                  j                  t         j                  j                  t                    }t         j                  j                  |d      }| j                  ||       y)a  
        Set up scripts into the created environment from a directory.

        This method installs the default scripts into the environment
        being created. You can prevent the default installation by overriding
        this method if you really need to, or if you need to specify
        a different location for the scripts to install. By default, the
        'scripts' directory in the venv package is used as the source of
        scripts to install.
        rE   N)r   r   r   rf   r   r(   install_scripts)r   r"   r   s      r   r   zEnvBuilder.setup_scripts}  sG     wwrwwx89ww||D),Wd+r   c                      y)a  
        Hook for post-setup modification of the venv. Subclasses may install
        additional packages or scripts here, add activation shell scripts, etc.

        :param context: The information for the environment creation request
                        being processed.
        N r   r"   s     r   r   zEnvBuilder.post_setup  s     	r   c                    |j                   |j                  |j                  |j                  |j                  d}d }d }t
        j                  }|j                  }|j                  d      r|}n$|j                  d      r|}nt
        j                  }|j                         D 	ci c]  \  }}	| ||	       }}}	|j                         D ]  \  }}
|j                  ||
      } |S c c}	}w )ai  
        Replace variable placeholders in script text with context-specific
        variables.

        Return the text passed in , but with variables replaced.

        :param text: The text in which to replace placeholder variables.
        :param context: The information for the environment creation request
                        being processed.
        )__VENV_DIR____VENV_NAME____VENV_PROMPT____VENV_BIN_NAME____VENV_PYTHON__c                 2    | j                  dd      } d|  dS )a  
            This should satisfy PowerShell quoting rules [1], unless the quoted
            string is passed directly to Windows native commands [2].
            [1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules
            [2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing#passing-arguments-that-contain-quote-characters
            'z'')replacess    r   	quote_ps1z/EnvBuilder.replace_variables.<locals>.quote_ps1  s!     		#t$Aqc8Or   c                     | S r&   r   r   s    r   	quote_batz/EnvBuilder.replace_variables.<locals>.quote_bat  s    Hr   z.ps1z.bat)r!   rR   r   r_   r`   shlexquotescript_pathr   itemsr   )r   textr"   replacementsr   r   r   r   keyr   quoteds              r   replace_variableszEnvBuilder.replace_variables  s     $OO$--&~~!(!1!1&
		 ))'E!!&)E KKE4@4F4F4HI&#qU1XII'--/ 	-KC<<V,D	- Js   (C*c                    |j                   }t        |      }t        j                  |      D ]  \  }}}||k(  r1|dd D ](  }|dt        j                  fvs|j                  |       * >|D ]  }	t        j                  dk(  r#|	j                  d      r|	j                  d      r:t        j                  j                  ||	      }
||d j                  t        j                        dd }|s|}n t        j                  j                  |g| }t        j                  j                  |      st        j                  |       t        j                  j                  ||	      }t        |
d      5 }	|	j                         }ddd       |
j                  d      s<|
|_        	 j#                  d      }| j%                  ||      }|j'                  d      }nt        |d
      5 }	|	j/                  |       ddd       t1        j2                  |
|         y# 1 sw Y   xY w# t(        $ r#}d}t*        j-                  d	|
|       Y d}~zd}~ww xY w# 1 sw Y   axY w)as  
        Install scripts into the created environment from a directory.

        :param context: The information for the environment creation request
                        being processed.
        :param path:    Absolute pathname of a directory containing script.
                        Scripts in the 'common' subdirectory of this directory,
                        and those in the directory named for the platform
                        being run on, are installed in the created environment.
                        Placeholder variables are replaced with environment-
                        specific values.
        Ncommonru   r   )r   z.pdb   rbro   z+unable to copy script %r, may be binary: %swb)r]   lenr   r   r;   r+   r   r   r   r(   rQ   sepr?   r@   r   readr   decoder   encodeUnicodeErrorrd   re   r   r-   copymode)r   r"   r   rh   plenr   r   r   rB   r   srcfiler   dstdirdstfiledataes                   r   r   zEnvBuilder.install_scripts  s    ""4y!# "	6D$t|a 'A277 33A'  6GGtOX(>JJ'78'',,tQ/de**2662126$FWW\\';F;Fww~~f-KK''',,vq1'4( $A668D$''(89*1G'H#{{73#55dGD#{{73
 #gt, &&OOGW596"	6&$ $ ( H# (;<CQH HH
& &s0   1H#4H*'IH'*	I3III"c                     t         j                  dt         d|j                           | j                  |ddddgt          y )Nz
Upgrading z packages in r   r   installr{   )rd   debugCORE_VENV_DEPSr]   r   r   s     r   r    zEnvBuilder.upgrade_dependencies  sJ    (g6F6F5GH	
 	gtUI{ 	/-	/r   )FFFFFNF)F)__name__
__module____qualname____doc__r   r$   r0   r<   r   r   r   r;   r   r   r   r   r   r   r   r   r    r   r   r   r   r      sx    2 :?GK#)/8"BCJ*DX 
ww$	*&)	&V;z0.,.`16f/r   r   c                 H    t        ||||||      }|j                  |        y)z,Create a virtual environment in a directory.)r   r	   r
   r   r   r   N)r   r$   )r!   r   r	   r
   r   r   r   builders           r   r$   r$     s,     .B$x( &\CG NN7r   c           
         dd l }|j                  t        dd      }|j                  dddd	       |j                  d
dddd       t        j
                  dk(  rd}nd}|j                         }|j                  d|ddd       |j                  d| ddd       |j                  ddddd       |j                  ddddd       |j                  ddddd !       |j                  d"d#$       |j                  d%ddd&d'd(j                  t               d)       |j                  |       }|j                  r|j                  rt        d*      t        |j                  |j                  |j                  |j                  |j                   |j"                  |j$                  +      }|j&                  D ]  }|j)                  |        y ),Nr   zFCreates virtual Python environments in one or more target directories.z|Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate script in its bin directory.)progdescriptionepilogr   ENV_DIR+z)A directory to create the environment in.)metavarnargshelpry   F
store_truesystem_sitezDGive the virtual environment access to the system site-packages dir.)defaultactiondestr  ru   Trv   r
   z[Try to use symlinks rather than copies, when symlinks are not the default for the platform.rw   store_falsez\Try to use copies rather than symlinks, even when symlinks are the default for the platform.rz   r	   zcDelete the contents of the environment directory if it already exists, before environment creation.r{   r   zlUpgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.rx   r   z]Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default))r  r  r  r  z--promptz;Provides an alternative prompt prefix for this environment.)r  r|   r   zUpgrade core dependencies (z, z) to the latest version in PyPIz1you cannot supply --upgrade and --clear together.)r   r	   r
   r   r   r   r   )argparseArgumentParserr  add_argumentr   r;   add_mutually_exclusive_groupr(   r   
parse_argsr   r	   rA   r   r  r
   r   r   r   r   r$   )r   r  parseruse_symlinksgroupoptionsr  rB   s           r   mainr  
  s(   $$(2@-D % 	EF 	H  J
0%+-9  : 
ww$//1E	|\*,  -
 
z|+;+*,  -
 	5$ ,C  D
 U<& .K  L
 j $]+  ,
 
1  2 (%+:499^;T:U V= =  > %G7==LMMg.A.A&}}")"2"2!(")"2"2 '&-&:&:<G \\ qr   __main__rD   z	Error: %s)file)FFFFNFr&   )r  loggingr   r-   r   rS   r9   rO   r   r   	getLoggerr  rd   r   r$   r  rcr   r   r   r   r   r   r   r   <module>r%     s     	   
    			8	$i/ i/X 7<EJCL z	
B0 CHHRL 
  0kAoCJJ//0s   	A0 0B5BB