3
ûâdeð"  ã               @   s²   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZmZ e jeƒZG d
d„ de
ƒZdS )é    N)ÚValues)ÚAnyÚListÚOptional)ÚCommand)ÚERRORÚSUCCESS)ÚConfigurationÚKindÚget_configuration_filesÚkinds)ÚPipError)Ú
indent_log)Úget_progÚwrite_outputc               @   s*  e Zd ZdZdZdZddœdd„Zeee	 e
dœd	d
„Zeeee dœdd„Zeee	 ddœdd„Zeee	 ddœdd„Zeee	 ddœdd„Zeee	 ddœdd„Zeee	 ddœdd„Zeddœdd„Zddœdd„Zeee	 ddœdd„Zee	 e	e
edœd d!„Zddœd"d#„Zee	d$œd%d&„ZdS )'ÚConfigurationCommandaa  
    Manage local and global configuration.

    Subcommands:

    - list: List the active configuration (or from the file specified)
    - edit: Edit the configuration file in an editor
    - get: Get the value associated with name
    - set: Set the name=value
    - unset: Unset the value associated with name
    - debug: List the configuration files and values defined under them

    If none of --user, --global and --site are passed, a virtual
    environment configuration file is used if one is active and the file
    exists. Otherwise, all modifications happen to the user file by
    default.
    Ta  
        %prog [<file-option>] list
        %prog [<file-option>] [--editor <editor-path>] edit

        %prog [<file-option>] get name
        %prog [<file-option>] set name value
        %prog [<file-option>] unset name
        %prog [<file-option>] debug
    N)Úreturnc             C   sl   | j jdddd dd� | j jdddd	d
d� | j jdddd	dd� | j jdddd	dd� | jjd| j ƒ d S )Nz--editorÚeditorÚstorez\Editor to use to edit the file. Uses VISUAL or EDITOR environment variables if not provided.)ÚdestÚactionÚdefaultÚhelpz--globalÚglobal_fileÚ
store_trueFz+Use the system-wide configuration file onlyz--userÚ	user_filez$Use the user configuration file onlyz--siteÚ	site_filez3Use the current environment configuration file onlyr   )Úcmd_optsÚ
add_optionÚparserÚinsert_option_group)Úself© r"   úC/tmp/pip-build-red6nood/pip/pip/_internal/commands/configuration.pyÚadd_options4   s2    z ConfigurationCommand.add_options)ÚoptionsÚargsr   c             C   s  | j | j| j| j| j| jdœ}| s0|d |krJtjddjt	|ƒƒƒ t
S |d }y| j||dkd	�}W n0 tk
r˜ } ztj|jd ƒ t
S d }~X nX t|j|d
�| _| jjƒ  y|| ||dd … ƒ W n2 tk
�r  } ztj|jd ƒ t
S d }~X nX tS )N)ÚlistÚeditÚgetÚsetÚunsetÚdebugr   zNeed an action (%s) to perform.z, r)   r*   r+   r(   )Ú
need_value)ÚisolatedÚ	load_onlyé   )r)   r*   r+   r(   )Úlist_valuesÚopen_in_editorÚget_nameÚset_name_valueÚ
unset_nameÚlist_config_valuesÚloggerÚerrorÚjoinÚsortedr   Ú_determine_filer   r&   r	   Úisolated_modeÚconfigurationÚloadr   )r!   r%   r&   Úhandlersr   r/   Úer"   r"   r#   ÚrunZ   s6    

