U
    |e.!                     @  s   d dl 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 ddlmZ eejejZed Z G dd dZ!G dd dedZ"dd Z#ddgZ$dS )    )annotationsN)ABCMetaabstractmethod)ArgumentTypeError)literal_eval)OrderedDict)Path)LogCmd)safe_delete)run_cmd)__version__   )PyEnvCfgzdebug.pyc                   @  s   e Zd ZddddZdS )CreatorMetaNonereturnc                 C  s
   d | _ d S N)errorself r   =/tmp/pip-unpacked-wheel-tuxcr54r/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ddZdd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edd ZdS )CreatorzFA class that given a python Interpreter creates a virtual environment.r   r   c                 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   Zfrom_folder	pyenv_cfgapp_dataenv)r   optionsr   r   r   r   r       s    zCreator.__init__strc                 C  s(   | j j dddd |  D  dS )N(z, c                 s  s    | ]\}}| d | V  qdS )=Nr   ).0kvr   r   r   	<genexpr>1   s     z#Creator.__repr__.<locals>.<genexpr>))	__class__r   join_argsr   r   r   r   __repr__0   s    zCreator.__repr__c                 C  s    dt | jfd| jfd| jfgS )Nr    r!   r"   )r'   r    r!   r"   r   r   r   r   r1   3   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_create:   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    actionr5   defaultz--no-vcs-ignorer"   z>don't create VCS ignore directive in the destination directoryN)add_argumentvalidate_dest)r3   parserr   metar$   r   r   r   add_parser_argumentsE   s&    
zCreator.add_parser_argumentsc                 C  s   t dS )z)Perform the virtual environment creation.N)NotImplementedErrorr   r   r   r   createc   s    zCreator.createc              	   C  s  dd }t  }t }|dkr&ddini }t|D ]N}z,|j|f||}||krXW q2t|W q2 tk
r~   d||< Y q2X q2|rd| }d| d	|d
|}	t	|	t
j|krd|dt
j d}	t	|	t|}
|
 r|
 rd|
 d}	t	|	tt
jt|
 }|}
|r| rVt
t|t
jrLq|||
 |j|j }}||krx|||
 |}q$t|
S )zBNo path separator in the path, valid chars and must be write-able.c                 S  s:   t tj|j| jg }d| | d| }t|d S )Nthe destination z is not write-able at )r   ospathcommonprefixpartsrelative_tor   )r    valuecommonmsgr   r   r   non_write_ablel   s    z-Creator.validate_dest.<locals>.non_write_ablembcserrorsignoreN zthe file system codec (z) cannot handle characters z within zdestination z& must not contain the path separator (z,) as this would break the activation scriptsrA   z already exists and is a file)sysgetfilesystemencodingr   r'   encodedecode
ValueErrorr0   keysr   rB   pathsepr   existsis_filerC   abspathresolveaccessW_OKparentname)r3   Z	raw_valuerJ   encodingZrefusedkwargscharZtripbadrI   rG   r    base_r   r   r   r;   h   sH    




zCreator.validate_destc                 C  sJ   | j  r(| jr(td| j  t| j  |   |   | jsF| 	  d S )Nz	delete %s)
r    rV   r!   loggingdebugr
   r@   set_pyenv_cfgr"   setup_ignore_vcsr   r   r   r   run   s    
zCreator.runc                 C  sb   t  | j_tj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   )r'   )r*   ir   r   r   r-      s     z(Creator.set_pyenv_cfg.<locals>.<genexpr>version_info
virtualenv)r   r#   contentrB   rC   dirnamerX   r   Zsystem_executablerj   r0   rm   r   r   r   r   r   rf      s
    
zCreator.set_pyenv_cfgc                 C  s$   | j d }| s |jddd dS )z9Generate ignore instructions for version control systems.z
.gitignorez(# created by virtualenv automatically
*
zutf-8)r^   N)r    rV   
write_text)r   Z
git_ignorer   r   r   rg      s    
zCreator.setup_ignore_vcsc                 C  s4   | j dkr.| jdk	r.t| j|  | j| j| _ | j S )zb:return: debug information about the virtual environment (only valid after :meth:`create` has run)N)r   Zexeget_env_debug_infodebug_scriptr$   r%   r   r   r   r   re      s    zCreator.debugc                   C  s   t S r   )DEBUG_SCRIPTr   r   r   r   rs      s    zCreator.debug_scriptN)r   r   r   __doc__r   r2   r1   classmethodr4   r>   r   r@   r;   rh   rf   rg   propertyre   staticmethodrs   r   r   r   r   r      s&   




1	
r   )	metaclassc           
   
   C  s  |  }|dd  ||4}t| t|g}tdt| t|\}}}W 5 Q R X zR|dkr|rpt|}q|dkrd|krt	|t
|n
t|}|r||d< W n8 t
k
r }	 z|||t|	d W Y S d }	~	X Y nX d|kr
d	|d kr
|d d	 d= |S )
N
PYTHONPATHzdebug via %rr      fileerr)outr}   
returncode	exceptionrO   rC   )copypopZensure_extractedr'   rd   re   r	   r   r   OSError	Exceptionjsonloadsrepr)
Zenv_exers   r$   r%   cmdcoder~   r}   resultr   r   r   r   rr      s*    


(rr   )%
__future__r   r   rd   rB   rO   abcr   r   argparser   astr   collectionsr   pathlibr   Z#virtualenv.discovery.cached_py_infor	   Zvirtualenv.util.pathr
   Zvirtualenv.util.subprocessr   Zvirtualenv.versionr   r#   r   rC   rX   __file__r\   ZHERErt   r   r   rr   __all__r   r   r   r   <module>   s.    &