B
    È S]S  ã            
   @   sÆ   d Z ddlZddlZddlZddlZddlZddlZddlZe e	¡Z
G dd„ dƒZddd„Zddd	„Ze	d
krÂdZyeƒ  dZW n4 ek
r¶ Z zede ejd W ddZ[X Y nX e e¡ dS )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Š   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Ze	j
dkrLddd„Zn
d dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!Ú
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.
    FNc             C   s(   || _ || _|| _|| _|| _|| _d S )N)Úsystem_site_packagesÚclearÚsymlinksÚupgradeÚwith_pipÚprompt)Úselfr   r   r   r   r   r   © r
   úlib/python3.7/venv/__init__.pyÚ__init__+   s    zEnvBuilder.__init__c             C   sx   t j |¡}|  |¡}| j}d| _|  |¡ |  |¡ | jrF|  |¡ | j	s`|  
|¡ |  |¡ |rtd| _|  |¡ dS )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ÚcontextZtrue_system_site_packagesr
   r
   r   Úcreate4   s    





zEnvBuilder.createc             C   s`   xZt  |¡D ]L}t j ||¡}t j |¡s6t j |¡rBt  |¡ qt j |¡rt 	|¡ qW d S )N)
r   Úlistdirr   ÚjoinÚislinkÚisfileÚremoveÚisdirÚshutilZrmtree)r	   r   Úfnr
   r
   r   Úclear_directoryN   s    zEnvBuilder.clear_directoryc             C   sœ  dd„ }t j |¡r$| jr$|  |¡ t ¡ }||_t j |¡d |_	| j
dk	rT| j
n|j	}d| |_
||ƒ t j}ttdtjƒ}t j t j |¡¡\}}||_||_||_tjdkrÎd}	d	}
t j |d
d¡}n(d}	d}
t j |ddtjdd…  d¡}t j ||
¡ |_}||ƒ ||ƒ tjdkrft jdkrftjdkrft j |d¡}t j |¡sft  d|¡ t j ||	¡ |_}|	|_t j ||¡|_||ƒ |S )zª
        Create the directories for the environment.

        Returns a context object which holds paths in the environment,
        for use by subsequent logic.
        c             S   s@   t j | ¡st  | ¡ n$t j | ¡s0t j | ¡r<td|  ƒ‚d S )NzUnable to create directory %r)r   r   ÚexistsÚmakedirsr   r   Ú
ValueError)Údr
   r
   r   Úcreate_if_needed^   s    z7EnvBuilder.ensure_directories.<locals>.create_if_neededé   Nz(%s) Ú_base_executableZwin32ZScriptsZIncludeÚLibzsite-packagesÚbinZincludeÚlibzpython%d.%dé   l        ÚposixÚdarwinZlib64)r   r   r"   r   r!   ÚtypesÚSimpleNamespacer   ÚsplitÚenv_namer   ÚenvironÚgetattrÚsysÚ
executabler   Ú
python_dirZ
python_exeÚplatformr   Úversion_infoZinc_pathÚmaxsizeÚnameÚsymlinkÚbin_pathÚbin_nameÚenv_exe)r	   r   r&   r   r   Úenvr6   ÚdirnameZexenameZbinnameZincpathZlibpathr   Z	link_pathÚbinpathr
   r
   r   r   V   sJ    



zEnvBuilder.ensure_directoriesc          	   C   sz   t j |jd¡ |_}t|dddL}| d|j ¡ | jrBd}nd}| d| ¡ | d	t	j
d
d…  ¡ W d
Q R X d
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Úwzutf-8)Úencodingz
home = %s
ÚtrueZfalsez"include-system-site-packages = %s
zversion = %d.%d.%d
Né   )r   r   r   r   Zcfg_pathÚopenÚwriter7   r   r5   r9   )r	   r   r   ÚfZinclr
   r
   r   r   ‹   s    	zEnvBuilder.create_configurationÚntc             C   sš   | j  }|s†yRtj |¡s\|rPtj |¡tj |¡ks:t‚t tj |¡|¡ nt ||¡ W n& tk
