o
    $¼"bà"  ã                   @   s*  d dl mZmZmZ d dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZmZ d dlm Z  d dl!m"Z" ddl#m$Z$ eej% &e'¡ƒj(Z)e)d Z*G dd„ de+ƒZ,ee	ƒG dd„ de+ƒƒZ-dd„ Z.dS )é    )Úabsolute_importÚprint_functionÚunicode_literalsN)ÚABCMetaÚabstractmethod)ÚArgumentTypeError)Úliteral_eval)ÚOrderedDict)Údedent)Úadd_metaclass)ÚLogCmd)ÚWIN_CPYTHON_2)ÚPathÚsafe_delete)Ú
ensure_strÚensure_text)Úrun_cmd)Ú__version__é   )ÚPyEnvCfgzdebug.pyc                   @   s   e Zd Zdd„ ZdS )ÚCreatorMetac                 C   s
   d | _ d S ©N)Úerror©Úself© r   ú;/usr/lib/python3/dist-packages/virtualenv/create/creator.pyÚ__init__   s   
zCreatorMeta.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s    r   c                   @   sŒ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zed
d„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ ZdS )ÚCreatorzEA class that given a python Interpreter creates a virtual environmentc                 C   sJ   || _ d| _t|jƒ| _|j| _|j| _t | j¡| _|j	| _	|j
| _
dS )zØConstruct a new virtual environment creator.

        :param options: the CLI option as parsed from :meth:`add_parser_arguments`
        :param interpreter: the interpreter to create virtual environment from
        N)ÚinterpreterÚ_debugr   ÚdestÚclearÚno_vcs_ignorer   Úfrom_folderÚ	pyenv_cfgÚapp_dataÚenv)r   Úoptionsr"   r   r   r   r   %   s   zCreator.__init__c                 C   s   t |  ¡ ƒS r   )r   Ú__unicode__r   r   r   r   Ú__repr__4   s   zCreator.__repr__c                 C   s$   d  | jjd dd„ |  ¡ D ƒ¡¡S )Nz{}({})z, c                 s   s    � | ]\}}d   ||¡V  qdS )z{}={}N)Úformat)Ú.0ÚkÚvr   r   r   Ú	<genexpr>8   s   € z&Creator.__unicode__.<locals>.<genexpr>)r.   Ú	__class__r   ÚjoinÚ_argsr   r   r   r   r,   7   s   $zCreator.__unicode__c                 C   s$   dt t| jƒƒfd| jfd| jfgS )Nr$   r%   r&   )r   Ústrr$   r%   r&   r   r   r   r   r5   :   s   ýzCreator._argsc                 C   s   dS )a  Determine if we can create a virtual environment.

        :param interpreter: the interpreter in question
        :return: ``None`` if we can't create, any other object otherwise that will be forwarded to                   :meth:`add_parser_arguments`
        Tr   )Úclsr"   r   r   r   Ú
can_createA   s   zCreator.can_createc                 C   s>   |j dd| jd� |j dddddd	� |j d
ddddd	� dS )a  Add CLI arguments for the creator.

        :param parser: the CLI parser
        :param app_data: the application data folder
        :param interpreter: the interpreter we're asked to create virtual environment for
        :param meta: value as returned by :meth:`can_create`
        r$   z!directory to create virtualenv at)ÚhelpÚtypez--clearr%   Ú
store_truezZremove the destination directory if exist before starting (will overwrite files otherwise)F)r$   Úactionr9   Údefaultz--no-vcs-ignorer&   z>don't create VCS ignore directive in the destination directoryN)Úadd_argumentÚvalidate_dest)r7   Úparserr"   Úmetar)   r   r   r   Úadd_parser_argumentsK   s&   	ýû
ûzCreator.add_parser_argumentsc                 C   s   t ‚)z)Perform the virtual environment creation.)ÚNotImplementedErrorr   r   r   r   Úcreateh   s   zCreator.createc              	   C   sŽ  dd„ }t  ¡ }tƒ }|dkrddini }t|ƒD ]%}z|j|fi |¤Ž |¡}||kr.W qt|ƒ‚ ty>   d||< Y qw |rOtd |d 	| 
¡ ¡|¡ƒ‚tj|v r]td	 |tj¡ƒ‚t|ƒ}| ¡ rp| ¡ rptd
 |¡ƒ‚dt j  kr{dkr†n n	ttj |¡ƒ}	nttj t|ƒ¡ƒ ¡ }	|	}|	rÃ|	 ¡ r¯t tt|	ƒƒtj¡rª	 t|ƒS ||	|ƒ |	j|	j}
}|
|	kr¿||	|ƒ |
}	|	s–t|ƒS )zANo path separator in the path, valid chars and must be write-ablec                 S   s.   t tj |j| jg¡Ž }td |  |¡|¡ƒ‚)Nz*the destination {} is not write-able at {})r   ÚosÚpathÚcommonprefixÚpartsr   r.   Úrelative_to)r$   ÚvalueÚcommonr   r   r   Únon_write_ableq   s   ÿz-Creator.validate_dest.<locals>.non_write_ableÚmbcsÚerrorsÚignoreNzDthe file system codec ({}) cannot handle characters {!r} within {!r}Ú zddestination {!r} must not contain the path separator ({}) as this would break the activation scriptsz/the destination {} already exists and is a file)é   rQ   )rQ   é   )ÚsysÚgetfilesystemencodingr	   r   ÚencodeÚdecodeÚ
ValueErrorr   r.   r4   ÚkeysrE   Úpathsepr   ÚexistsÚis_fileÚversion_inforF   ÚrealpathÚabspathr6   ÚresolveÚaccessÚW_OKÚparentÚname)r7   Ú	raw_valuerL   ÚencodingÚrefusedÚkwargsÚcharÚtriprJ   r$   ÚbaseÚ_r   r   r   r?   m   sZ   ÿýÿ

