
%_[c           @   sA  d  Z  d d l m Z d d l m Z m Z 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 Z d d l Z d d l m Z m Z m Z m Z m 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 Z# e r;d d l$ Z$ n  e% a& d e' f d     YZ( d e' f d     YZ) d e) f d     YZ* d e) f d     YZ+ e j, d d k Z- e j, d d k Z. e/ Z0 e% d  Z1 d   Z2 d e f d     YZ3 d e f d     YZ4 d   Z5 d a6 d   Z7 d    Z8 e9 d! k r=e8   n  d S("   s   
Updated `conda-api` running on a Qt QProcess to avoid UI blocking.

This also add some extra methods to the original conda-api.
i(   t   deque(   t   abspatht   basenamet
   expandusert   isdirt   joinN(   t
   QByteArrayt   QObjectt   QProcesst   QTimert   Signal(   t   WINt   get_home_dir(   t   get_conda_info(   t   ensure_binary(   t   PIP_LIST_SCRIPT(   t   logger(   t   path_is_writables   1.3.0t   PipErrorc           B   s   e  Z d  Z RS(   s   General pip error.(   t   __name__t
   __module__t   __doc__(    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   4   s   t
   CondaErrorc           B   s   e  Z d  Z RS(   s   General Conda error.(   R   R   R   (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   :   s   t   CondaProcessWorkerc           B   s   e  Z d  Z RS(   s   General Conda error.(   R   R   R   (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   @   s   t   CondaEnvExistsErrorc           B   s   e  Z d  Z RS(   s!   Conda environment already exists.(   R   R   R   (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   F   s   i    t   2t   3c         C   sf   t  r, | d k r t |   St |  |  Sn6 | d k rB t |   St |  t  rU |  St |  |  Sd S(   s'   Convert `obj` to (unicode) text string.N(   t   PY2t   Nonet   unicodet   strt
   isinstance(   t   objt   encoding(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   to_text_stringS   s    

c         C   s.   t  |  t  r |  j   }  n  t |  d | S(   s    Qt/Python3 compatibility helper.R!   (   R   R   t   dataR"   (   R    R!   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   handle_qbytearrayf   s    t   ProcessWorkerc           B   s   e  Z d  Z e e e e  Z e e e e  Z e e e e  Z e e d
 d
 d
 d  Z
 d   Z d   Z d   Z d   Z d   Z d   Z d   Z d	   Z RS(   s5   Conda worker based on a QProcess for non blocking UI.c         C   s   t  t |   j   d |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 t
 |  _ t
 |  _ d |  _ | rq | n i  |  _ t   |  _ t   |  _ |  j |  |  j j d  |  j j j |  j  |  j j j |  j  d S(   s  Conda worker based on a QProcess for non blocking UI.

        Parameters
        ----------
        cmd_list : list of str
            Command line arguments to execute.
        parse : bool (optional)
            Parse json from output.
        pip : bool (optional)
            Define as a pip command.
        callback : func (optional)
            If the process has a callback to process output from comd_list.
        extra_kwargs : dict
            Arguments for the callback.
        i   N(   t   superR%   t   __init__R   t   _resultt	   _cmd_listt   _parset   _pipt   _condat	   _callbackt   Falset   _firedt   _communicate_firstt   _partial_stdoutt   _extra_kwargsR	   t   _timerR   t   _processt   _set_environmentt   setIntervalt   timeoutt   connectt   _communicatet   readyReadStandardOutputt   _partial(   t   selft   cmd_listt   parset   pipt   callbackt   extra_kwargst   environ(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR'   u   s"    				
				c         C   s5   d } t  r1 t t j j j    } d | } n  | S(   s$   Return the encoding/codepage to use.s   utf-8t   cp(   R   R   t   ctypest   cdllt   kernel32t   GetACP(   R<   t   encot   codepage(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_encoding   s
    c         C   sY   | rU |  j  j   } x* | j   D] \ } } | j | |  q" W|  j  j |  n  d S(   s$   Set the environment on the QProcess.N(   R4   t   processEnvironmentt   itemst   insertt   setProcessEnvironment(   R<   RB   t	   q_environt   kt   v(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR5      s
    c         C   s   |  j  j   } t | |  j    } y? g  | j d  D] } | r7 t j |  ^ q7 } | d } Wn t k
 r| | } n X|  j d k r | |  _ n |  j | 7_ |  j
 j |  | d  d S(   s   Callback for partial output.s    iN(   R4   t   readAllStandardOutputR$   RJ   t   splitt   jsont   loadst	   ExceptionR1   R   t   sig_partialt   emit(   R<   t
   raw_stdoutt   stdoutt   st   json_stdout(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;      s    1
c         C   sL   |  j  r/ |  j j   t j k r/ |  j   n |  j rH |  j j   n  d S(   s   Callback for communicate.N(	   R0   R4   t   stateR   t
   NotRunningt   communicateR/   R3   t   stop(   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR9      s
    
	c         C   s<  t  |  _ |  j j   |  j   } |  j d k rR |  j j   } t | |  } n	 |  j } |  j j	   } t | |  } | j
 |  | j
 |  g } t r | j   } n  d | j   k rS| j   r|  j ri d j |  j  d 6| d 6} t j d j |   qS| j   rS|  j rSi d j |  j  d 6} t j d d | qSn  d | d	 <|  j r| rg  } | j d
  }	 xu t |	  D]g \ }
 } | ry | j t j |   Wqt k
 r} t j d j |
 | t |    qXqqW| r| d	 } n  | | d	 f } | d } d | k s>d | k rt | t   sii t |  d 6d f } qd j d j |  j  | j! d d   } | | f } qn  |  j" r|  j" | d | |  j#  | d	 f } n  | |  _$ |  j% j& |  | d | d	  | d	 r/i | d	 d 6} t j d d | n  t  |  _' | S(   s   Retrieve information.s   using anacondat    t   commandt   stderrs"   Conda command output on stderr {0}s   Pip command output on stderrt   extrat    is    sD   Problem parsing conda json output. Line {0}. Data - {1}. Error - {2}i    t   exception_namet   exception_typet   errors   {0}: {1}t   messageN((   t   TrueR0   R4   t   waitForFinishedRJ   R1   R   RR   R$   t   readAllStandardErrort   encodeR   t   decodet   lowert   stripR,   R   R)   R   t   debugt   formatR+   R*   RS   t	   enumeratet   appendRT   RU   RV   t   warningR   R   t   dictt   getR-   R2   R(   t   sig_finishedRX   R/   (   R<   RH   RY   RZ   t
   raw_stderrRc   t   resultt   dR\   t   json_lines_outputt   it   lRh   t   out(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR_      sf    		 
!
$		
	c         C   s   |  j  j   d S(   s   Close the running process.N(   R4   t   close(   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   +  s    c         C   s   |  j  j   t j k o |  j S(   s.   Return True if worker has finished processing.(   R4   R]   R   R^   R/   (   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   is_finished/  s    c         C   sr   t  j t d j |  j    |  j sb d |  _ |  j j	 |  j d |  j d  |  j
 j	   n t d   d S(   s   Start process.Ra   i    i   s8   A Conda ProcessWorker can only run once per method call.N(   R   Rq   R   R   R)   R/   R   t   _partial_ouputR4   t   startR3   R   (   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   3  s    		!N(   R   R   R   R
   t   objectt   sig_chain_finishedRx   RW   R.   R   R'   RJ   R5   R;   R9   R_   R   R   R   (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR%   n   s"   '					Z		t	   _CondaAPIc           B   s  e  Z d  Z dB d  Z d   Z d   Z d   Z d   Z e	 e
 dB dB d  Z e	 dB dB d  Z e e   d   Z dB e
 d	  Z dB d
  Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z e	 d  Z dB d  Z d   Z e e
 d   Z d   Z e d    Z dB e	 d  Z e	 d  Z  dB dB dB d  Z! d   Z" dB dB d  Z# dB dB dB dB e
 d  Z$ dB dB dB e	 e
 e
 d  Z% dB dB dB e	 e
 e
 e
 d  Z& dB dB dB e
 e
 d  Z' dB dB d   Z( dB dB d!  Z) e d"    Z* dB dB e
 d#  Z+ d$   Z, dB dB e
 e	 d%  Z- dB dB e
 e	 d&  Z. dB dB e
 e	 d'  Z/ dB dB e
 e	 d(  Z0 dB dB e
 e	 d)  Z1 dB dB e
 e	 d*  Z2 dB dB e
 e	 d+  Z3 e dB e
 d,   Z4 dB e
 d-  Z5 dB e
 e
 e	 d.  Z6 dB dB e
 d/  Z7 dB dB dB dB e	 d0  Z8 dB dB e	 e	 d1  Z9 e	 d2  Z: dB dB dB e
 d3  Z; e d4    Z< dB dB d5  Z= e
 d6 dB d7  Z> dB dB dB dB d8  Z? dB dB d9  Z@ dB dB e	 d:  ZA dB d;  ZB dB dB dB d<  ZC dB d=  ZD e d>    ZE dB dB d?  ZF dB d@  ZG e dA    ZH RS(C   sA   Conda API to connect to conda in a non blocking way via QProcess.c         C   s   t  t |   j   | |  _ t   |  _ t   |  _ d |  _	 g  |  _
 d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ |  j d t    t t d   |  _ t |  j d  |  _ |  j j d  |  j j j |  j  d S(   sA   Conda API to connect to conda in a non blocking way via QProcess.t   infos
   ~/.condarcs   .condarci  N(   R&   R   R'   t   _parentR    t   _queueR	   R3   R   t   _current_workert   _workerst   CONDA_PREFIXt   ROOT_PREFIXt
   _envs_dirst
   _pkgs_dirst   _user_agentt   _proxy_serverst   _conda_versiont   set_conda_prefixR   R   R   t   user_rc_pathR   t   sys_rc_pathR6   R7   R8   t   _clean(   R<   t   parent(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR'   I  s$    										c         C   sY   |  j  r? xI |  j  D]% } | j   r |  j  j |  q q Wn d |  _ |  j j   d S(   s3   Remove references of inactive workers periodically.N(   R   R   t   removeR   R   R3   R`   (   R<   t   w(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   f  s    		c         C   s[   t  |  j  d k rW |  j j   |  _ |  j j |  j  |  j j   |  j j   n  d  S(   Ni   (   t   lenR   t   popleftR   R   Rt   R   R3   (   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _startp  s
    c         C   s   t  |  j  d k S(   s"   Check if a worker is still active.i    (   R   R   (   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt	   is_activew  s    c         C   sl   xe |  j  D]Z } y | j j   Wn t k
 r4 n Xy | j j   Wn t k
 rY n X| j   q
 Wd S(   s   Kill all working processes.N(   R   Rx   t
   disconnectRV   RW   R   (   R<   t   worker(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   terminate_all_processes{  s    c   
      C   s   | rr t  j d k r? t |  j d  } t |  j d d  } n$ t |  j d  } t |  j d  } | | g } n	 d g } | j |  t | d | d	 | d
 | }	 |	 j j |  j  |  j	 j
 |	  |  j   |	 S(   s   
        Call conda with the list of extra arguments, and return the worker.

        The result can be force by calling worker.communicate(), which returns
        the tuple (stdout, stderr).
        t   win32s
   python.exet   Scriptss   conda-script.pys
   bin/pythons	   bin/condat   condaR>   R@   RB   (   t   syst   platformR   R   t   extendR%   Rx   R8   R   R   Rt   (
   R<   t
   extra_argsR   R>   R@   RB   t   pythonR   R=   t   process_worker(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _call_conda  s$    		
c      
   C   s%   |  j  | d | d t d | d | S(   NR   R>   R@   RB   (   R   Rj   (   R<   R   R   R@   RB   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _call_and_parse  s    c         C   s1  g  } |  j  d t  r3 d |  k r3 t d   n  d |  k r^ | j d |  j d  g  n  d |  k r | j d |  j d  g  n  d |  k r |  j d  } t | t  r | j d | g  q | j d  | j |  n  xD | D]< } | |  k r |  | r | j d	 | j d
 d   q q W| S(   s!   Setup install commands for conda.t   override_channelst   channels0   conda search: override_channels requires channelt   envs   --namet   prefixs   --prefixs	   --channels   --t   _t   -(	   Rw   R.   t	   TypeErrorR   t   popR   R   Rt   t   replace(   t   kwargst   keysR=   R   t   key(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt#   _setup_install_commands_from_kwargs  s"    $c         C   s   t  j j   } |  j } | r' | } n  | rn | |  j k rE d } n t  j j |  } | | d <| | d <n  | r d | d <n  | S(   s0   Set the right CONDA_PREFIX environment variable.t   rootR   t   CONDA_DEFAULT_ENVt   CONDA_DEFAULT_PYTHONN(   t   osRB   t   copyR   t   pathR   R   (   R<   R   t   no_default_pythont   environ_copyt   conda_prefixt   name(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _set_environment_variables  s    			
c         C   s   | d
 k r1 |  j d t  } | j   d } n | d |  _ | d |  _ | d |  _ | d |  _ | d |  _ g  } xR | d j	 d	  D]= } y t
 |  } Wn t k
 r | } n X| j |  q Wt |  |  _ d
 S(   s   
        Set the prefix of the conda environment.

        This function should only be called once (right after importing
        conda_api).
        R   i    t   root_prefixR   t	   envs_dirst	   pkgs_dirst
   user_agentt   conda_versiont   .N(   R   R   R.   R_   R   R   R   R   R   RS   t   intt
   ValueErrorRt   t   tupleR   (   R<   R   R   t   versiont   partt   new_part(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR     s     
c         C   s   |  j  d g d |  j S(   s=   Return the version of conda being used (invoked) as a string.s	   --versionR@   (   R   t   _get_conda_version(   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_conda_version	  s    	c         C   s   t  j d  } y | j | j   j    } Wn& t k
 rV | j | j    } n X| d k r y | j |  j   j    } Wq t k
 r | j |  j    } q Xn  | d k r t d j |    n  | j d  S(   s   Callback for get_conda_version.s   conda:?\s+(\d+\.\d\S+|unknown)s   output did not match: {0}i   N(	   t   ret   compilet   matchRn   Rp   RV   R   Rr   t   group(   RZ   Rc   t   patt   m(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR     s    c         C   s\   |  j  r |  j  } nC t j j |  j d g  } t j j t   d d g  } | | } | S(   s    Conda package cache directories.t   pkgss   .conda(   R   R   t   sepR   R   R   (   R<   R   t	   pkgs_patht   user_pkgs_path(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   &  s    	
c         C   s^   |  j  r |  j  } nE t j j |  j d g  } t j j t   d d g  } | | g } | S(   sa   
        Conda environment directories.

        The first writable item should be used.
        t   envss   .conda(   R   R   R   R   R   R   (   R<   R   t	   envs_patht   user_envs_path(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   3  s    	c         C   s   |  j  S(   N(   R   (   R<   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   D  s    c         C   s:   g  } x- |  j  D]" } t |  r | j |  q q W| S(   s'   Conda writable environment directories.(   R   R   Rt   (   R<   t   writablet   env_dir(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   envs_dirs_writableH  s
    c         C   s   | r t  j d  n  g  } xd |  j D]Y } t j j |  r& t j |  } | g  | D] } t j j | | g  ^ qW 7} q& q& Wg  | D]0 } t j j |  r |  j	 d |  r | ^ q } | S(   s;   Return environment list of absolute path to their prefixes.Re   R   (
   R   Rq   R   R   R   R   t   listdirR   R   t   environment_exists(   R<   t   logt   all_envsR   t
   envs_namesR}   t
   valid_envs(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_envsQ  s    60c         C   sZ   d } | d k r |  j } n  |  j   } x) | D]! } t |  | k r1 | } q1 q1 W| S(   s9   Return full prefix path of environment defined by `name`.R   N(   R   R   R   R   (   R<   R   R   R   R   t   p(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_prefix_envnamec  s    c         C   s7   t  j j |  } | o' |  j d |  s3 | } n  | S(   s   
        Return name of environment defined by full `prefix` path.

        Returns the name if it is located in the default conda environments
        directory, otherwise it returns the prefix.
        R   (   R   R   R   R   (   R<   R   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_name_envprefixp  s    	c   	      C   s  t  j t |    t j j |   s, t   St   } t |  d  } t |  rt d   t j |  D  } | r x | D]} } t j j | |  } t j j	 |  r| t
 |   } | j   } Wd QXd | k s d | k r | j | d   q q| q| Wqt d   | D  } n  | S(   s=   Return set of canonical names of linked packages in `prefix`.s
   conda-metac         s   s$   |  ] } | j  d   r | Vq d S(   s   .jsonN(   t   endswith(   t   .0t   fname(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pys	   <genexpr>  s    Nt	   app_entryt   app_typeic         s   s   |  ] } | d   Vq d S(   iN(    (   R   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pys	   <genexpr>  s    (   R   Rq   R   R   R   R   t   setR   R   t   isfilet   opent   readt   add(	   R   t   appst   packagest   meta_dirt
   meta_filesR   t   fpatht   fR#   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   linked|  s$    	c         C   s  |  j  | d t } t | d  } i  } x | D] } t j j | | d  } t j j |  r1 t |   } t j |  } Wd QX|  j	 |  \ }	 }
 } i | j
 d |	  d 6| j
 d d  d 6|
 g d	 6| j
 d
 d  d 6i | j
 d
 d  |
 6d
 6d d 6} | | |	 <q1 q1 W| S(   s+   Return local installed apps info on prefix.R   s
   conda-metas   .jsonNR   t   summaryRe   t   descriptiont   versionsR   Rb   t
   image_path(   R   Rj   R   R   R   R   R   RT   t   loadt   split_canonical_nameRw   R   (   R<   R   t   linked_appsR   t	   apps_infot
   linked_appR   R   R#   t   nRQ   t   bt   app_info(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   linked_apps_info  s&    

c         C   s   t  |  j d d   S(   s9   Split a canonical package name into name, version, build.R   i   (   R   t   rsplit(   t   cname(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR    s    c         C   sA   t  j t d   |  j |  } |  j d d g d | d | S(   s3   Return a dictionary with configuration information.Re   R   s   --jsonR   RB   (   R   Rq   R   R   R   (   R<   R   R   RB   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR     s    c         C   s   |  j  d | d g d | S(   s-   Return a dictionary with package information.R   s   --jsonR   (   R   (   R<   t   packageR   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   package_info  s    c         K   s   d d g } |  j  |  } | r6 | r6 t d   n  | rL | j |  n  | rh | j d | g  n  d | k r | j d | j d  g  n  | j |  j | d   |  j | d | j d t  d | S(   s   Search for packages.t   searchs   --jsons/   conda search: only one of regex or spec alloweds   --specR   s
   --platformt	   canonicalt   unknownt   use_index_cachet   outdatedR   R   RB   (   R  R  R  R  R   (	   R   R   Rt   R   R   R   R   Rw   Rj   (   R<   t   regext   specR   R   R=   RB   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR    s,        
c         C   s   | r | |  j  k r | d k r d | k r | j d  } | d  } d j |  } | d } d j d j |  | |  } | S| Sd S(   s   
        Adapt a channel to include token of the logged user.

        Ignore default channels, and ignore channels that already include a
        token.
        t   defaultss   /t/t   /is   t/{0}s   {0}/{1}/{2}N(   t   DEFAULT_CHANNELSRS   Rr   R   (   R<   R   t   tokent	   url_partsR   t   middlet   endt   token_channel(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   parse_token_channel  s    

c         C   s   t  j t | | f   | d k r@ | d k r@ t d   n  d d d | d g } | rq | j d | g  n | r | j d | g  n  |  j |  S(	   s  
        Create new environment using conda-env via a yaml specification file.

        Unlike other methods, this calls conda-env, and requires a named
        environment and uses channels as defined in rcfiles.

        Parameters
        ----------
        name : string
            Environment name
        yamlfile : string
            Path to yaml file with package spec (as created by conda env export
        s!   must specify a `name` or `prefix`R   t   creates   -fs   --jsons   --names   --prefixN(   R   Rq   R   R   R   R   R   (   R<   t   yamlfileR   R   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _create_from_yaml  s    c         C   s  t  j t | | f   |  j d | d |  } | d k rX | d k rX t d   n  | r | j d  r |  j | d | d | } n3| s t | t	 t
 f  r t d   n  |  j d k r d
 d d g } n d
 d d d g } | r7| }	 g  |  j D] }
 t j j |
 |  ^ q } | j d | g  n7 | rb| }	 | g } | j d | g  n t d   t d   | D  rt d j |	    n  | j |  |  j | d | } | S(   s   
        Create an environment with a specified set of packages.

        Default python option is on deprecation route for 4.4.
        R   R   s!   must specify a `name` or `prefix`s   .yamlt   ymlR   sK   must specify a list of one or more packages to install into new environmenti   i   R  s   --yess   --jsons   --mkdirs   --names   --prefixsE   must specify either an environment name or a path for new environmentc         s   s!   |  ] } t  j j |  Vq d  S(   N(   R   R   t   exists(   R   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pys	   <genexpr>P  s    s$   Conda environment {0} already existsRB   N(   s   .yamlR!  (   i   i   (   R   Rq   R   R   R   R   R   R   R   t   listR   R   R   R   R   R   R   t   anyR   Rr   R   (   R<   R   R   R   t   fileR   RB   Rz   R=   t   refR{   R  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR    s<    	+		c   	      C   s9  t  j t | | f   |  j d | d |  } | sQ t | t t t f  r` t d   n  d d d d g } | r | j d | g  n | r | j d	 | g  n  t | t t f  r | j |  n% t | t  r | j d
 | g  n  | s| j d g  n  | r&| j d g  n  |  j	 | d | S(   s>   Install a set of packages into an environment by name or path.R   R   sP   must specify a list of one or more packages to install into existing environmentt   installs   --yess   --jsons   --force-pschecks   --names   --prefixs   --files	   --no-depss	   --dry-runRB   (
   R   Rq   R   R   R   R#  R   R   R   R   (	   R<   R   R   R   t   dept   dry_runR   RB   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR'  [  s(    
	 c   
      C   s  t  j t | | f   |  j d | d |  } d d d d g }	 | r` | r` t d   n  | r| |	 j d | g  n | r |	 j d	 | g  n  t | t t f  r |	 j |  n  | s |	 j d
 g  n  | r |	 j d g  n  |  j	 |	 d | S(   s>   Install a set of packages into an environment by name or path.R   R   t   updates   --yess   --jsons   --force-pschecks:   Must specify at least one package to update, or all_=True.s   --names   --prefixs	   --no-depss	   --dry-runRB   (
   R   Rq   R   R   R   R   R   R#  R   R   (
   R<   R   R   R   R(  t   all_R)  R   RB   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR*    s$    	c         C   s   t  j t | | f   d d d g } | rE | rE t d   n  | ra | j d | g  n( | r} | j d | g  n t d   | r | j d g  n | j |  | r | j d	 g  n  |  j |  S(
   s   
        Remove a package (from an environment) by name.

        Returns {
            success: bool, (this is always true),
            (other information)
        }
        R   s   --jsons   --yess9   Must specify at least one package to remove, or all=True.s   --names   --prefixsE   must specify either an environment name or a path for package removals   --alls	   --dry-run(   R   Rq   R   R   R   R   (   R<   R   R   R   R+  R)  R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR     s"    	c         C   s   |  j  d | d | d t  S(   s?   Remove an environment entirely specified by `name` or `prefix`.R   R   R+  (   R   Rj   (   R<   R   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   remove_environment  s    c         K   s   d d g } | r | s% | p! | r4 t  d   n  | rP | j d | g  n  | rl | j d | g  n  | j d | g  | j |  j | d   |  j | d | j d t  S(   s6   Clone the environment `clone` into `name` or `prefix`.R  s   --jsonsA   conda clone_environment: exactly one of `name` or `path` requireds   --names   --prefixs   --cloneR)  R  R  t	   use_localt   no_pint   forcet   allR   R   t   no_default_packagesR   (
   R)  R  R  R-  R.  R/  R0  R   R   R1  (   R   R   R   R   Rw   Rj   (   R<   t   clone_from_prefixR   R   R   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   clone_environment  s2             
c         C   sR   d d g } d |  k r2 | j  d |  d g  n  d |  k rN | j d  n  | S(   s    Setup config commands for conda.s   --jsons   --forceR%  s   --filet   systems   --system(   R   Rt   (   R   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _setup_config_from_kwargs  s    c         C   s}   d d g } | r | } nK | rK |  j  |  rK t j j |  j d  } n | r] |  j } n	 |  j } | j d | g  | S(   s    Setup config commands for conda.s   --jsons   --forces   .condarcs   --file(   R   R   R   R   R   R   R   R   (   R<   R%  R   R4  R=   t   config_file(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _setup_config_args  s    		c         O   sW   d d g } | j  |  | j  |  j |   |  j | d | j d t  d d   S(   s   
        Get the values of configuration keys.

        Returns a dictionary of values. Note, the key may not be in the
        dictionary if the key wasn't set in the configuration file.
        t   configs   --getR   R@   c         S   s   |  d S(   NRw   (    (   t   ot   e(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   <lambda>8  s    (   R   R5  R   Rw   Rj   (   R<   R   R   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt
   config_get*  s    c   	      C   s_   d d | t  |  g } |  j d | d | d |  } | j |  |  j | d | d d   S(	   sj   
        Set a key to a (bool) value.

        Returns a list of warnings Conda may have emitted.
        R8  s   --setR4  R%  R   R   R@   c         S   s   |  j  d g   S(   Nt   warnings(   Rw   (   R9  R:  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;  J  s    (   R   R7  R   R   (	   R<   R   t   valueR%  R   R4  R   R=   t   args(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt
   config_set;  s    c   	      C   sY   d d | | g } |  j  d | d | d |  } | j |  |  j | d | d d   S(	   st   
        Append entry to the end of a list key.

        Returns a list of warnings Conda may have emitted.
        R8  s   --appendR4  R%  R   R   R@   c         S   s   |  j  d g   S(   NR=  (   Rw   (   R9  R:  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;  \  s    (   R7  R   R   (	   R<   R   R>  R%  R   R4  R   R=   R?  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   config_appendM  s    c         C   s(   |  j  | | d | d | d | d | S(   s)   Prepend entry to the start of a list key.R%  R   R4  R   (   t
   config_add(   R<   R   R>  R%  R   R4  R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   config_prepend_  s    c   	      C   sY   d d | | g } |  j  d | d | d |  } | j |  |  j | d | d d   S(	   sc   
        Add a value to a key.

        Returns a list of warnings Conda may have emitted.
        R8  s   --addR4  R%  R   R   R@   c         S   s   |  j  d g   S(   NR=  (   Rw   (   R9  R:  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;  {  s    (   R7  R   R   (	   R<   R   R>  R%  R   R4  R   R=   R?  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyRB  l  s    c   	      C   sY   d d | | g } |  j  d | d | d |  } | j |  |  j | d | d d   S(	   sh   
        Remove a value from a key.

        Returns a list of warnings Conda may have emitted.
        R8  s   --removeR4  R%  R   R   R@   c         S   s   |  j  d g   S(   NR=  (   Rw   (   R9  R:  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;    s    (   R7  R   R   (	   R<   R   R>  R%  R   R4  R   R=   R?  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   config_remove~  s    c      
   C   s%   |  j  | d | d | d | d | S(   Re   R%  R   R4  R   (   t   config_delete(   R<   R   R%  R   R4  R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   config_remove_key  s    c         C   sV   d d | g } |  j  d | d | d |  } | j |  |  j | d | d d   S(	   sd   
        Remove a key entirely.

        Returns a list of warnings Conda may have emitted.
        R8  s   --remove-keyR4  R%  R   R   R@   c         S   s   |  j  d g   S(   NR=  (   Rw   (   R9  R:  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;    s    (   R7  R   R   (   R<   R   R%  R   R4  R   R=   R?  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyRE    s    c         C   s_   d |  k r[ |  j  d  x? |  j   D]. \ } } t j j |  s& |  j  |  q& q& Wn  |  S(   s!   Callback for show sources method.t   cmd_line(   R   RL   R   R   R   (   t   sourcesRh   R   R+  RP   RQ   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   _config_show_sources  s    c         C   s   |  j  d t d | d |  S(   s  
        Show configuration sources.

        Parameters
        ----------
        prefix : str
            This is equivalent of using `--env` flag for the activated
            environent `prefix`.
        all : bool
            This includes all the configuration options in envs, which depend
            on the concept of an activated environment. If both prefix and
            all are provided, all overides the specific path.
        RH  R   R+  (   t   config_showRj   (   R<   R   R+  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   config_show_sources  s    c            s   d d g }  j  d   } | rd | j d   j | d | d     f d   d | } n( | j d	   j | d | d | } | S(
   s   Show configuration options.R8  s   --jsonR   s   --show-sourcesR   R@   c            s    j  |  | d  d   S(   NR   R+  (   RI  (   R9  R:  (   R+  R   R<   (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR;    s   RB   s   --show(   R   Rt   R   (   R<   R   R+  RH  R   R=   RB   R   (    (   R+  R   R<   s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyRJ    s    	c         C   s   | r | } nZ | r? |  j  d |  r? t j j | d  } n* | rQ |  j } n | sc |  j } n d } i  } | r t j j |  r t |   } t	 j
 |  } Wd QXn  | S(   s  
        Load the raw conda configuration file using pyyaml.

        Depending on path or specific environment prefix and system that
        config file will be returned. If invalid or inexistent file, then an
        empty dicionary is returned.

        Parameters
        ----------
        path : str
            Path to conda configuration file.
        prefix : str
            Prefix path, to retrieve the specific prefix configuration file.
        system : bool
            Retrieve the system configuration file.
        R   s   .condarcN(   R   R   R   R   R   R   R   R   R   t   yamlR  (   R<   R   R   R4  t   config_pathR#   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   load_rc  s    	c         C   s   | s t  | t t f  r, t d   n  d d d d g } | sW | j d g  n  | rs | j d | g  n | r | j d | g  n  | j |  |  j |  S(	   s<   Get dependenciy list for packages to be installed in an env.sP   must specify a list of one or more packages to install into existing environmentR'  s	   --dry-runs   --jsons   --force-pschecks	   --no-depss   --names   --prefix(   R   R#  R   R   R   R   (   R<   R   R   R   t   channelsR(  R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   dependencies  s    c         C   s   | r" t  j t | | f   n  | r. | sF | d k rU | d k rU t d   n  | rm |  j |  } n  | d k r |  j } n  t j j	 t j j
 | d   S(   s   Check if an environment exists by 'name' or by 'prefix'.

        If query is by 'name' only the default conda environments directory is
        searched.
        s.   Exactly one of 'name' or 'prefix' is required.s
   conda-metaN(   R   Rq   R   R   R   R   R   R   R   R   R   (   R<   R   R   R   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   7  s    $c         C   s"   d d d g } |  j  | d | S(   s#   Clean any conda lock in the system.t   cleans   --locks   --jsonR   (   R   (   R<   R   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt
   clear_lockM  s    c   
      C   s   i  } | r! | r! t  d   n  | r9 |  j |  } n  |  j d |  r xj |  j |  D]V } | | k r[ |  j |  \ } } }	 | r d j | |	  | | <q | | | <q[ q[ Wn  | j |  S(   s-   Get installed package version in a given env.s.   Exactly one of 'name' or 'prefix' is required.R   s   {0}={1}(   R   R   R   R   R  Rr   Rw   (
   R<   R   R   t   pkgt   buildt   package_versionsR  R  RQ   R  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   package_versionR  s    c          C   s   i d d 6d d 6d d 6d d 6d d 6}  d	 d
 d h } |  j  t j d  } d t j } | d k r t j   | k r t j   } d j |  } n* i d d 6d d 6| } d j | |  } | S(   s4   Get platform of current system (system and bitness).t   linuxt   linux2t   osxt   darwint   winR   t   openbsdt   openbsd5t   armv6lt   armv7lt   ppc64leR  i   s	   linux-{0}t   x86_64i@   t   x86i    s   {0}-{1}(   Rw   R   R   R   t   __itemsize__t   machineRr   (   t   _sys_mapt   non_x86_linux_machinest   sys_platformt   bitst	   arch_namet   subdir(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_platformh  s     
c         C   s   |  j  d | d |  } i  } t j j d  } t j j d  } | rU | | d <n  | rh | | d <n  | j d i   } | j |  | S(   s   Load the proxy configuration.R   R4  t
   HTTP_PROXYt   HTTPS_PROXYt   httpt   httpst   proxy_servers(   RN  R   RB   Rw   R*  (   R<   R   R4  R8  Rp  Rl  Rm  t   proxy_servers_conf(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   load_proxy_config  s    s   https://conda.anaconda.orgc   	      C   s  |  j  d t  j d |  j  } g  } | d k ri |  j    } | j d  } | d k ri d g } qi n  | r| d d k r d n d } x} | D]u } | d k r | | 7} q | j d	  s | j d
  s | j d  r | j |  q | j | j | |   q W| } n  | S(   s   Return all the channel urls defined in .condarc.

        If no condarc file is found, use the default channels.
        the `default_channel_alias` key is ignored and only the anaconda client
        `url` key is used.
        R4  t   default_channelsRO  R  iR  s   {0}/{1}s   {0}{1}s   http://s   https://s   file://N(   RN  Rj   Rw   R  R   t
   startswithRt   Rr   (	   R<   t	   normalizet	   conda_urlRO  Rs  t   normalized_channelst   condarct   templateR   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_condarc_channels  s*    	c         C   sn   |  j  d | d |  } | j |  t | d t d | } | j j |  j  |  j j |  |  j   | S(   s   Call pip in QProcess worker.R   R   R?   R@   (	   t   _pip_cmdR   R%   Rj   Rx   R8   R   R   Rt   (   R<   R   R   R   R@   R=   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt	   _call_pip  s    
c         C   s   | r | s | p | r( t  d   n  | rR |  j d |  rR |  j |  } n  t j d k r t | d  } t | d  } n t | d  } t | d  } | | g } | S(   s9   Get pip location based on environment `name` or `prefix`.s6   conda pip: exactly one of 'name' or 'prefix' required.R   R   s
   python.exes   pip.exes
   bin/pythons   bin/pip(   R   R   R   R   R   R   (   R<   R   R   R   R?   R=   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR{    s    c         C   s   | r | s | p | r( t  d   n  | r@ |  j |  } n  t j j | d d g  } | t g } t | d t d t d |  j d i | d 6} | j	 j
 |  j  |  j j |  |  j   | S(	   s#   Get list of pip installed packages.s6   conda pip: exactly one of 'name' or 'prefix' required.t   binR   R?   R>   R@   RA   R   (   R   R   R   R   R   R   R%   Rj   t	   _pip_listRx   R8   R   R   Rt   (   R<   R   R   R   t   pip_commandR=   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   pip_list  s"    	
c         C   s   | } |  j  |  } g  } g  | D] } |  j |  d ^ q" } x= | D]5 }	 |  j |	  d }
 |
 | k rH | j |	  qH qH W| S(   s   Callback for `pip_list`.i    (   R   R  Rt   (   R<   RZ   Rc   R   Rz   R   t   pip_onlyR~   t   linked_namesRS  R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR~    s    &c         C   sq   t  j t | | f   t | t t f  r@ d j |  } n | } d d | g } |  j d | d | d |  S(   s@   Remove a pip package in given environment by `name` or `prefix`.Ra   t	   uninstalls   --yesR   R   R   (   R   Rq   R   R   R#  R   R   R|  (   R<   R   R   R   RS  R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt
   pip_remove  s    c         C   s+   d | g } |  j  d d d | d |  j  S(   s9   Search for pip packages in PyPI matching `search_string`.R  R   R   R   R@   (   R|  t   _pip_search(   R<   t   search_stringR   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt
   pip_search,  s
    	c         C   s   i  } t  |   j d  } x d | k r: | j d  q WxV | D]N } d | k rB | j d  } | d j   } | d j   } | | | <qB qB W| S(   s   Callback for pip search.s   
Re   s    - i    i   (   R"   RS   R   Rp   (   RZ   Rc   Rz   t   linest   linet   partsR   R   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR  ;  s    c         C   s  | p |  j  } i  } g  } g  } g  } xg | D]_ } t j j | d  } y0 t t j j | d  d  j   | g } PWq. t k
 r q. Xq. Wx| D]} t j j | d  } t j j |  rYg  t j |  D] }	 t j j | |	  ^ q }
 g  |
 D] }	 t j j	 |	  r |	 ^ q }
 g  |
 D] }	 |	 j
 d  r(|	 ^ q(}
 | j |
  n  | r xK | D]@ } t j j | |  j |   } | | k rf| j |  qfqfWq q W| d k r| } n  i  } x | D] } t | d   } | j   } Wd QXy t j |  } Wn t k
 r)i  } n X| j d  } t j j |  } | r| | k r| | } | | k r| | | <| | | <qq| | | <| | | <qqW| S(   s,   Return repodata stored in conda cache files.t   caches   urls.txtt   as   .jsont   rNt   _url(   R   R   R   R   R   R   RV   R   R   R   R   R   t   cache_fn_urlRt   R   R   RT   RU   Rw   t   getmtime(   R<   RO  R   t   repodata_dict	   all_pathst   paths_to_loadt   official_pkgs_dirst   pkgs_dirR  R   t   pathsR   t   repodata_patht	   mod_datesR   t   raw_dataR#   t   urlt   current_mod_datet   previous_mod_date(    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   get_repodataL  s^    "	1+(		



c   
      C   s   |  j  d |  } t } xm | j   D]_ \ } } g  | j d i   j   D] \ } } | j d d  ^ qJ }	 | |	 k } | r% Pq% q% W| S(   sW   
        Check if a package is available for install based on cached repodata.
        RO  R   R   Re   (   R  R.   RL   Rw   (
   R<   RS  RO  t   repodatat   checkR  R#   t   cnt   subdataR   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   is_package_available  s    :c         C   sH   |  j  d  s |  d 7}  n  t j t |    j   } d j | d   S(   s2   Return file name for channel fully normalized url.R  s   {}.jsoni   (   R   t   hashlibt   md5R   t	   hexdigestRr   (   R  R  (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR    s    N(I   R   R   R   R   R'   R   R   R   R   Rj   R.   R   R   t   staticmethodR   R   R   R   R   R   t   propertyR   R   R   R   R   R   R   R   R	  R  R   R  R  R  R   R  R'  R*  R   R,  R3  R5  R7  R<  R@  RA  RC  RB  RD  RF  RE  RI  RK  RJ  RN  RP  R   RR  RV  Rk  Rr  Rz  R|  R{  R  R~  R  R  R  R  R  R  (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyR   F  s   	
			#
				
#	:)$(*	&&0Fc           C   s   t  d k r t   a  n  t  S(   s   Conda non blocking api.N(   t	   CONDA_APIR   R   (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   CondaAPI  s    c         C   s   t  d 7a  t  | | f GHd S(   s   Local test helper.i   N(   t   COUNTER(   R   t   outputRh   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   ready_print  s    
c          C   sU   d d l  m }  |    } t   } | j d d d } | j j t  | j   d S(   s   Run local test.i(   t   qapplications1   /Users/gpena-castellanos/Desktop/requirements.ymlR   s,   /Users/gpena-castellanos/anaconda/envs/booboN(   t"   anaconda_navigator.utils.qthelpersR  R  R   Rx   R8   R  t   exec_(   R  t   appt	   conda_apiR   (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt
   local_test  s    			t   __main__(:   R   t   collectionsR    t   os.pathR   R   R   R   R   R  RT   R   R   R   R   t   qtpy.QtCoreR   R   R   R	   R
   RL  t   navigator_updater.configR   R   t   navigator_updater.utils.condaR   t    navigator_updater.utils.encodingR   t   navigator_updater.utils.findpipR   t   navigator_updater.utils.logsR   t   navigator_updater.utils.miscR   t   __version__RD   R   R  RV   R   R   R   R   R   R   t   PY3R.   t   DEBUGR"   R$   R%   R   R  R  R  R  R   (    (    (    s>   lib/python2.7/site-packages/navigator_updater/api/conda_api.pyt   <module>   sT   ((	     k	
		%