r„   t	 
d||¡ d}Y nX |r–t ||¡ dS )zY
            Try symlinking a file, and if that fails, fall back to copying.
            zUnable to symlink %r to %rTN)r   r   r   r   rA   ÚAssertionErrorr<   ÚbasenameÚ	ExceptionÚloggerÚwarningr   Úcopyfile)r	   ÚsrcÚdstÚrelative_symlinks_okZ
force_copyr
   r
   r   Úsymlink_or_copyŸ   s    
zEnvBuilder.symlink_or_copyc       	      C   s´  t j |¡ot j |¡ }| jrš|sšt j |¡sšyF|rht j |¡t j |¡ksRt‚t  t j 	|¡|¡ nt  ||¡ dS  t
k
r˜   t d||¡ Y nX t j t j 	|¡¡\}}t j t j t¡dd|| ¡}t d¡sèt j |¡sB| d¡rd| }|dd… }|dkrd	}n|d
kr&d}t j t j |¡|| ¡}n>| d¡rVtj}n*t j t j t¡dd¡}t j ||| ¡}t j |¡s¤|s t d|¡ dS t ||¡ dS )zY
            Try symlinking a file, and if that fails, fall back to copying.
            NzUnable to symlink %r to %rÚscriptsrJ   TZ_déþÿÿÿÚpythonZvenvlauncherZpythonwZvenvwlauncherzUnable to copy %r)r   r   Úlexistsr"   r   r   rA   rK   r<   rL   rM   rN   rO   Úsplitextr   Ú__file__Ú	sysconfigÚis_python_buildr   ÚendswithÚ
startswithr5   Úprefixr   rP   )	r	   rQ   rR   rS   Zbad_srcrL   ZextZsrcfnrU   r
   r
   r   rT   ²   sB    


c             C   s®  |j }|j}| j}||j|ƒ |j}tjdkrštj |¡sFt 	|d¡ xNdD ]F}tj 
||¡}tj |¡sL||j|dd tj |¡sLt 	|d¡ qLW n| jrÎdd„ t |¡D ƒ}t d¡rÚdd„ |D ƒ}nd	d
ddg}x:|D ]2}tj 
||¡}tj |¡rà||tj 
||¡ƒ qàW t d¡rªx†t |j¡D ]v\}	}
}d|kr0tj |	¡}tj 
|jd|¡}tj |¡szt |¡ tj 
|	d¡}tj 
|d¡}t ||¡ P q0W dS )z·
        Set up a Python executable in the environment.

        :param context: The information for the environment creation request
                        being processed.
        rJ   ií  )rW   Zpython3T)rS   c             S   s,   g | ]$}t j t j |¡d  ¡dkr|‘qS )r'   )z.exez.dll)r   r   ÚnormcaserY   )Ú.0rI   r
   r
   r   ú
<listcomp>   s    z+EnvBuilder.setup_python.<locals>.<listcomp>c             S   s"   g | ]}t j |¡ d ¡r|‘qS ))rW   Z	vcruntime)r   r   r`   r^   )ra   rI   r
   r
   r   rb     s    z
python.exezpython_d.exezpythonw.exezpythonw_d.exezinit.tclr)   N)r=   r?   rT   r6   r7   r   r;   r   r   Úchmodr   r"   r   r   r[   r\   rX   ÚwalkrL   r   r#   r   rP   )r	   r   rB   r   ZcopierrA   ÚsuffixÚsuffixesrQ   ÚrootÚdirsÚfilesZtcldirrR   r
   r
   r   r   â   sF    





zEnvBuilder.setup_pythonc             C   s$   |j ddddg}tj|tjd dS )z1Installs or upgrades pip in a virtual environmentz-ImZ	ensurepipz	--upgradez--default-pip)ÚstderrN)r?   Ú
subprocessZcheck_outputZSTDOUT)r	   r   Úcmdr
   r
   r   r     s    