þ
û
÷
zCreator.validate_destc                 C   sN   | j  ¡ r| jrt d| j ¡ t| j ƒ |  ¡  |  ¡  | js%|  	¡  d S d S )Nz	delete %s)
r$   rZ   r%   ÚloggingÚdebugr   rD   Úset_pyenv_cfgr&   Úsetup_ignore_vcsr   r   r   r   Úrun§   s   
ÿzCreator.runc                 C   sR   t ƒ | j_| jj| jd< | jj| jd< d dd„ | jjD ƒ¡| jd< t| jd< d S )NÚhomeÚimplementationÚ.c                 s   s   � | ]}t |ƒV  qd S r   )r6   ©r/   Úir   r   r   r2   ´   s   € z(Creator.set_pyenv_cfg.<locals>.<genexpr>r\   Ú
virtualenv)	r	   r(   Úcontentr"   Úsystem_exec_prefixrr   r4   r\   r   r   r   r   r   rn   °   s
   
zCreator.set_pyenv_cfgc                 C   s,   | j d }| ¡ s| tdƒ ¡ ¡ dS dS )z9Generate ignore instructions for version control systems.z
.gitignoreze
                    # created by virtualenv automatically
                    *
                    N)r$   rZ   Ú
write_textr
   Úlstrip)r   Ú
git_ignorer   r   r   ro   ·   s   
ÿúÿzCreator.setup_ignore_vcsc                 C   s4   | j du r| jdurt| j|  ¡ | j| jƒ| _ | j S )zt
        :return: debug information about the virtual environment (only valid after :meth:`create` has run)
        N)r#   ÚexeÚget_env_debug_infoÚdebug_scriptr)   r*   r   r   r   r   rm   Ê   s   zCreator.debugc                 C   s   t S r   )ÚDEBUG_SCRIPTr   r   r   r   r~   Ô   s   zCreator.debug_scriptN)r   r   r    Ú__doc__r   r-   r,   r5   Úclassmethodr8   rB   r   rD   r?   rp   rn   ro   Úpropertyrm   r~   r   r   r   r   r!   !   s(    
	


9	
	r!   c           
   
   C   s  |  ¡ }| tdƒd ¡ | |¡�*}t| ƒt|ƒg}tr#dd„ |D ƒ}t tdƒt|ƒ¡ t|ƒ\}}}W d   ƒ n1 s>w   Y  z|dkrMt	|ƒ}nt
 |¡}|rX||d< W n tyt }	 z|||t|	ƒdœW  Y d }	~	S d }	~	ww d|v r†d	|d v r†|d d	 d= |S )
NÚ
PYTHONPATHc                 S   s   g | ]}t |ƒ‘qS r   )r   rt   r   r   r   Ú
<listcomp>ß   s    z&get_env_debug_info.<locals>.<listcomp>zdebug via %rr   Úerr)Úoutr…   Ú
returncodeÚ	exceptionrS   rF   )ÚcopyÚpopr6   Úensure_extractedr   rl   rm   r   r   r   ÚjsonÚloadsÚ	ExceptionÚrepr)
Úenv_exer~   r)   r*   ÚcmdÚcoder†   r…   Úresultrˆ   r   r   r   r}   Ø   s.   û

€€ÿr}   )/Ú
__future__r   r   r   rŒ   rl   rE   rS   Úabcr   r   Úargparser   Úastr   Úcollectionsr	   Útextwrapr
   Úsixr   Ú#virtualenv.discovery.cached_py_infor   Úvirtualenv.infor   Úvirtualenv.util.pathr   r   Úvirtualenv.util.sixr   r   Úvirtualenv.util.subprocessr   Úvirtualenv.versionr   r(   r   rF   r^   Ú__file__rb   ÚHEREr   Úobjectr   r!   r}   r   r   r   r   Ú<module>   s2     7