e fL@s dZddlZddlZddlZddlZddlZddlZejeZ GdddZ ddddddZ ddd Z ed kr d Z ye dZ Wn<ek rZzed ed ejWYddZ[XnXeje ndS)a Virtual environment (venv) package for Python. Based on PEP 405. Copyright (C) 2011-2014 Vinay Sajip. Licensed to the PSF under a contributor agreement. usage: python -m venv [-h] [--system-site-packages] [--symlinks] [--clear] [--upgrade] ENV_DIR [ENV_DIR ...] Creates virtual Python environments in one or more target directories. positional arguments: ENV_DIR A directory to create the environment in. optional arguments: -h, --help show this help message and exit --system-site-packages Give the virtual environment access to the system site-packages dir. --symlinks Attempt to symlink rather than copy. --clear Delete the environment directory if it already exists. If not specified and the directory exists, an error is raised. --upgrade Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place. --without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default) Nc@seZdZdZdddddddZddZddZd d Zd d Ze j d kr{ddZ ndddZ ddZ ddZddZddZddZddZdS) EnvBuilderan 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 and the target directory exists, it is deleted. Otherwise, if the target directory exists, an error is raised. :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 FcCs1||_||_||_||_||_dS)N)system_site_packagesclearsymlinksupgradewith_pip)selfrrrrrr 2/opt/alt/python34/lib64/python3.4/venv/__init__.py__init__As     zEnvBuilder.__init__cCs~tjj|}|j|}|j||j||jrT|j|n|jsz|j ||j |ndS)z Create a virtual environment in a directory. :param env_dir: The target directory to create an environment in. N) ospathabspathensure_directoriescreate_configuration setup_pythonr _setup_pipr setup_scripts post_setup)renv_dircontextr r r createIs     zEnvBuilder.createcCsxtj|D]q}tjj||}tjj|sOtjj|r_tj|qtjj|rtj |qqWdS)N) r listdirr joinislinkisfileremoveisdirshutilZrmtree)rr fnr r r clear_directoryZs $zEnvBuilder.clear_directorycCsNdd}tjj|r7|jr7|j|ntj}||_tjj|d|_ d|j |_ ||tj }t j dkrd|krtj d}n t j}tjjtjj|\}}||_||_||_t j dkr1d}d } tjj|d d } n8d }d } tjj|ddt jddd } tjj|| |_} || || t jdkrtjdkrt j dkrtjj|d} tjj| stjd| qntjj|||_} ||_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|s"tj|n7tjj|sFtjj|rYtd|ndS)NzUnable to create directory %r)r r existsmakedirsrr ValueError)dr r r create_if_neededjs$z7EnvBuilder.ensure_directories..create_if_neededz(%s) darwin__PYVENV_LAUNCHER__win32ZScriptsIncludeLibz site-packagesbinincludelibz python%d.%dN posixlib64l)r r r!rr typesSimpleNamespacersplitenv_namepromptenvironsysplatform executabler python_dirZ python_exer version_infoZinc_pathmaxsizenamesymlinkbin_pathbin_nameenv_exe)rrr%renvr;dirnameZexenameZbinnameZincpathZlibpathr Z link_pathbinpathr r r rbsL      $        zEnvBuilder.ensure_directoriesc Cstjj|jd|_}t|dddb}|jd|j|jr]d}nd}|jd||jd t j d d Wd QXd 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.cfgwencodingzutf-8z home = %s trueZfalsez"include-system-site-packages = %s zversion = %d.%d.%d N) r r rrZcfg_pathopenwriter<rr9r=)rrr fZinclr r r rs   zEnvBuilder.create_configurationntcCs:|jdrd}n|jdo3|jd}|S)N.pyd.dllTpythonz.exe)rOrP)endswith startswith)rrMresultr r r include_binarys zEnvBuilder.include_binaryc Cs|j }|syxtjj|s|rttjj|tjj|ksUttjtjj||qtj||nWqtk rt j d||d}YqXn|rt j ||ndS)zQ Try symlinking a file, and if that fails, fall back to copying. zUnable to symlink %r to %rTN) rr r rrEAssertionErrorr@basename Exceptionloggerwarningrcopyfile)rsrcdstrelative_symlinks_okZ force_copyr r r symlink_or_copys * zEnvBuilder.symlink_or_copycs|j}|j}|j}||j||j}tjdkrtjj|shtj |dnxTdD]k}tjj ||}tjj |so||j|ddtjj|stj |dqqoqoWnd}|j fdd tj |D}xW|D]O} tjj || } tjj || } | |jkr|| | qqWtjj ||}tjj|rfd d tj |D}xH|D]=} tjj || } tjj || } || | qWnxtj|jD]\} } }d |krtjj| }tjj |jd |}tjj |s}tj|ntjj | d } tjj |d } tj| | PqqWd S)z Set up a Python executable in the environment. :param context: The information for the environment creation request being processed. rNirQpython3r^TZDLLscs"g|]}|r|qSr r ).0rM)r-r r s z+EnvBuilder.setup_python..cs"g|]}|r|qSr r )rarM)r-r r rbs zinit.tclr+N)zpythonr`)rArCr_r;r<r r?r rchmodrr!rUrrwalkrWrr"rr[)rrrFr ZcopierrEsuffixZsubdirfilesrMr\r]rootdirsZtcldirr )r-r rsN      " " " zEnvBuilder.setup_pythoncCs2|jddddg}tj|dtjdS)z1Installs or upgrades pip in a virtual environmentz-ImZ ensurepipz --upgradez --default-pipstderrN)rC subprocessZ check_outputZSTDOUT)rrcmdr r r rs zEnvBuilder._setup_pipcCsGtjjtjjt}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 r rrE__file__rinstall_scripts)rrr r 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 )rrr r r rszEnvBuilder.post_setupcCsm|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__)replacerr6r7rBrC)rtextrr r r replace_variabless zEnvBuilder.replace_variablescCs!|j}t|}xtj|D]\}}}||krx=|ddD]+}|dtjfkrQ|j|qQqQWq%nx|D]} tjj|| } ||djtj dd} | s|} ntjj|| } tjj | stj | ntjj| | } t | d} | j }WdQX| jdred}nld}y%|jd}|j||}Wn>tk r}zd}tjd | |WYdd}~XnX|dk rt | |} | j|WdQXtj| | qqWq%WdS) 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. NZcommonr/rbz.exewbrGzutf-8z+unable to copy script %r, may be binary: %s)rAlenr rdr?rr rr5sepr!r"rKreadrRdecoderqUnicodeDecodeErrorrYrZrLrZcopymode)rrr rFZplenrgrhrfr$rMZsrcfilereZdstdirZdstfiledatamodeer r r rn.sB    &    zEnvBuilder.install_scriptsN)__name__ __module__ __qualname____doc__r rr rrr r?rUr_rrrrrqrnr r r r r(s    7  3  rFc Cs2td|d|d|d|}|j|dS)ah Create a virtual environment in a directory. By default, makes the system (global) site-packages dir *un*available to the created environment, and uses copying rather than symlinking for files obtained from the source Python installation. :param env_dir: The target directory to create an environment in. :param system_site_packages: If True, the system (global) site-packages dir is available to the environment. :param clear: If True and the target directory exists, it is deleted. Otherwise, if the target directory exists, an error is raised. :param symlinks: If True, attempt to symlink rather than copy files into virtual environment. :param with_pip: If True, ensure pip is installed in the virtual environment rrrrN)rr)rrrrrbuilderr r r r`s rc Cs(d}tjd,krd}nttds6d}n|sKtdnddl}|jdtdd d d }|jd d ddddd|jdddddddddtj dkrd}nd}|j }|jdd|dddddd|jdd| dddddd |jd!dddddd"dd#|jd$dddddd%dd&|jd'dd(dddddd)|j |}|j r|j rtd*ntd+|jd"|j d|jd%|j d(|j}x|jD]}|j|q WdS)-NTrJF base_prefixz.This script is only for use with Python >= 3.3rprogZ descriptionzFCreates virtual Python environments in one or more target directories.Zepilogz|Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate script in its bin directory.rhmetavarZENV_DIRnargs+helpz)A directory to create the environment in.z--system-site-packagesdefaultaction store_truedest system_sitezDGive the virtual environment access to the system site-packages dir.rNz --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)z1you cannot supply --upgrade and --clear together.r)rJrJ)r9r=hasattrr#argparseArgumentParserr| add_argumentr r?Zadd_mutually_exclusive_group parse_argsrrrrrrrhr) argsZ compatiblerparserZ use_symlinksgroupZoptionsrr$r r r mainxsV                r__main__r&z Error: %sfile)rZloggingr rrjr9r3Z getLoggerr|rYrrrrcrXr{printriexitr r r r s(      9D  *