ó
Ð²Ebc           @   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 d  d l m	 Z	 m
 Z
 m Z d  d l m Z d  d l m Z d  d l m Z m Z d  d l m Z e rñ d  d	 l m Z d  d
 l m Z m Z m Z d  d l m Z n  e  j e ƒ Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   Command(   t   ERRORt   SUCCESS(   t   Configurationt   get_configuration_filest   kinds(   t   PipError(   t
   indent_log(   t   get_progt   write_output(   t   MYPY_CHECK_RUNNING(   t   Values(   t   Anyt   Listt   Optional(   t   Kindt   ConfigurationCommandc           B   s˜   e  Z d  Z e Z d Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sh  
    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 on the to the user file by
    default.
    s  
        %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
    c      
   C   sº   |  j  j d d d d d d d  d d ƒ|  j  j d	 d d
 d d d t d d ƒ|  j  j d d d d d d t d d ƒ|  j  j d d d d d d t d d ƒ|  j j d |  j  ƒ d  S(   Ns   --editort   destt   editort   actiont   storet   defaultt   helps\   Editor to use to edit the file. Uses VISUAL or EDITOR environment variables if not provided.s   --globalt   global_filet
   store_trues+   Use the system-wide configuration file onlys   --usert	   user_files$   Use the user configuration file onlys   --sitet	   site_files3   Use the current environment configuration file onlyi    (   t   cmd_optst
   add_optiont   Nonet   Falset   parsert   insert_option_group(   t   self(    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyt   add_options4   s2    				c         C   s@  i |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6} | sY | d | k r| t j d d	 j t	 | ƒ ƒ ƒ t
 S| d } y |  j | d
 | d k ƒ} Wn( t k
 rÏ } t j | j d ƒ t
 SXt d | j d | ƒ |  _ |  j j ƒ  y | | | | d ƒ Wn( t k
 r;} t j | j d ƒ t
 SXt S(   Nt   listt   editt   gett   sett   unsett   debugi    s   Need an action (%s) to perform.s   , t
   need_valuet   isolatedt	   load_onlyi   (   R%   R&   R'   R$   (   t   list_valuest   open_in_editort   get_namet   set_name_valuet
   unset_namet   list_config_valuest   loggert   errort   joint   sortedR   t   _determine_fileR   t   argsR   t   isolated_modet   configurationt   loadR   (   R!   t   optionsR7   t   handlersR   R+   t   e(    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyt   run[   s8    





c         C   s½   g  t  j | j f t  j | j f t  j | j f f D] \ } } | r4 | ^ q4 } | s“ | sb d  St d „  t	 ƒ  t  j Dƒ ƒ r‰ t  j St  j Sn t
 | ƒ d k r­ | d St d ƒ ‚ d  S(   Nc         s   s!   |  ] } t  j j | ƒ Vq d  S(   N(   t   ost   patht   exists(   t   .0t   site_config_file(    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pys	   <genexpr>–   s   i   i    sL   Need exactly one file to operate upon (--user, --site, --global) to perform.(   R   t   USERR   t   GLOBALR   t   SITER   R   t   anyR   t   lenR   (   R!   R;   R)   t   keyt   valuet   file_options(    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR6   ‰   s"    
c         C   sP   |  j  | d d d ƒx3 t |  j j ƒ  ƒ D] \ } } t d | | ƒ q, Wd  S(   NR#   t   ni    s   %s=%r(   t   _get_n_argsR5   R9   t   itemsR	   (   R!   R;   R7   RI   RJ   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR,   ¤   s    "c         C   s;   |  j  | d d d ƒ} |  j j | ƒ } t d | ƒ d  S(   Ns
   get [name]RL   i   s   %s(   RM   R9   t	   get_valueR	   (   R!   R;   R7   RI   RJ   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR.   «   s    c         C   s?   |  j  | d d d ƒ\ } } |  j j | | ƒ |  j ƒ  d  S(   Ns   set [name] [value]RL   i   (   RM   R9   t	   set_valuet   _save_configuration(   R!   R;   R7   RI   RJ   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR/   ²   s    c         C   s6   |  j  | d d d ƒ} |  j j | ƒ |  j ƒ  d  S(   Ns   unset [name]RL   i   (   RM   R9   t   unset_valueRQ   (   R!   R;   R7   RI   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR0   ¹   s    c      
   C   s°   |  j  | d d d ƒ|  j ƒ  x‰ t |  j j ƒ  ƒ D]r \ } } t d | ƒ xV | D]N } t ƒ  = t j j	 | ƒ } t d | | ƒ | rž |  j
 | ƒ n  Wd QXqV Wq6 Wd S(   s9   List config key-value pairs across different config filesR(   RL   i    s   %s:s   %s, exists: %rN(   RM   t   print_env_var_valuesR5   R9   t   iter_config_filesR	   R   R?   R@   RA   t   print_config_file_values(   R!   R;   R7   t   variantt   filest   fnamet   file_exists(    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR1   À   s    
"

c      	   C   sM   xF |  j  j | ƒ j ƒ  D], \ } } t ƒ   t d | | ƒ Wd QXq Wd S(   s.   Get key-value pairs from the file of a variants   %s: %sN(   R9   t   get_values_in_configRN   R   R	   (   R!   RV   t   nameRJ   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyRU   Ò   s    
c      	   C   sl   t  d d ƒ t ƒ  P xH t |  j j ƒ  ƒ D]1 \ } } d j | j ƒ  ƒ } t  d | | ƒ q- WWd QXd S(   s5   Get key-values pairs present as environment variabless   %s:t   env_vars   PIP_{}s   %s=%rN(   R	   R   R5   R9   t   get_environ_varst   formatt   upper(   R!   RI   RJ   R\   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyRS   Ú   s
    
"c         C   s…   |  j  | ƒ } |  j j ƒ  } | d  k r9 t d ƒ ‚ n  y t j | | g ƒ Wn. t j k
 r€ } t d j | j	 ƒ ƒ ‚ n Xd  S(   Ns%   Could not determine appropriate file.s*   Editor Subprocess exited with exit code {}(
   t   _determine_editorR9   t   get_file_to_editR   R   t
   subprocesst
   check_callt   CalledProcessErrorR^   t
   returncode(   R!   R;   R7   R   RX   R=   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR-   ã   s    c         C   sU   t  | ƒ | k r9 d j | t ƒ  | ƒ } t | ƒ ‚ n  | d k rM | d S| Sd S(   sJ   Helper to make sure the command got the right number of arguments
        sJ   Got unexpected number of arguments, expected {}. (example: "{} config {}")i   i    N(   RH   R^   R   R   (   R!   R7   t   exampleRL   t   msg(    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyRM   ó   s    c         C   sB   y |  j  j ƒ  Wn* t k
 r= t j d ƒ t d ƒ ‚ n Xd  S(   Ns:   Unable to save configuration. Please report this as a bug.s   Internal Error.(   R9   t   savet	   ExceptionR2   t	   exceptionR   (   R!   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyRQ     s    c         C   sZ   | j  d  k	 r | j  Sd t j k r0 t j d Sd t j k rJ t j d St d ƒ ‚ d  S(   Nt   VISUALt   EDITORs"   Could not determine editor to use.(   R   R   R?   t   environR   (   R!   R;   (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR`     s    (   t   __name__t
   __module__t   __doc__t   Truet   ignore_require_venvt   usageR"   R>   R6   R,   R.   R/   R0   R1   RU   RS   R-   RM   RQ   R`   (    (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyR      s"   		'	.												(    t   loggingR?   Rb   t   pip._internal.cli.base_commandR    t   pip._internal.cli.status_codesR   R   t   pip._internal.configurationR   R   R   t   pip._internal.exceptionsR   t   pip._internal.utils.loggingR   t   pip._internal.utils.miscR   R	   t   pip._internal.utils.typingR
   t   optparseR   t   typingR   R   R   R   t	   getLoggerRn   R2   R   (    (    (    sA   /tmp/pip-build-UPPWic/pip/pip/_internal/commands/configuration.pyt   <module>   s   