ó
¼S]c           @@  s@  d  Z  d d l m Z d d l m Z m Z d d l m Z d d l Z d d l	 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 d e f d „  ƒ  YZ d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z d e e ƒ f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s/   Types related to project requirement providers.i    (   t   absolute_import(   t   ABCMetat   abstractmethod(   t   deepcopyN(   t	   conda_api(   t   logged_subprocess(   t   with_metaclass(   t   makedirs_ok_if_exists(   t   SimpleStatusc         C@  s%   t  j j t  j j |  j ƒ d | ƒ S(   Nt   services(   t   ost   patht   joint   dirnamet   filename(   t   local_state_filet   relative_name(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   _service_directory   s    t   ProvideContextc           B@  st   e  Z d  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z RS(	   sS   A context passed to ``Provider.provide()`` representing state that can be modified.c         C@  s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d S(   s`  Create a ProvideContext.

        Args:
            environ (dict): environment variables to be read and modified
            local_state_file (LocalStateFile): to store any created state
            status (RequirementStatus): current status
            mode (str): one of PROVIDE_MODE_PRODUCTION, PROVIDE_MODE_DEVELOPMENT, PROVIDE_MODE_CHECK
        N(   t   environt   _local_state_filet   _default_env_spec_namet   _statust   _modet	   _frontend(   t   selfR   R   t   default_env_spec_namet   statust   modet   frontend(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   __init__   s    						c         C@  s    t  |  j | ƒ } t | ƒ | S(   s7  Create a directory in PROJECT_DIR/services with the given name.

        The name should be unique to the ServiceRequirement creating the directory,
        so usually the requirement's env var.

        Args:
            relative_name (str): name to distinguish this dir from other service directories
        (   R   R   R   (   R   R   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   ensure_service_directory.   s    	
c         C@  s]   |  j  j | ƒ } t | ƒ } | | ƒ } | | k rY |  j  j | | ƒ |  j  j ƒ  n  | S(   sá  Run a function which takes and potentially modifies the state of a service.

        If the function modifies the state it's given, the new state will be saved
        and passed in next time.

        Args:
            service_name (str): the name of the service, should be
                specific enough to uniquely identify the provider
            func (function): function to run, passing it the current state

        Returns:
            Whatever ``func`` returns.
        (   R   t   get_service_run_stateR   t   set_service_run_statet   save(   R   t   service_namet   funct	   old_statet   modifiedt   result(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   transform_service_run_state;   s    c         C@  s   |  j  S(   s&   Get the current ``RequirementStatus``.(   R   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   Q   s    c         C@  s   |  j  S(   s   Get the LocalStateFile.(   R   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   V   s    c         C@  s   |  j  S(   s   Get the default env spec.(   R   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   [   s    c         C@  s   |  j  S(   sŽ   Get flavor of provide.

        Value should be ``PROVIDE_MODE_DEVELOPMENT``, ``PROVIDE_MODE_PRODUCTION``, or ``PROVIDE_MODE_CHECK``.
        (   R   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   `   s    c         C@  s   |  j  S(   s   Get the current ``Frontend``.(   R   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   h   s    (   t   __name__t
   __module__t   __doc__R   R   R(   t   propertyR   R   R   R   R   (    (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR      s   			c         C@  s  |  j  ƒ  } | | k r/ t d t d d | ƒ Sg  } | | } d | k r§ | d } xO | D]D } t j | ƒ } | d k r\ | j d | t | ƒ | f ƒ q\ q\ Wn  |  j | t ƒ  ƒ |  j	 ƒ  | rç t d t
 d d | d | ƒ St d t d d	 | ƒ Sd
 S(   s±  Run any shutdown commands from the local state file for the given service.

    Also remove the shutdown commands from the file.

    Args:
        local_state_file (LocalStateFile): local state
        service_name (str): the name of the service, usually a
            variable name, should be specific enough to uniquely
            identify the provider

    Returns:
        a `Status` instance potentially containing errors
    t   successt   descriptions   Nothing to do to shut down %s.t   shutdown_commandsi    s1   Shutting down %s, command %s failed with code %d.s    Shutdown commands failed for %s.t   errorss   Successfully shut down %s.N(   t   get_all_service_run_statesR   t   TrueR   t   callt   appendt   reprR!   t   dictR"   t   False(   R   R#   t
   run_statesR0   t   statet   commandst   commandt   code(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   shutdown_service_run_staten   s"    

*
c         C@  sl   t  |  | ƒ } y t j d | ƒ Wn t k
 r6 n Xy t j t j j | ƒ ƒ Wn t k
 rg n Xd S(   s{  Delete a directory in PROJECT_DIR/services with the given name.

    The name should be unique to the ServiceRequirement creating the directory,
    so usually the requirement's env var.

    IF this fails, it does so silently (returns no errors).

    Args:
        relative_name (str): name to distinguish this dir from other service directories

    Returns:
        None
    R   N(   R   t   shutilt   rmtreet   OSErrorR
   t   rmdirR   R   (   R   R   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   delete_service_directory“   s    t   ProviderAnalysisc           B@  sD   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   s¼   A Provider's preflight check snapshotting the state prior to ``provide()``.

    Instances of this class are immutable, and are usually created as part of a
    ``RequirementStatus``.
    c         C@  s%   t  | ƒ |  _ | |  _ | |  _ d S(   s   Create a ProviderAnalysis.N(   R   t   _configt   _missing_env_vars_to_configuret   _missing_env_vars_to_provide(   R   t   configt   missing_env_vars_to_configuret   missing_env_vars_to_provide(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   µ   s    	c         C@  s   |  j  S(   s5   Get the configuration dict from the time of analysis.(   RD   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRG   »   s    c         C@  s   |  j  S(   sR   Get the env vars we were missing in order to configure, from the time of analysis.(   RE   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRH   À   s    c         C@  s   |  j  S(   sP   Get the env vars we were missing in order to provide, from the time of analysis.(   RF   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRI   Å   s    (   R)   R*   R+   R   R,   RG   RH   RI   (    (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRC   ®   s
   	t   ProvideResultc           B@  sD   e  Z d  Z d d „ Z d d „ Z e d „  ƒ Z e d „  ƒ Z	 RS(   s‡   A Provider's results from the ``provide()`` call.

    Instances of this class are immutable, and are returned from ``provide()``.
    c         C@  s"   | d k r g  } n  | |  _ d S(   s   Create a ProvideResult.N(   t   Nonet   _errors(   R   R0   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR   Ñ   s    	c         C@  sC   | d k r g  } n  t | ƒ d k r+ |  St d |  j | ƒ Sd S(   s.   Copy this result, appending additional errors.i    R0   N(   RK   t   lenRJ   RL   (   R   R0   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   copy_with_additions×   s
    	c         C@  s   |  j  S(   sG   Get any fatal errors that occurred during provide() preventing success.(   RL   (   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR0   á   s    c         C@  s   t  S(   sM   Get an empty ProvideResult (currently a singleton since these are immutable).(   t   _empty_provide_result(   t   cls(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   emptyæ   s    N(
   R)   R*   R+   RK   R   RN   R,   R0   t   classmethodRQ   (    (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRJ   Ë   s
   
t   Providerc           B@  sn   e  Z d  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z e d „  ƒ Z	 e d d „ ƒ Z RS(	   s6   A Provider can take some action to meet a Requirement.c         C@  s   d S(   sH  Get a list of unset environment variable names that must be set before configuring this provider.

        Args:
            requirement (Requirement): requirement instance we are providing for
            environ (dict): current environment variable dict
            local_state_file (LocalStateFile): local state file
        N(    (   R   t   requirementR   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRH   ó   s    	c         C@  s   d S(   s@  Get a list of unset environment variable names that must be set before calling provide().

        Args:
            requirement (Requirement): requirement instance we are providing for
            environ (dict): current environment variable dict
            local_state_file (LocalStateFile): local state file
        N(    (   R   RT   R   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRI   þ   s    	c         C@  s   d S(   s7  Read a config dict from the local state file for the given requirement.

        You can think of this as the GET returning a web form for
        configuring the provider. And in fact it was once used for
        that, though we deleted the html stuff now.

        The returned 'config' has a 'source' field which was
        essentially a selected radio option for where to get the
        requirement, and other fields are entry boxes underneath
        each radio option.

        This method still exists in the code in case we want to
        do a textual version (or a new HTML version, but probably
        outside of the anaconda-project codebase). See also
        UI_MODE_TEXT_ASK_QUESTIONS in the cli code.

        Args:
            requirement (Requirement): the requirement we're providing
            environ (dict): current environment variables
            local_state_file (LocalStateFile): file to read from
            default_env_spec_name (str): the fallback env spec name
            overrides (UserConfigOverrides): user-supplied forced config

        N(    (   R   RT   R   R   R   t	   overrides(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   read_config	  s    c         C@  s   d S(   s¾  Set some config values in the state file (should not save the file).

        You can think of this as the POST submitting a web form
        for configuring the provider. And in fact it was once used
        for that, though we deleted the html stuff now.

        Args:
            requirement (Requirement): the requirement we're providing
            environ (dict): current environment variables
            local_state_file (LocalStateFile): file to save to
            default_env_spec_name (str): default env spec name for this prepare
            overrides (UserConfigOverrides): if any values in here change, delete the override
            values (dict): dict from string to string

        N(    (   R   RT   R   R   R   RU   t   values(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   set_config_values_as_strings%  s    c   	      C@  s^   |  j  | | | | | ƒ } |  j | | | ƒ } |  j | | | ƒ } t d | d | d | ƒ S(   s)  Analyze whether and how we'll be able to provide the requirement.

        This is used to show the situation in the UI, and also to
        consolidate all IO-type work in one place (inside
        Requirement.check_status()).

        Returns:
          A ``ProviderAnalysis`` instance.
        RG   RH   RI   (   RV   RH   RI   RC   (	   R   RT   R   R   R   RU   RG   t   missing_to_configuret   missing_to_provide(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   analyze8  s    
c         C@  s   d S(   sŸ  Execute the provider, fulfilling the requirement.

        The implementation should read and modify the passed-in
        ``environ`` rather than accessing the OS environment
        directly.

        Args:
            requirement (Requirement): requirement we want to meet
            context (ProvideContext): context containing project state

        Returns:
            a ``ProvideResult`` instance

        N(    (   R   RT   t   context(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   provideJ  s    c         C@  s   d S(   sŽ  Undo the provide, cleaning up any files or processes we created.

        The requirement may still be met after this, if our providing wasn't
        really needed.

        Args:
            requirement (Requirement): requirement we want to de-provide
            environ (dict): current env vars, often from a previous prepare
            local_state_file (LocalStateFile): the local state
            overrides (UserConfigOverrides): overrides to state
            requirement_status (RequirementStatus or None): requirement status if available

        Returns:
            a `Status` instance describing the (non)success of the unprovision
        N(    (   R   RT   R   R   RU   t   requirement_status(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt	   unprovide\  s    N(   R)   R*   R+   R   RH   RI   RV   RX   R[   R]   RK   R_   (    (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRS   ð   s   		t   EnvVarProviderc           B@  st   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 d „ Z RS(   sE   Meets a requirement for an env var by letting people set it manually.c         C@  s   | j  d | j g d d  ƒS(   Nt	   variablest   default(   t	   get_valuet   env_varRK   (   R   RT   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   _local_state_overrides  s    c         C@  s   | j  d | j g d d  ƒS(   Nt   disabled_variablesRb   (   Rc   Rd   RK   (   R   RT   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   _disabled_local_state_overridev  s    c         C@  s*   |  j  | ƒ d k	 r d St j ƒ  f Sd S(   s*   Override superclass to require env prefix.N(    (   t   _get_env_prefixRK   R   t   conda_prefix_variable(   R   RT   R   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRH   y  s    c         C@  s   |  j  | | | ƒ S(   s*   Override superclass to require env prefix.(   RH   (   R   RT   R   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRI   €  s    c         C@  s   | j  t j ƒ  d  ƒ S(   N(   t   getR   Ri   RK   (   R   R   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRh   „  s    c         C@  sJ  t  ƒ  } d } | j rf d d l j j } |  j | ƒ }	 |	 d k rN d } qf | j |	 | j ƒ } n  | d k r‡ |  j	 | | ƒ } n  |  j
 | | ƒ }
 | d k o® |
 d k	 } | rÀ |
 } n  | d k	 rÙ | | d <n  | d k	 rõ | rõ d } nG | j | k rd } | | j | d <n d | j k r6d } n d } | | d <| S(	   s*   Override superclass to read env var value.i    Nt   valueRa   R   Rb   t   unsett   source(   R6   RK   t	   encryptedt!   anaconda_project.internal.keyringt   internalt   keyringRh   Rj   Rd   Re   Rg   t   options(   R   RT   R   R   R   RU   RG   Rk   Rq   t
   env_prefixt   disabled_valuet   was_disabledRm   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRV   ‰  s4    						
c         C@  sH   | j  r( |  j | | | | | | ƒ n |  j | | | | | | ƒ d S(   s)   Override superclass to set env var value.N(   Rn   t'   _set_encrypted_config_values_as_stringst*   _set_nonencrypted_config_values_as_strings(   R   RT   R   R   R   RU   RW   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRX   ²  s
    	c         C@  sú   d | j  g } d | j  g } | j d d ƒ d k }	 |  j | | ƒ }
 |
 d  k ri |  j | | ƒ }
 n  | j d |
 ƒ } | d  k	 rö | d k r° | j | ƒ | j | ƒ qö |	 rÖ | j | | ƒ | j | ƒ qö | j | | ƒ | j | ƒ n  d  S(   NRa   Rf   Rm   Rk   t    (   Rd   Rj   Re   RK   Rg   t   unset_valuet	   set_value(   R   RT   R   R   R   RU   RW   t   override_patht   disabled_patht
   overridingt   local_override_valuet   value_string(    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRw   ¼  s     c         C@  s“   d d  l  j j } |  j | ƒ } | j | | j ƒ }	 | j d |	 ƒ }
 |
 d  k	 r |
 d k rv | j | | j ƒ q | j | | j |
 ƒ n  d  S(   Ni    Rk   Rx   (	   Ro   Rp   Rq   Rh   Rj   Rd   RK   Rl   t   set(   R   RT   R   R   R   RU   RW   Rq   Rs   t   from_keyringR   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyRv   á  s    c         C@  s  d } | j rZ d d l j j } |  j | j ƒ } | d k	 rZ | j | | j ƒ } qZ n  | d k r~ |  j	 | | j
 ƒ } n  | d k	 r | | j | j <nS | j | j k r² n> d | j k rð | j d } | d k	 rð | | j | j <qð n  t j ƒ  j d g  ƒ S(   sG   Override superclass to use configured env var (or already-set env var).i    NRb   R0   (   RK   Rn   Ro   Rp   Rq   Rh   R   Rj   Rd   Re   R   Rr   RJ   RQ   RN   (   R   RT   R\   t   local_state_overrideRq   Rs   Rk   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR]   ñ  s"    	c         C@  s   t  d t d d | j ƒ S(   s-   Override superclass to return success always.R-   R.   s   Nothing to clean up for %s.(   R   R2   Rd   (   R   RT   R   R   RU   R^   (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR_   "  s    N(   R)   R*   R+   Re   Rg   RH   RI   Rh   RV   RX   Rw   Rv   R]   RK   R_   (    (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyR`   p  s   						)	
	%		1(   R+   t
   __future__R    t   abcR   R   t   copyR   R
   R>   t   anaconda_project.internalR   R   t#   anaconda_project.internal.metaclassR   t"   anaconda_project.internal.makedirsR   t'   anaconda_project.internal.simple_statusR   R   t   objectR   R=   RB   RC   RJ   RO   RS   R`   (    (    (    sN   lib/python2.7/site-packages/anaconda_project/requirements_registry/provider.pyt   <module>   s&   	S	%	"	€