a DOgC_ @sdZddlZddlZddlZddlZddlZddlZddlZddlZdZ e e Z GdddZ dddZdd d Ze d krd ZzedZWn6eyZzed eejdWYdZ[n dZ[00eedS)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@seZdZdZd ddZddZdd Zd d Zd d Ze j dkrLd!ddZ n d"ddZ ddZ ddZ ddZddZddZddZddZdS)# 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||_||_||_||_||_|dkr6tjt}||_ ||_ dS)N.) system_site_packagesclearsymlinksupgradewith_pipospathbasenamegetcwdprompt upgrade_deps)selfrrrrr rrr%/usr/lib64/python3.9/venv/__init__.py__init__/szEnvBuilder.__init__cCstj|}||}|j}d|_|||||jrF|||j s`| || ||rtd|_|||j r| |dS)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]L}tj||}tj|s4tj|r@t|q tj|r t |q dSN) r listdirr joinislinkisfileremoveisdirshutilrmtree)rr fnrrrclear_directoryXs   zEnvBuilder.clear_directorycCsdd}tj|r$|jr$||t}||_tj|d|_ |j durT|j n|j }d||_ ||t j }tjtj |\}}||_||_||_t jdkrd}d} tj|d d } n(d }d } tj|d dt jddd } tj|| |_} || || t jdkrXtjdkrXt jdkrXtj|d} tj| sXtd | tj|||_} ||_tj| ||_|| |j|_t jdkrtj|j}tj|tj|jkrtd|j|||_|S)z Create the directories for the environment. Returns a context object which holds paths in the environment, for use by subsequent logic. cSs@tj|st|n$tj|s0tj|r.create_if_neededNz(%s) win32ZScriptsZIncludeLibz 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 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|dddt}|d|j|jrBd}nd}|d||d t j d d |j d ur|d |j d Wd n1s0Yd 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>rr;r@r)rrr fZinclrrrrs  zEnvBuilder.create_configurationntcCs|j }|szRtj|s\|rPtj|tj|ks:Jttj||n t||Wn$tyt d||d}Yn0|rt ||dS)Y Try symlinking a file, and if that fails, fall back to copying. Unable to symlink %r to %rTN) rr r r"rLrCr ExceptionrJrKr&copyfile)rsrcdstrelative_symlinks_okZ force_copyrrrsymlink_or_copys   zEnvBuilder.symlink_or_copycCsztj|otj| }|jr|stj|szH|rhtj|tj|ksRJttj||n t||WdSt yt d||Yn0tj tj|\}}tj tjtdd||}tdstj|sB|drd|}|dd}|dkrd }n|d kr&d }tj tj|||}n|}tj|sj|sft d |dSt||dS) rXNrYscriptsrWTZ_dpythonZ venvlauncherZpythonwZ venvwlauncherzUnable to copy %r)r r lexistsr*rr"rLrCr rZrJrKsplitextr!__file__ sysconfigis_python_buildr#endswithr&r[)rr\r]r^Zbad_srcr extZsrcfnrrrr_s>       cCs|j}|j}|j}|j}tjdkr||j|tj|sFt |ddddt j dfD]F}tj ||}tj |s\||j|ddtj|s\t |dq\n$|jrd d t|D}tdrd d |D}n hd }tj|j}|||D]6}tj ||} tj| r|| tj ||qtdrt|jD]z\} } } d | vrPtj| } tj |jd| } tj | st| tj | d } tj | d }t| |q̐qPdS)z Set up a Python executable in the environment. :param context: The information for the environment creation request being processed. rWirbZpython3zpython3.r/T)r^cSs,g|]$}tjtj|ddvr|qS)r/).exez.dll)r r rIrd.0rVrrr sz+EnvBuilder.setup_python..cSs"g|]}tj|dr|qS))rbZ vcruntime)r r rI startswithrkrrrrms>z pythonw_d.exez pythonw.exez python_d.exez python.exezinit.tclr1N)rDrFr_r>r rBr=r r"chmodr;r@r!r*rr rfrgr addrcwalkrr+r&r[)rrrMr ZcopierrLsuffixsuffixesZbase_exer\rootdirsfilesZtcldirr]rrrrsP             zEnvBuilder.setup_pythoncCs$|jddddg}tj|tjddS)z1Installs or upgrades pip in a virtual environmentz-ImZ ensurepip --upgradez --default-pip)stderrN)rG subprocess check_outputSTDOUTrrcmdrrrr3s 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 rrLrer!install_scripts)rrr rrrr<s 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. Nr)rrrrrrKszEnvBuilder.post_setupc s|j|j|j|j|jd}dd}dd}tj|j}|drF|n|drV|ntjfdd | D}| D]\}}| ||}qz|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_ps1hs z/EnvBuilder.replace_variables..quote_ps1cSs|Srrrrrr quote_batrsz/EnvBuilder.replace_variables..quote_batz.ps1z.batcsi|]\}}||qSrr)rlkeyrquoterr z0EnvBuilder.replace_variables..) rr:rrErFshlexr script_pathrhitemsr) rtextrZ replacementsrrrrZquotedrrrreplace_variablesUs&    zEnvBuilder.replace_variablesc Cs|j}t|}t|D]\}}}||krX|ddD]}|dtjfvr8||q8q|D]~} tjdkr| dr| drq\tj || } ||d tj dd} | s|} ntjj |g| R} tj | st | tj | | } t| d} | }Wdn1s0Y| ds| |_z$|d}|||}|d}Wn8ty}zd}td | |WYd}~n d}~00|dur\t| d } | |Wdn1s0Yt| | q\qdS) 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. NcommonrWrb)rjz.pdbr4rbrOz+unable to copy script %r, may be binary: %swb)rDlenr rqrBr$rnrhr r!r9sepr*r+rTreadrdecoderencode UnicodeErrorrJrKrUr&copymode)rrr rMplenrtrurvr-rVZsrcfilerrZdstdirZdstfiledataerrrr~sL      (    *zEnvBuilder.install_scriptscCs@tdtd|j|jddddg}|tt|dS)Nz Upgrading z packages in z-mrinstallrw)rJdebugCORE_VENV_DEPSrDrGextendry check_callr|rrrrs  zEnvBuilder.upgrade_dependencies)FFFFFNF)F)F)__name__ __module__ __qualname____doc__rrr)rrr rBr_rrrrrr~rrrrrrs& B   +=  03rFcCs"t||||||d}||dS)z,Create a virtual environment in a directory.)rrrr rrN)rr)rrrrr rrbuilderrrrrs rc Csd}tjdkrd}nttds"d}|s2tdnLddl}|jtddd }|jd d d d d|jddddddtj dkrd}nd}| }|jd|dddd|jd| dddd|jdddddd|jddddd d|jd!d"ddd#d$|jd%d&d'|jd(ddd)d* d+ t d||}|jrB|jrBtd,t|j|j|j|j|j|j|jd-}|jD]}||qldS).NT)rRrRF 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 descriptionepilogruZENV_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.)defaultactiondestrrWz --symlinksrz[Try to use symlinks rather than copies, when symlinks are not the default for the platform.z--copies 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.rwrzlUpgrade 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)r;r@hasattrr,argparseArgumentParserr add_argumentr rBadd_mutually_exclusive_groupformatr!r parse_argsrrrrrr rrrur) argsZ compatiblerparserZ use_symlinksgroupoptionsrr-rrrmains~           r__main__r/z Error: %s)file)FFFFNF)N)rloggingr r&ryr;rfr7rr getLoggerrrJrrrrcrZrprintrxexitrrrrs4 /  N(