zEnvBuilder._setup_pipc             C   s2   t j t j t¡¡}t j |d¡}|  ||¡ dS )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.
        rU   N)r   r   r   rA   rZ   r   Úinstall_scripts)r	   r   r   r
   r
   r   r   '  s    zEnvBuilder.setup_scriptsc             C   s   dS )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	   r   r
   r
   r   r   6  s    zEnvBuilder.post_setupc             C   sJ   |  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__)Úreplacer   r2   r   r>   r?   )r	   Útextr   r
   r
   r   Úreplace_variables@  s    zEnvBuilder.replace_variablesc             C   s¾  |j }t|ƒ}x¨t |¡D ]˜\}}}||kr`x,|dd… D ]}|dtjfkr>| |¡ q>W qxR|D ]H}	tjdkrŽ|	 d¡rŽ|	 d¡rŽqhtj 	||	¡}
||d…  
tj¡dd… }|sÂ|}ntjj	|f|žŽ }tj |¡sêt |¡ tj 	||	¡}t|
dƒ}	|	 ¡ }W dQ R X |
 d¡s~y$| d¡}|  ||¡}| d¡}W n6 tk
r| } zd}t d	|
|¡ W dd}~X Y nX |dk	rht|d
ƒ}	|	 |¡ W dQ R X t |
|¡ qhW qW dS )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ÚcommonrJ   rW   )z.exez.pdbr,   Úrbzutf-8z+unable to copy script %r, may be binary: %sÚwb)r=   Úlenr   rd   r;   r   r^   r]   r   r   r1   Úsepr"   r#   rG   ÚreadÚdecoderp   ÚencodeÚUnicodeErrorrN   rO   rH   r   Zcopymode)r	   r   r   rB   Úplenrg   rh   ri   r%   rI   Zsrcfilere   ZdstdirZdstfileÚdataÚer
   r
   r   rm   R  sF    


zEnvBuilder.install_scripts)FFFFFN)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r   r   r   r;   rT   r   r   r   r   rp   rm   r
   r
   r
   r   r      s     
5

0<	
r   Fc             C   s    t |||||d}| | ¡ dS )z,Create a virtual environment in a directory.)r   r   r   r   r   N)r   r   )r   r   r   r   r   r   Úbuilderr
   r
   r   r   …  s    r   c       	      C   sb  d}t jdk rd}ntt dƒs"d}|s2tdƒ‚n,dd l}|jtddd	}|jd
dddd |jdddddd tj	dkr€d}nd}| 
¡ }|jd|dddd |jd| dddd |jdddddd |jddddd d |jd!d"ddd#d$ |jd%d&d' | | ¡}|jr"|jr"td(ƒ‚t|j|j|j|j|j|jd)}x|jD ]}| |¡ qJW d S )*NT)rF   rF   FÚbase_prefixz.This script is only for use with Python >= 3.3r   zFCreates 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descriptionZepilogrh   ZENV_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‡   rJ   z
--symlinksr   z[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--clearr   zcDelete the contents of the environment directory if it already exists, before environment creation.z	--upgrader   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))rŒ   rŠ   r‹   r‡   z--promptz;Provides an alternative prompt prefix for this environment.)r‡   z1you cannot supply --upgrade and --clear together.)r   r   r   r   r   r   )r5   r9   Úhasattrr$   ÚargparseÚArgumentParserr}   Úadd_argumentr   r;   Zadd_mutually_exclusive_groupÚ
parse_argsr   r   r   r‰   r   r   r   rh   r   )	ÚargsZ
compatiblerŽ   ÚparserZuse_symlinksÚgroupZoptionsr   r%   r
   r
   r   Úmain  s\    










r•   Ú__main__r'   z	Error: %s)Úfile)FFFFN)N)r€   Zloggingr   r   rk   r5   r[   r/   Z	getLoggerr}   rN   r   r   r•   ZrcrM   r|   Úprintrj   Úexitr
   r
   r
   r   Ú<module>   s,   
  u 

H$