U ,aFR @sdZddlZddlZddlZddlZddlZddlZddlZee Z GdddZ dddZ ddd Z e d krd Zze dZWn4ek rZzed eejd W5dZ[XYnXeedS)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. Nc@seZdZdZdddZddZdd Zd d Zd d Ze j dkrLdddZ n d ddZ ddZ ddZ ddZddZddZddZdS)! 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. FNcCs(||_||_||_||_||_||_dSNsystem_site_packagesclearsymlinksupgradewith_pipprompt)selfrrrrr r r 2/opt/alt/python38/lib64/python3.8/venv/__init__.py__init__+s zEnvBuilder.__init__cCsxtj|}||}|j}d|_|||||jrF|||j s`| || ||rtd|_||dS)z Create a virtual environment in a directory. :param env_dir: The target directory to create an environment in. FTN) ospathabspathensure_directoriesrcreate_configuration setup_pythonr _setup_pipr setup_scripts post_setup)r env_dircontextZtrue_system_site_packagesr r r create4s       zEnvBuilder.createcCs\t|D]L}tj||}tj|s4tj|r@t|q tj|r t |q dSr) rlistdirrjoinislinkisfileremoveisdirshutilZrmtree)r rfnr r r clear_directoryNs   zEnvBuilder.clear_directorycCsdd}tj|r$|jr$||t}||_tj|d|_ |j dk rT|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| ||_|| |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) Zwin32ZScriptsZIncludeLibz site-packagesbinZincludelibz python%d.%dlposixdarwinlib64)rrr$rr#typesSimpleNamespacersplitenv_namer sys_base_executabler executable python_dirZ python_exeplatformr version_infoZinc_pathmaxsizenamesymlinkbin_pathbin_nameenv_exe)r rr(rr r7dirnameZexenameZbinnameZincpathZlibpathrZ link_pathbinpathr r r rVsL     zEnvBuilder.ensure_directoriesc Cstj|jd|_}t|dddj}|d|j|jrBd}nd}|d||d t j d d |j d k r|d |j d W5QRXd 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 =  ) rrrrZcfg_pathopenwriter8rr5r:r )r rrfZinclr r r rs  zEnvBuilder.create_configurationntcCs|j }|szRtj|s\|rPtj|tj|ks:tttj||n t||Wn&tk rt d||d}YnX|rt ||dS)Y Try symlinking a file, and if that fails, fall back to copying. Unable to symlink %r to %rTN) rrrrrAAssertionErrorr=basename Exceptionloggerwarningr!copyfile)r srcdstrelative_symlinks_okZ force_copyr r r symlink_or_copys  zEnvBuilder.symlink_or_copycCs|tj|otj| }|jr|stj|szH|rhtj|tj|ksRtttj ||n t||WdSt k rt d||YnXtj tj |\}}tjtjtdd||}tdstj|sD|dr d|}|dd}|dkrd }n|d kr(d }tjtj|||}n|}tj|sl|sht d |dSt||dS) rMNrNscriptsrLTZ_dpythonZ venvlauncherZpythonwZ venvwlauncherzUnable to copy %r)rrlexistsr$rrrArOr=rPrQrRrSsplitextr__file__ sysconfigis_python_buildrendswithr!rT)r rUrVrWZbad_srcrPZextZsrcfnr r r rXs>      cCs|j}|j}|j}|j}tjdkr||j|tj|sFt |ddD]F}tj ||}tj |sJ||j|ddtj|sJt |dqJn |j rddt |D}tdrdd|D}n d d d d g}|D]2}tj ||}tj|r||tj ||qtdrt|jD]z\} } } d | kr&tj| } tj |jd| } tj | spt| tj | d }tj | d } t|| qq&dS)z Set up a Python executable in the environment. :param context: The information for the environment creation request being processed. rLi)r[Zpython3T)rWcSs,g|]$}tjtj|ddkr|qS)r)).exez.dll)rrnormcaser].0rKr r r sz+EnvBuilder.setup_python..cSs"g|]}tj|dr|qS))r[Z vcruntime)rrrc startswithrdr r r rfsz python.exez python_d.exez pythonw.exez pythonw_d.exezinit.tclr*N)r>r@rXr8rr<r7rrchmodrr$rrr_r`r\walkrPrr%r!rT)r rrBrZcopierrAsuffixsuffixesrUrootdirsfilesZtcldirrVr r r rsP             zEnvBuilder.setup_pythoncCs$|jddddg}tj|tjddS)z1Installs or upgrades pip in a virtual environmentz-ImZ ensurepip --upgradez --default-pip)stderrN)r@ subprocessZ check_outputZSTDOUT)r rcmdr r r rs 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. rYN)rrrrAr^rinstall_scripts)r rrr r r r#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 )r rr r r r2szEnvBuilder.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. Z __VENV_DIR__Z __VENV_NAME__Z__VENV_PROMPT__Z__VENV_BIN_NAME__Z__VENV_PYTHON__)replacerr4r r?r@)r textrr r r replace_variables<s zEnvBuilder.replace_variablesc Cs|j}t|}t|D]\}}}||krX|ddD]}|dtjfkr8||q8q|D]H} tjdkr| dr| drq\tj || } ||d tj dd} | s|} ntjj |f| } tj | st | tj | | } t| d} | }W5QRX| dsrz$|d}|||}|d}Wn6tk rp}zd}td | |W5d}~XYnX|dk r\t| d } | |W5QRXt| | 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. NcommonrLr[)rbz.pdbr-rbrDz+unable to copy script %r, may be binary: %swb)r>lenrrir<rrgrarrr3sepr$r%rIreaddecodervencode UnicodeErrorrRrSrJr!Zcopymode)r rrrBZplenrlrmrnr'rKZsrcfilerjZdstdirZdstfiledataer r r rsNsL          zEnvBuilder.install_scripts)FFFFFN)F)F)__name__ __module__ __qualname____doc__rrr#rrrr<rXrrrrrvrsr r r r rs( 4   +<  rFcCs t|||||d}||dS)z,Create a virtual environment in a directory.)rrrr r N)rr)rrrrr r builderr r r rsrc Cs^d}tjdkrd}nttds"d}|s2tdn(ddl}|jtddd }|jd d d d d|jddddddtj dkrd}nd}| }|jd|dddd|jd| dddd|jdddddd|jddddd d|jd!d"ddd#d$|jd%d&d'| |}|j r"|j r"td(t|j|j |j|j |j|jd)}|jD]}||qHdS)*NT)rGrGF 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.)progZ descriptionZepilogrmZENV_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.)defaultactiondestrrLz --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.rorzlUpgrade 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.)rz1you cannot supply --upgrade and --clear together.r)r5r:hasattrr&argparseArgumentParserr add_argumentrr<Zadd_mutually_exclusive_group parse_argsrrrrrr r rmr) argsZ compatiblerparserZ use_symlinksgroupZoptionsrr'r r r mains|          r__main__r)z Error: %s)file)FFFFN)N)rZloggingrr!rqr5r_r1Z getLoggerrrRrrrZrcrQrprintrpexitr r r r s2 q  H$