zConfigurationCommand.run)r%   r-   r   c             C   s€   dd„ t j|jft j|jft j|jffD ƒ}|s`|s8d S tdd„ tƒ t j D ƒƒrXt jS t jS nt	|ƒdkrt|d S t
dƒ‚d S )Nc             S   s   g | ]\}}|r|‘qS r"   r"   )Ú.0ÚkeyÚvaluer"   r"   r#   ú
<listcomp>‰   s   z8ConfigurationCommand._determine_file.<locals>.<listcomp>c             s   s   | ]}t jj|ƒV  qd S )N)ÚosÚpathÚexists)rB   Zsite_config_filer"   r"   r#   ú	<genexpr>—   s   z7ConfigurationCommand._determine_file.<locals>.<genexpr>r0   r   zLNeed exactly one file to operate upon (--user, --site, --global) to perform.)r   ÚUSERr   ÚGLOBALr   ZSITEr   Úanyr   Úlenr   )r!   r%   r-   Zfile_optionsr"   r"   r#   r;   ‡   s     

z$ConfigurationCommand._determine_filec             C   s<   | j |ddd� x&t| jjƒ ƒD ]\}}td||ƒ q W d S )Nr'   r   )Únz%s=%r)Ú_get_n_argsr:   r=   Úitemsr   )r!   r%   r&   rC   rD   r"   r"   r#   r1   ¥   s    z ConfigurationCommand.list_valuesc             C   s*   | j |ddd�}| jj|ƒ}td|ƒ d S )Nz
get [name]r0   )rN   z%s)rO   r=   Ú	get_valuer   )r!   r%   r&   rC   rD   r"   r"   r#   r3   «   s    zConfigurationCommand.get_namec             C   s.   | j |ddd�\}}| jj||ƒ | jƒ  d S )Nzset [name] [value]é   )rN   )rO   r=   Z	set_valueÚ_save_configuration)r!   r%   r&   rC   rD   r"   r"   r#   r4   ±   s    z#ConfigurationCommand.set_name_valuec             C   s(   | j |ddd�}| jj|ƒ | jƒ  d S )Nzunset [name]r0   )rN   )rO   r=   Zunset_valuerS   )r!   r%   r&   rC   r"   r"   r#   r5   ·   s    zConfigurationCommand.unset_namec             C   sˆ   | j |ddd� | jƒ  xjt| jjƒ ƒD ]X\}}td|ƒ xD|D ]<}tƒ �, tjj	|ƒ}td||ƒ |rr| j
|ƒ W dQ R X q@W q(W dS )z9List config key-value pairs across different config filesr,   r   )rN   z%s:z%s, exists: %rN)rO   Úprint_env_var_valuesr:   r=   Ziter_config_filesr   r   rF   rG   rH   Úprint_config_file_values)r!   r%   r&   ÚvariantÚfilesÚfnameZfile_existsr"   r"   r#   r6   ½   s    

z'ConfigurationCommand.list_config_values)rV   r   c             C   s@   x:| j j|ƒjƒ D ]&\}}tƒ � td||ƒ W dQ R X qW dS )z.Get key-value pairs from the file of a variantz%s: %sN)r=   Zget_values_in_configrP   r   r   )r!   rV   ÚnamerD   r"   r"   r#   rU   Í   s    z-ConfigurationCommand.print_config_file_valuesc             C   sV   t ddƒ tƒ �< x4t| jjƒ ƒD ]"\}}d|jƒ › �}t d||ƒ q"W W dQ R X dS )z5Get key-values pairs present as environment variablesz%s:Úenv_varZPIP_z%s=%rN)r   r   r:   r=   Úget_environ_varsÚupper)r!   rC   rD   rZ   r"   r"   r#   rT   Ó   s
    
z)ConfigurationCommand.print_env_var_valuesc             C   sp   | j |ƒ}| jjƒ }|d kr$tdƒ‚ytj||gƒ W n4 tjk
rj } ztdj|jƒƒ‚W Y d d }~X nX d S )Nz%Could not determine appropriate file.z*Editor Subprocess exited with exit code {})	Ú_determine_editorr=   Zget_file_to_editr   Ú
subprocessÚ
check_callÚCalledProcessErrorÚformatÚ
returncode)r!   r%   r&   r   rX   r@   r"   r"   r#   r2   Û   s    

z#ConfigurationCommand.open_in_editor)r&   ÚexamplerN   r   c             C   s<   t |ƒ|kr$dj|tƒ |ƒ}t|ƒ‚|dkr4|d S |S dS )zAHelper to make sure the command got the right number of argumentszJGot unexpected number of arguments, expected {}. (example: "{} config {}")r0   r   N)rM   ra   r   r   )r!   r&   rc   rN   Úmsgr"   r"   r#   rO   é   s    z ConfigurationCommand._get_n_argsc             C   s:   y| j jƒ  W n& tk
r4   tjdƒ tdƒ‚Y nX d S )Nz:Unable to save configuration. Please report this as a bug.zInternal Error.)r=   ÚsaveÚ	Exceptionr7   Ú	exceptionr   )r!   r"   r"   r#   rS   ÷   s    z(ConfigurationCommand._save_configuration)r%   r   c             C   sD   |j d k	r|j S dtjkr$tjd S dtjkr8tjd S tdƒ‚d S )NZVISUALZEDITORz"Could not determine editor to use.)r   rF   Úenvironr   )r!   r%   r"   r"   r#   r]     s    




z&ConfigurationCommand._determine_editor)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zignore_require_venvÚusager$   r   r   ÚstrÚintrA   Úboolr   r
   r;   r1   r3   r4   r5   r6   rU   rT   r2   r   rO   rS   r]   r"   r"   r"   r#   r      s"   	&-r   )ÚloggingrF   r^   Úoptparser   Útypingr   r   r   Zpip._internal.cli.base_commandr   Zpip._internal.cli.status_codesr   r   Zpip._internal.configurationr	   r
   r   r   Zpip._internal.exceptionsr   Zpip._internal.utils.loggingr   Zpip._internal.utils.miscr   r   Ú	getLoggerri   r7   r   r"   r"   r"   r#   Ú<module>   s   
