U
��,aFR�
@s�dZddlZddlZddlZddlZddlZddlZddlZe�e �Z
Gdd�d�Zddd�Zddd �Z
e d
kr�dZze
�dZWn4ek
r�Zzedeejd
�W5dZ[XYnXe�e�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@s�eZdZdZddd�Zdd�Zdd �Zd
d�Zdd
�Ze j
dkrLddd�Zn
d dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�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.
FNcCs(||_||_||_||_||_||_dS�N��system_site_packages�clear�symlinks�upgrade�with_pip�prompt)�selfrrrrr r
�r�2/opt/alt/python38/lib64/python3.8/venv/__init__.py�__init__+szEnvBuilder.__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)�os�path�abspath�ensure_directoriesr�create_configuration�setup_pythonr �
_setup_pipr�
setup_scripts�
post_setup)r�env_dir�contextZtrue_system_site_packagesrrr
�create4s
zEnvBuilder.createcCs\t�|�D]L}tj�||�}tj�|�s4tj�|�r@t�|�q
tj�|�r
t� |�q
dSr)
r�listdirr�join�islink�isfile�remove�isdir�shutilZrmtree)rr�fnrrr
�clear_directoryNszEnvBuilder.clear_directorycCs�dd�}tj�|�r$|jr$|�|�t��}||_tj�|�d|_ |j
dk rT|j
n|j }d||_
||�tj}tj�tj�
|��\}}||_||_||_tjdkr�d}d} tj�|d d
�}
n(d}d} tj�|d
dtjdd�d
�}
tj�|| �|_}||�||
�tjdk�rXtjdk�rXtjdk�rXtj�|d�}tj�|��sXt�d
|�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<td|��dS)NzUnable to create directory %r)rr�exists�makedirsrr�
ValueError)�drrr
�create_if_needed^sz7EnvBuilder.ensure_directories.<locals>.create_if_needed�Nz(%s) Zwin32ZScriptsZInclude�Libz
site-packages�binZinclude�libzpython%d.%d�l�posix�darwin�lib64)rrr$rr#�types�SimpleNamespacer�split�env_namer
�sys�_base_executabler�
executable�
python_dirZ
python_exe�platformr�version_infoZinc_path�maxsize�name�symlink�bin_path�bin_name�env_exe)rrr(rr
r7�dirnameZexenameZbinnameZincpathZlibpathrZ link_path�binpathrrr
rVsL
��zEnvBuilder.ensure_directoriesc Cs�tj�|jd�|_}t|ddd��j}|�d|j�|jrBd}nd}|�d|�|�d t j
d
d��|jd
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.cfg�w�utf-8)�encodingz
home = %s
�trueZfalsez"include-system-site-packages = %s
zversion = %d.%d.%d
N�z prompt = �
)rrrrZcfg_path�open�writer8rr5r:r
)rrr�fZinclrrr
r�s
zEnvBuilder.create_configuration�ntcCs�|j}|s�zRtj�|�s\|rPtj�|�tj�|�ks:t�t�tj�|�|�nt�||�Wn&tk
r�t �
d||�d}YnX|r�t�||�dS)�Y
Try symlinking a file, and if that fails, fall back to copying.
�Unable to symlink %r to %rTN)
rrrrrA�AssertionErrorr=�basename� Exception�logger�warningr!�copyfile)r�src�dst�relative_symlinks_okZ
force_copyrrr
�symlink_or_copy�s
zEnvBuilder.symlink_or_copycCs|tj�|�otj�|�}|jr�|s�tj�|�s�zH|rhtj�|�tj�|�ksRt�t�tj� |�|�nt�||�WdSt
k
r�t�d||�YnXtj�
tj� |��\}}tj�tj�t�dd||�}t�d�s�tj�|��sD|�d��r
d|}|dd�}|dk�rd }n|d
k�r(d}tj�tj�|�||�}n|}tj�|��sl|�sht�d|�dSt�||�dS)
rMNrN�scriptsrLTZ_d����pythonZvenvlauncherZpythonwZ
venvwlauncherzUnable to copy %r)rr�lexistsr$rrrArOr=rPrQrRrS�splitextr�__file__� sysconfig�is_python_buildr�endswithr!rT)rrUrVrWZbad_srcrPZextZsrcfnrrr
rX�s>�
cCs�|j}|j}|j}|j}tjdkr�||j|�tj�|�sFt� |d�dD]F}tj�
||�}tj�|�sJ||j|dd�tj�|�sJt� |d�qJ�n|jr�dd�t�
|�D�}t�d�r�dd�|D�}nd d
ddg}|D]2}tj�
||�}tj�|�r�||tj�
||��q�t�d��r�t�|j�D]z\} }
}d
|k�r&tj�| �}tj�
|jd|�}tj�|��spt�|�tj�
| d
�}tj�
|d
�}
t�||
��q��q&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|]$}tj�tj�|�d�dkr|�qS)r))�.exez.dll)rr�normcaser]��.0rKrrr
�
<listcomp>�s�z+EnvBuilder.setup_python.<locals>.<listcomp>cSs"g|]}tj�|��d�r|�qS))r[Z vcruntime)rrrc�
startswithrdrrr
rfs�z
python.exezpython_d.exezpythonw.exez
pythonw_d.exezinit.tclr*N)r>r@rXr8rr<r7rr�chmodrr$rrr_r`r\�walkrPrr%r!rT)rrrBrZcopierrA�suffix�suffixesrU�root�dirs�filesZtcldirrVrrr
r�sP
�
��
zEnvBuilder.setup_pythoncCs$|jddddg}tj|tjd�dS)z1Installs or upgrades pip in a virtual environmentz-ImZ ensurepip� --upgradez
--default-pip)�stderrN)r@�
subprocessZcheck_outputZSTDOUT)rr�cmdrrr
rs
�zEnvBuilder._setup_pipcCs2tj�tj�t��}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^r�install_scripts)rrrrrr
r#szEnvBuilder.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)rrrrr
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�textrrrr
�replace_variables<szEnvBuilder.replace_variablescCs�|j}t|�}t�|�D�]�\}}}||krX|dd�D]}|dtjfkr8|�|�q8q|D�]H} tjdkr�| �d�r�| �d�r�q\tj� || �}
||d��
tj�dd�}|s�|}ntjj |f|��}tj�|�s�t�
|�tj� || �}
t|
d��} | ��}W5QRX|
�d��srz$|�d�}|�||�}|�d�}Wn6tk
�rp}zd}t�d |
|�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.
N�commonrLr[)rbz.pdbr-�rbrDz+unable to copy script %r, may be binary: %s�wb)r>�lenrrir<rrgrarrr3�sepr$r%rI�read�decoderv�encode�UnicodeErrorrRrSrJr!Zcopymode)rrrrBZplenrlrmrnr'rKZsrcfilerjZdstdirZdstfile�data�errr
rsNsL
�
�zEnvBuilder.install_scripts)FFFFFN)F)F)�__name__�
__module__�__qualname__�__doc__rrr#rrrr<rXrrrrrvrsrrrr
rs(�
4
+<
rFcCs t|||||d�}|�|�dS)z,Create a virtual environment in a directory.)rrrr r
N)rr)rrrrr r
�builderrrr
r�s�rc Cs^d}tjdkrd}nttd�s"d}|s2td���n(ddl}|jtddd �}|jd
ddd
d�|jdddddd�tj dkr�d}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.)�progZdescriptionZepilogrmZENV_DIR�+z)A directory to create the environment in.)�metavar�nargs�helpz--system-site-packages�
store_true�system_sitezDGive the virtual environment access to the system site-packages dir.)�default�action�destr�rLz
--symlinksrz[Try to use symlinks rather than copies, when symlinks are not the default for the platform.z--copiesZstore_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))r�r�r�r�z--promptz;Provides an alternative prompt prefix for this environment.)r�z1you cannot supply --upgrade and --clear together.r)r5r:�hasattrr&�argparse�ArgumentParserr��add_argumentrr<Zadd_mutually_exclusive_group�
parse_argsrrrr�rr r
rmr) �argsZ
compatibler��parserZuse_symlinks�groupZoptionsr�r'rrr
�main�s|
�
��
�
�
�
���
�
r��__main__r)z Error: %s)�file)FFFFN)N)r�Zloggingrr!rqr5r_r1Z getLoggerr�rRrrr�ZrcrQr��printrp�exitrrrr
�<module>s2
q�
H$ |