o [dq` @sdZddlZddlZddlZddlZddlZddlZddlZdZe e Z GdddZ  dddZ dd d Ze d krhd ZzedZWney`Zzed 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)pip 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)selfrrrr r rrr$/usr/lib/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_dircontexttrue_system_site_packagesrrrcreate;s"        zEnvBuilder.createcCs\t|D]&}tj||}tj|stj|r t|qtj|r+t |qdSN) r listdirr joinislinkisfileremoveisdirshutilrmtree)rr fnrrrclear_directoryWs   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_neededgs  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.win32ScriptsIncludeLibz site-packagesbinincludelibz python%d.%dlposixdarwinlib64zActual 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_dir python_exeplatformr# version_infoinc_pathmaxsizenamesymlinkbin_pathbin_nameenv_exe env_exec_cmdrealpathnormcaseloggerwarning)rrr0rrrCdirnameexenamebinnameincpathlibpathr link_pathbinpath 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 truefalsez"include-system-site-packages = %s zversion = %d.%d.%d Nz prompt =  ) r r r#rcfg_pathopenwriterDrrArGr)rrr finclrrrrs  "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$rTrKr ExceptionrRrSr(copyfile)rsrcdstrelative_symlinks_ok 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) riNrjscriptsrhT_dpython venvlauncherpythonw venvwlauncherzUnable to copy %r)r r lexistsr,rr$rTrKr rkrRrSsplitextr#__file__ sysconfigis_python_buildr%endswithr(rl)rrmrnrobad_srcr extsrcfnrrrrqsB      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. rhirupython3zpython3.r1T)rocSs,g|]}tjtj|ddvr|qS)r1).exez.dll)r r rQrz.0rfrrr s z+EnvBuilder.setup_python..cSs"g|] }tj|dr|qS))ru vcruntime)r r rQ startswithrrrrrs > python.exe pythonw.exe python_d.exe pythonw_d.exezinit.tclr5N)rLrNrqrDr rJrCr r$chmodrArGr#r,rr"r|r}r addrywalkrr-r(rl)rrrZr copierrTsuffixsuffixesbase_exermrootdirsfilestcldirrnrrrrs^                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 optionsenv VIRTUAL_ENV PYTHONHOMEN PYTHONPATHcwdrC)rOr environcopyrpop subprocess check_output)rrpy_argskwargsargsrrrr_call_new_python7s      zEnvBuilder._call_new_pythoncCsnz|j|ddddtjdWd Stjy6td}tj|ds5t d t |j t d w) z1Installs or upgrades pip in a virtual environment-m ensurepip --upgradez --default-pip)stderrstdlibz/ensurepip/__main__.pyanThe 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: {} r1N)rrSTDOUTCalledProcessErrorr|get_pathr r r,printformatget_python_versionrOrAexit)rrrrrrrFs     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. rrN)r r rrTr{r#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. NrrrrrrrlszEnvBuilder.post_setupcCsJ|d|j}|d|j}|d|j}|d|j}|d|j}|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. __VENV_DIR__ __VENV_NAME____VENV_PROMPT____VENV_BIN_NAME____VENV_PYTHON__)replacerr@rrMrN)rtextrrrrreplace_variablesvs  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| dsz|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. Ncommonrhru)rz.pdbr9rbr]z+unable to copy script %r, may be binary: %swb)rLlenr rrJr&rr~r r#r?sepr,r-rdreaddecoderencode UnicodeErrorrRrSrer(copymode)rrr rZplenrrrr/rfsrcfilerdstdirdstfiledataerrrrsZ            zEnvBuilder.install_scriptscCs6tdtd|j|j|ddddgtRdS)Nz Upgrading z packages in rrinstallr)rRdebugCORE_VENV_DEPSrLrrrrrrs  zEnvBuilder.upgrade_dependencies)FFFFFNF)F)__name__ __module__ __qualname____doc__rr r+rrr rJrqrrrrrrrrrrrrrs*  G   +=  2rFcCs"t||||||d}||dS)z,Create a virtual environment in a directory.)rrrr rrN)rr )rrrrr rrbuilderrrrr s r c 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)raraF 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 descriptionepilogrENV_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.)defaultactiondestrrhz --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.rr zlUpgrade 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.)rrrr r rr)rArGhasattrr.argparseArgumentParserr add_argumentr rJadd_mutually_exclusive_grouprr#r parse_argsr rrrrr rrrr ) r compatiblerparser use_symlinksgroupoptionsrr/rrrmains            r__main__r1z Error: %s)file)FFFFNFr!)rloggingr r(rrAr|r=r getLoggerrrRrr rrcrkrrrrrrrrs: 1 N