o ?OgSb @sdZddlZddlZddlZddlZddlZddlZddlZddlZdZ e e Z GdddZ  dddZdd d Ze d krld ZzedZWneydZzed eejdWYdZ[ndZ[wweedSdS)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)pipZ setuptoolsc@seZdZdZ   d"ddZddZdd Zd d Zd d Ze j dkr)d#ddZ nd#ddZ ddZ ddZ ddZddZddZddZddZd d!ZdS)$ 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 FNcCsF||_||_||_||_||_|dkrtjt}||_ ||_ dS)N.) system_site_packagesclearsymlinksupgradewith_pipospathbasenamegetcwdprompt upgrade_deps)selfrrrrr rrr4/opt/alt/python310/lib64/python3.10/venv/__init__.py__init__/s zEnvBuilder.__init__cCstj|}||}|j}d|_|||||jr#|||j s0| || ||r:d|_|||j rD| |dSdS)z Create a virtual environment in a directory. :param env_dir: The target directory to create an environment in. FTN)r r abspathensure_directoriesrcreate_configuration setup_pythonr _setup_pipr setup_scripts post_setuprupgrade_dependencies)renv_dircontextZtrue_system_site_packagesrrrcreate<s"        zEnvBuilder.createcCs\t|D]&}tj||}tj|stj|r t|qtj|r+t |qdSN) r listdirr joinislinkisfileremoveisdirshutilZrmtree)rr fnrrrclear_directoryXs   zEnvBuilder.clear_directorycCsdd}tj|r|jr||t}||_tj|d|_ |j dur*|j n|j }d||_ ||t j }|s?t dtjtj|\}}||_||_||_t jdkrfd}d } tj|d d } nd }d } tj|ddt jddd } tj|| |_} || || t jdkrtjdkrt jdkrtj|d} tj| std| tj|||_} ||_tj| ||_|| |j|_t jdkrtj|j}tj|tj|jkrt 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. cSsBtj|s t|dStj|stj|rtd|dS)NzUnable to create directory %r)r r existsmakedirsr"r# ValueError)drrrcreate_if_neededhs  z7EnvBuilder.ensure_directories..create_if_neededNz(%s) zUnable to determine path to the running Python interpreter. Provide an explicit path or check that your PATH environment variable is correctly set.Zwin32ZScriptsZIncludeLibz site-packagesbinZincludelibz python%d.%dlposixdarwinZlib64zActual environment location may have moved due to redirects, links or junctions. Requested location: "%s" Actual location: "%s")!r r r)rr(typesSimpleNamespacersplitenv_namersys_base_executabler+r executable python_dirZ python_exeplatformr! version_infoZinc_pathmaxsizenamesymlinkbin_pathbin_nameenv_exe env_exec_cmdrealpathnormcaseloggerwarning)rrr-rrr;dirnameZexenameZbinnameZincpathZlibpathr Z link_pathbinpathZ real_env_exerrrr`s^        zEnvBuilder.ensure_directoriescCstj|jd|_}t|dddC}|d|j|jr!d}nd}|d||d t j d d |j d urM|d |j d Wd d SWd d S1sXwYd S)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.cfgwutf-8)encodingz home = %s trueZfalsez"include-system-site-packages = %s zversion = %d.%d.%d Nz prompt =  ) r r r!rZcfg_pathopenwriter<rr9r>r)rrr fZinclrrrrs  "zEnvBuilder.create_configurationntcCs|j }|sBz)tj|s.|r(tj|tj|ksJttj||nt||WntyAt d||d}Ynw|rLt ||dSdS)Y Try symlinking a file, and if that fails, fall back to copying. Unable to symlink %r to %rTN) rr r r"rJrAr ExceptionrHrIr&copyfile)rsrcdstrelative_symlinks_okZ force_copyrrrsymlink_or_copys    zEnvBuilder.symlink_or_copycCsrtj|o tj| }|jrO|sOtj|sOz&|r6tj|tj|ks)Jttj||WdSt||WdSt yNt d||Ynwtj tj|\}}tj tjtdd||}tdsutj|s|drd|}|dd}|dkrd }n|d krd }tj tj|||}n|}tj|s|st d |dSt||dS) rVNrWscriptsrUTZ_dpythonZ venvlauncherZpythonwZ venvwlauncherzUnable to copy %r)r r lexistsr)rr"rJrAr rXrHrIsplitextr!__file__ sysconfigis_python_buildr#endswithr&rY)rrZr[r\Zbad_srcr ZextZsrcfnrrrr]sB      cCs|j}|j}|j}|j}tjdkrT||j|tj|s#t |ddddt j dfD]#}tj ||}tj |sQ||j|ddtj|sQt |dq.dS|jrnd d t|D}tdrmd d |D}nhd }tj|j}|||D]}tj ||} tj| r|| tj ||qtdrt|jD]<\} } } d | vrtj| } tj |jd| } tj | st| tj | d } tj | d }t| |dSqdSdS)z Set up a Python executable in the environment. :param context: The information for the environment creation request being processed. rUir`Zpython3zpython3.r.T)r\cSs,g|]}tjtj|ddvr|qS)r.).exez.dll)r r rGrb.0rTrrr s z+EnvBuilder.setup_python..cSs"g|] }tj|dr|qS))r`Z vcruntime)r r rG startswithrhrrrrjs >z pythonw.exez pythonw_d.exez python_d.exez python.exezinit.tclr/N)rBrDr]r<r r@r;r r"chmodr9r>r!r)rr rdrer addrawalkrr*r&rY)rrrKr ZcopierrJsuffixsuffixesZbase_exerZrootdirsfilesZtcldirr[rrrrs^                zEnvBuilder.setup_pythoncOsj|jg|}tj|d<}|j|d<|dd|dd|j|d<|j|d<tj|fi|dS)z8Executes the newly created Python using safe-ish optionsenvZ VIRTUAL_ENVZ PYTHONHOMENZ PYTHONPATHcwdr;)rEr environcopyrpop subprocessZ check_output)rrZpy_argskwargsargsrtrrr_call_new_python8s      zEnvBuilder._call_new_pythoncCs|j|ddddtjddS)z1Installs or upgrades pip in a virtual environment-mZ ensurepip --upgradez --default-pip)stderrN)r|ryZSTDOUTrrrrrrGs  zEnvBuilder._setup_pipcCs2tjtjt}tj|d}|||dS)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. r^N)r r rrJrcr!install_scripts)rrr rrrrLs zEnvBuilder.setup_scriptscCsdS)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. Nrrrrrr[szEnvBuilder.post_setupc s|j|j|j|j|jd}dd}dd}tj|j}|dr#|n |dr+|ntjfdd | D}| D] \}}| ||}q=|S) 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. )Z __VENV_DIR__Z __VENV_NAME__Z__VENV_PROMPT__Z__VENV_BIN_NAME__Z__VENV_PYTHON__cSs|dd}d|dS)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'')replacesrrr quote_ps1xs  z/EnvBuilder.replace_variables..quote_ps1cSs|Srrrrrr quote_batsz/EnvBuilder.replace_variables..quote_batz.ps1z.batcsi|] \}}||qSrr)rikeyrquoterr sz0EnvBuilder.replace_variables..) rr8rrCrDshlexr script_pathrfitemsr) rtextrZ replacementsrrrrZquotedrrrreplace_variableses&    zEnvBuilder.replace_variablesc Cs|j}t|}t|D]\}}}||kr+|ddD]}|dtjfvr)||qq |D]} tjdkr?| dr?| dr?q-tj || } ||d tj dd} | sY|} n tjj |g| R} tj | snt | tj | | } t| d } | }Wdn1swY| ds| |_z|d}|||}|d}Wnty}zd}td | |WYd}~nd}~ww|durt| d  } | |Wdn1swYt| | q-q dS) 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. NcommonrUr`)rgz.pdbr2rbrMz+unable to copy script %r, may be binary: %swb)rBlenr rnr@r$rkrfr r!r7sepr)r*rRreadrdecoderencode UnicodeErrorrHrIrSr&Zcopymode)rrr rKZplenrqrrrsr,rTZsrcfileroZdstdirZdstfiledataerrrrs\            zEnvBuilder.install_scriptscCs6tdtd|j|j|ddddgtRdS)Nz Upgrading z packages in r}rZinstallr~)rHdebugCORE_VENV_DEPSrBr|rrrrrs  zEnvBuilder.upgrade_dependencies)FFFFFNF)F)__name__ __module__ __qualname____doc__rrr(rrr r@r]rr|rrrrrrrrrrrs*  G   += 0 3rFcCs"t||||||d}||dS)z,Create a virtual environment in a directory.)rrrr rrN)rr)rrrrr rrbuilderrrrrs rc Csxd}tjdkr d}nttdsd}|stdddl}|jtddd }|jd d d d d|jddddddtj dkr>d}nd}| }|jd|dddd|jd| dddd|jdddddd|jddddd d|jd!d"ddd#d$|jd%d&d'|jd(ddd)d* d+ t d||}|jr|jrtd,t|j|j|j|j|j|j|jd-}|jD]}||qdS).NT)rPrPF base_prefixz.This script is only for use with Python >= 3.3rzFCreates 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 descriptionZepilogrrZENV_DIR+z)A directory to create the environment in.)metavarnargshelpz--system-site-packages store_true system_sitezDGive the virtual environment access to the system site-packages dir.)defaultactiondestrrUz --symlinksrz[Try to use symlinks rather than copies, when symlinks are not the default for the platform.z--copiesZ store_falsez\Try to use copies rather than symlinks, even when symlinks are the default for the platform.z--clearrzcDelete the contents of the environment directory if it already exists, before environment creation.r~rzlUpgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.z --without-pipr z]Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default))rrrrz--promptz;Provides an alternative prompt prefix for this environment.)rz--upgrade-depsrz;Upgrade core dependencies: {} to the latest version in PyPI z1you cannot supply --upgrade and --clear together.)rrrrr rr)r9r>hasattrr+argparseArgumentParserr add_argumentr r@Zadd_mutually_exclusive_groupformatr!r parse_argsrrrrrr rrrrr) r{Z compatiblerparserZ use_symlinksgroupZoptionsrr,rrrmains            r__main__r.z Error: %s)file)FFFFNFr)rZloggingr r&ryr9rdr5rrZ getLoggerrrHrrrZrcrXrprintrexitrrrrs< > N