3 \H@sdZddlZddlZddlZddlZddlZddlZejeZ GdddZ dddZ ddd Z ed krd Z ye dZ Wn4ek rZzed eejd WYddZ[XnXeje dS)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 dkrHddZ d 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. FNcCs(||_||_||_||_||_||_dS)N)system_site_packagesclearsymlinksupgradewith_pipprompt)selfrrrrrrr /usr/lib64/python3.6/__init__.py__init__*s zEnvBuilder.__init__cCsxtjj|}|j|}|j}d|_|j||j||jrF|j||j s`|j ||j ||rtd|_|j|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 create3s       zEnvBuilder.createcCs`xZtj|D]L}tjj||}tjj|s6tjj|rBtj|q tjj|r tj |q WdS)N) r listdirrjoinislinkisfileremoveisdirshutilZrmtree)r rfnr r r clear_directoryMs   zEnvBuilder.clear_directorycCsdd}tjj|r$|jr$|j|tj}||_tjj|d|_ |j dk rT|j n|j }d||_ ||tj }t j dkrd|krtj d}nt j}tjjtjj|\}}||_||_||_t j dkrd } d } tjj|d d } n(d } d} tjj|ddt jddd } tjj|| |_} || || t jdkr|tjdkr|t j dkr|tjj|d} tjj| s|tjd| tjj|| |_}| |_tjj|||_|||S)z Create the directories for the environment. Returns a context object which holds paths in the environment, for use by subsequent logic. cSs@tjj|stj|n$tjj|s0tjj|r.create_if_neededNz(%s) darwin__PYVENV_LAUNCHER__Zwin32ZScriptsZIncludeLibz site-packagesbinincludelibz python%d.%d posixlib64l)r rr"rr!typesSimpleNamespacersplitenv_namerenvironsysplatform executabler python_dirZ python_exer version_infoZinc_pathmaxsizenamesymlinkbin_pathbin_nameenv_exe)r rr&rrenvr9dirnameZexenameZbinnameZincpathZlibpathrZ link_pathbinpathr r r rUsN       zEnvBuilder.ensure_directoriesc Csztjj|jd|_}t|dddL}|jd|j|jrBd}nd}|jd||jd t j d d Wd QRXd 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.cfgwzutf-8)encodingz home = %s trueZfalsez"include-system-site-packages = %s zversion = %d.%d.%d N) r rrrZcfg_pathopenwriter:rr7r;)r rrfZinclr r r rs zEnvBuilder.create_configurationntcCs(|jdrd}n|jdo"|jd}|S)N.pyd.dllTpythonz.exe)rMrN)endswith startswith)r rKresultr r r include_binarys zEnvBuilder.include_binaryc Cs|j }|syRtjj|s\|rPtjj|tjj|ks:ttjtjj||n tj||Wn&tk rt j d||d}YnX|rt j ||dS)zQ Try symlinking a file, and if that fails, fall back to copying. zUnable to symlink %r to %rTN) rr rrrCAssertionErrorr>basename Exceptionloggerwarningrcopyfile)r srcdstrelative_symlinks_okZ force_copyr r r symlink_or_copys  zEnvBuilder.symlink_or_copycs|j}|j}|j}||j||j}tjdkrtjj|sFtj |dxNdD]F}tjj ||}tjj |sL||j|ddtjj|sLtj |dqLWnRd}|j fdd tj |D}x<|D]4} tjj || } tjj || } | |jkr|| | qWtjj ||}tjj|rdfd d tj |D}x4|D],} tjj || } tjj || } || | q4Wxtj|jD]v\} } }d |krrtjj| }tjj |jd |}tjj |stj|tjj | d } tjj |d } tj| | PqrWd S)z Set up a Python executable in the environment. :param context: The information for the environment creation request being processed. rLirOpython3T)r\ZDLLscsg|]}|r|qSr r ).0rK)r,r r sz+EnvBuilder.setup_python..csg|]}|r|qSr r )r_rK)r,r r r`szinit.tclr*N)rOr^)r?rAr]r9r:r r=rrchmodrr"rSrrwalkrUrr#rrY)r rrDrZcopierrCsuffixZsubdirfilesrKrZr[rootdirsZtcldirr )r,r rsN              zEnvBuilder.setup_pythoncCs$|jddddg}tj|tjddS)z1Installs or upgrades pip in a virtual environmentz-ImZ ensurepipz --upgradez --default-pip)stderrN)rA subprocessZ check_outputZSTDOUT)r rcmdr r r rs zEnvBuilder._setup_pipcCs2tjjtjjt}tjj|d}|j||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. scriptsN)r rrrC__file__rinstall_scripts)r rrr r r rs 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 rszEnvBuilder.post_setupcCsJ|jd|j}|jd|j}|jd|j}|jd|j}|jd|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__)replacerr5rr@rA)r textrr r r replace_variabless zEnvBuilder.replace_variablesc!Cs|j}t|}xtj|D]x\}}}||kr`x,|ddD]}|dtjfkr>|j|q>Wqx2|D](} tjj|| } ||djtj dd} | s|} ntjj|f| } tjj | stj | tjj| | } t | d} | j }WdQRX| jds^y$|jd}|j||}|jd}Wn6tk r\}zd}tjd| |WYdd}~XnX|dk rht | d} | j|WdQRXtj| | qhWqWdS) 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. Ncommonr.rbz.exezutf-8z+unable to copy script %r, may be binary: %swb)r?lenr rbr=rrrr4sepr"r#rIreadrPdecoderoencode UnicodeErrorrWrXrJrZcopymode)r rrrDZplenrerfrdr%rKZsrcfilercZdstdirZdstfiledataer r r rl"s@        zEnvBuilder.install_scripts)FFFFFN)F)__name__ __module__ __qualname____doc__r rr!rrr r=rSr]rrrrrorlr r r r rs  8  3  rFcCs t|||||d}|j|dS)z,Create a virtual environment in a directory.)rrrrrN)rr)rrrrrrbuilderr r r rRsrc Csbd}tjd*krd}nttds"d}|s2tdn,ddl}|jtddd }|jd d d d d|jddddddtj dkrd}nd}|j }|jd|dddd|jd| dddd|jdddddd|jddddd d|jd!d"ddd#d$|jd%d&d'|j |}|j r"|j r"td(t|j|j |j|j |j|jd)}x|jD]}|j|qJWdS)+NTrHF 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 descriptionZepilogrfZENV_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.z --upgraderzlUpgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.z --without-piprz]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.)rrrrrr)rHrH)r7r;hasattrr$argparseArgumentParserr{ add_argumentr r=Zadd_mutually_exclusive_group parse_argsrrrrrrrrfr) argsZ compatiblerparserZ use_symlinksgroupZoptionsrr%r r r mainZs\             r__main__r'z Error: %s)file)FFFFN)N)r~Zloggingr rrhr7r2Z getLoggerr{rWrrrZrcrVrzprintrgexitr r r r s* C  H$