
>[c           @@ s  d  d l  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 d  d l m Z d d l Td 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 d d l m Z d d l m Z e j d  Z  d d l! m" Z" e"   d Z# [" d e e e f d     YZ$ d d l! m" Z" e"   d Z# [" d S(   i    (   t   absolute_importt   print_functiont   unicode_literalsN(   t
   raise_from(   t   quotei   (   t   *(   t   stream_multipartt   NullAuth(   t   compute_hasht   jencodet   pv(   t   STATUS_CODES(   t   OrgMixin(   t   ChannelsMixin(   t   PackageMixin(   t   errorsu   binstar(   t   get_versionsu   versiont   Binstarc        	   B@ s  e  Z d  Z d$ d e d  Z e d    Z d   Z d   Z	 d   Z
 d   Z d$ d$ d$ d$ d$ d e e j   d	  Z d
   Z d   Z d   Z d$ d$ d  Z d g d  Z d$ d  Z d$ d$ d$ d$ d$ d  Z d   Z d   Z d   Z d   Z d$ d  Z d$ d$ e d$ d$ d$ d$ d  Z d   Z d   Z d   Z d   Z  d$ d  Z! d$ d$ d  Z" d$ d  Z# d d$ d$ d$ d$ d% d$ d!  Z$ d$ d$ d"  Z% d#   Z& RS(&   u   
    An object that represents interfaces with the Anaconda repository restful API.

    :param token: a token generated by Binstar.authenticate or None for
                  an anonymous user.
    u   https://api.anaconda.orgc         K@ s   t  j   |  _ t |  j j d <| |  j _ t   |  j _ | |  _	 t
 |  _ d j t  } |  j j j i | d 6d d 6d d 6 | r |  j j j i d j |  d 6 n  | j d	  r | d
  } n  | j d  s d | } n  | |  _ d  S(   Nu   x-binstar-api-versionu*   Anaconda-Client/{} (+https://anaconda.org)u
   User-Agentu   application/jsonu   Content-Typeu   Acceptu   token {}u   Authorizationu   /iu   http://u   https://(   u   http://u   https://(   t   requestst   Sessiont   _sessiont   __version__t   headerst   sessiont   verifyR   t   autht   tokent   Falset   _token_warning_sentt   formatt   updatet   endswitht
   startswitht   domain(   t   selfR   R!   R   t   kwargst
   user_agent(    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   __init__*   s$    		&c         C@ s   |  j  S(   N(   R   (   R"   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyR   B   s    c         C@ s   d } y |  j  j |  j  } Wn) t k
 rJ } t t j |  |  n Xy |  j |  Wn. t j k
 r } t t j |  |   n Xd S(   u`   
        Checks if the server is reachable and throws
        and exception if it isn't
        u>   API server not found. Please check your API url configuration.N(	   R   t   headR!   t	   ExceptionR   R   t   ServerErrort   _check_responset   NotFound(   R"   t   msgt   responset   e(    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   check_serverF   s    c         C@ s]   d |  j  } |  j j |  } y% |  j |  | j   } | d SWn t k
 rX d SXd  S(   Nu   %s/authentication-typeu   authentication_typeu   password(   R!   R   t   getR)   t   jsont   BinstarError(   R"   t   urlt   res(    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   authentication_typeW   s    c         O@ sN   y* d d l  m } |  j |   | |  SWn t k
 rI t d   n Xd  S(   Ni    (   t   HTTPKerberosAuthu   Kerberos authentication requires the requests-kerberos package to be installed:
    conda install requests-kerberos
or: 
    pip install requests-kerberos(   t   requests_kerberosR5   t   _authenticatet   ImportErrorR1   (   R"   t   argsR#   R5   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   krb_authenticatea   s    c         O@ s   |  j  | | f | |  S(   N(   R7   (   R"   t   usernamet   passwordR9   R#   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   authenticaten   s    u   strongc         C@ s   d |  j  } i	 | d 6| d 6| d 6|
 d 6| d 6| d 6d d 6| d	 6|	 d
 6} t |  \ } } |  j j | d | d | d | } |  j |  | j   } | d } |  j j j i d | d 6 | S(   u?  
        Use basic authentication to create an authentication token using the interface below.
        With this technique, a username and password need not be stored permanently, and the user can
        revoke access at any time.

        :param username: The users name
        :param password: The users password
        :param application: The application that is requesting access
        :param application_url: The application's home page
        :param scopes: Scopes let you specify exactly what type of access you need. Scopes limit access for the tokens.
        u   %s/authenticationsu   scopesu   noteu   note_urlu   hostnameu   useru   max-ageu   created_withu   strengthu   fail-if-existsR   t   dataR   u   tokenu   token %su   AuthorizationN(	   R!   t   NoneR	   R   t   postR)   R0   R   R   (   R"   R   t   applicationt   application_urlt   for_usert   scopest   created_witht   max_aget   strengtht   fail_if_already_existst   hostnameR2   t   payloadR>   R   R3   R   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyR7   q   s    
$
c         C@ s3   d |  j  } t j |  } |  j |  | j   S(   Nu	   %s/scopes(   R!   R   R/   R)   R0   (   R"   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   list_scopes   s    c         C@ s6   d |  j  } |  j j |  } |  j |  | j   S(   uJ   
        Retrieve information on the current authentication token
        u   %s/authentication(   R!   R   R/   R)   R0   (   R"   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   authentication   s    c         C@ s6   d |  j  } |  j j |  } |  j |  | j   S(   uA   
        Get a list of the current authentication tokens
        u   %s/authentications(   R!   R   R/   R)   R0   (   R"   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   authentications   s    c         C@ st   | r; | r% d |  j  | | f } qK d |  j  | f } n d |  j  f } |  j j |  } |  j | d g  d S(   uS   
        Remove the current authentication or the one given by `auth_name`
        u!   %s/authentications/org/%s/name/%su   %s/authentications/name/%su   %s/authenticationsi   N(   R!   R   t   deleteR)   (   R"   t	   auth_namet   organizationR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   remove_authentication   s    i   c   	      C@ s  | j  j d d  } t |  t t  k rG t j d | t f  n  |  j r d | j  k r t j d j | j  d   t |  _ n  d | j  k r t j d  n  d | j  k r t j d	  n  | j	 | k rt
 j | j	 d  \ } } d | | | j j | j j | j	 f } y | j   } Wn n X| j d |  } t j } | j	 d k rit j } nQ | j	 d k rt j } n6 | j	 d k rt j } n | j	 d k rt j } n  | | | j	   n  d  S(   Nu   x-binstar-api-versionu   0.2.1u   The api server is running the binstar-api version %s. you are using %s
Please update your client with pip install -U binstar or conda update binstaru   Conda-Token-Warningu   Token warning: {}u   X-Anaconda-Lockdownu2   Anaconda repository is currently in LOCKDOWN mode.u   X-Anaconda-Read-Onlyu3   Anaconda repository is currently in READ ONLY mode.u   ?u   Undefined erroru   %s: %s ([%s] %s -> %s)u   errori  i  i  i  (   u   ?u   Undefined error(   R   R/   R
   R   t   loggert   warningR   R   t   Truet   status_codeR   t   requestt   methodR2   R0   R   R1   t   UnauthorizedR*   t   ConflictR(   (	   R"   R3   t   allowedt   api_versiont   shortt   longR+   R>   t   ErrCls(    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyR)      s:    	(	c         C@ s^   | r d |  j  | f } n d |  j  } |  j j | d |  j j } |  j |  | j   S(   u   
        Get user information.

        :param login: (optional) the login name of the user or None. If login is None
                      this method will return the information of the authenticated user.
        u
   %s/user/%su   %s/userR   (   R!   R   R/   R   R)   R0   (   R"   t   loginR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   user   s    c   	      C@ s   | r d j  |  j |  } n d j  |  j  } t j   } | rO | | d <n  | rb | | d <n  | ru | | d <n  | r | | d <n  |  j j | d | } |  j |  | j   S(   u  
        Returns a list of packages for a given user and optionally filter
        by `platform`, `package_type` and `type_`.

        :param login: (optional) the login name of the user or None. If login
                      is None this method will return the packages for the
                      authenticated user.
        :param platform: only find packages that include files for this platform.
           (e.g. 'linux-64', 'osx-64', 'win-32')
        :param package_type: only find packages that have this kind of file
           (e.g. 'env', 'conda', 'pypi')
        :param type_: only find packages that have this conda `type`
           (i.e. 'app')
        :param access: only find packages that have this access level
           (e.g. 'private', 'authenticated', 'public')
        u   {0}/packages/{1}u   {0}/packagesu   platformu   package_typeu   typeu   accesst   params(   R   R!   t   collectionst   OrderedDictR   R/   R)   R0   (	   R"   R_   t   platformt   package_typet   type_t   accessR2   t	   argumentsR3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   user_packages   s    c         C@ s?   d |  j  | | f } |  j j |  } |  j |  | j   S(   u   
        Get information about a specific package

        :param login: the login of the package owner
        :param package_name: the name of the package
        u   %s/package/%s/%s(   R!   R   R/   R)   R0   (   R"   R_   t   package_nameR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   package%  s    c         C@ sB   d |  j  | | | f } |  j j |  } |  j | d g  d  S(   Nu"   %s/packages/%s/%s/collaborators/%si   (   R!   R   t   putR)   (   R"   t   ownerRj   t   collaboratorR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   package_add_collaborator1  s    c         C@ sB   d |  j  | | | f } |  j j |  } |  j | d g  d  S(   Nu"   %s/packages/%s/%s/collaborators/%si   (   R!   R   RN   R)   (   R"   Rm   Rj   Rn   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   package_remove_collaborator7  s    c         C@ sE   d |  j  | | f } |  j j |  } |  j | d g  | j   S(   Nu   %s/packages/%s/%s/collaboratorsi   (   R!   R   R/   R)   R0   (   R"   Rm   Rj   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   package_collaborators=  s    c         C@ sO   d |  j  } i | p d d 6} |  j j | d | } |  j |  | j   S(   u	   
        u   %s/package_listingu    u   modified_afterR>   (   R!   R   R/   R)   R0   (   R"   t   modified_afterR2   R>   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   all_packagesD  s
    c
         C@ s   d |  j  | | f }
 | p i  } | | d <|	 g | d <i | d 6| d 6| d 6| d <t d t |  d	 t d
 t | p| i    } t |  \ } } |  j j |
 d | d | } |  j |  | j   S(   uI  
        Add a new package to a users account

        :param login: the login of the package owner
        :param package_name: the name of the package to be created
        :param package_type: A type identifier for the package (eg. 'pypi' or 'conda', etc.)
        :param summary: A short summary about the package
        :param license: the name of the package license
        :param license_url: the url of the package license
        :param public: if true then the package will be hosted publicly
        :param attrs: A dictionary of extra attributes for this package
        u   %s/package/%s/%su   summaryu   package_typesu   nameu   urlu   familyu   licenset   publict   publisht   public_attrsR>   R   (	   R!   t   dictt   boolR   R	   R   R@   R)   R0   (   R"   R_   Rj   t   summaryt   licenseRt   t   license_urlt   license_familyt   attrsRe   R2   RJ   R>   R   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   add_packageN  s    
c         C@ s?   d |  j  | | f } |  j j |  } |  j | d g  d  S(   Nu   %s/package/%s/%si   (   R!   R   RN   R)   (   R"   R;   Rj   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   remove_packagew  s    c         C@ sB   d |  j  | | | f } |  j j |  } |  j |  | j   S(   u   
        Get information about a specific release

        :param login: the login of the package owner
        :param package_name: the name of the package
        :param version: the name of the package
        u   %s/release/%s/%s/%s(   R!   R   R/   R)   R0   (   R"   R_   Rj   t   versionR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   release  s    c         C@ sB   d |  j  | | | f } |  j j |  } |  j | d g  d S(   u   
        remove a release and all files under it

        :param username: the login of the package owner
        :param package_name: the name of the package
        :param version: the name of the package
        u   %s/release/%s/%s/%si   N(   R!   R   RN   R)   (   R"   R;   Rj   R   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   remove_release  s    c         C@ s   d |  j  | | | f } | s( i  } n  i | d 6| d 6d d 6} | j |  t |  \ }	 }
 |  j j | d |	 d |
 } |  j |  | j   S(   un  
        Add a new release to a package.

        :param login: the login of the package owner
        :param package_name: the name of the package
        :param version: the version string of the release
        :param requirements: A dict of requirements TODO: describe
        :param announce: An announcement that will be posted to all package watchers
        u   %s/release/%s/%s/%su   requirementsu   announceu   descriptionR>   R   N(   R!   R?   R   R	   R   R@   R)   R0   (   R"   R_   Rj   R   t   requirementst   announcet   release_attrsR2   RJ   R>   R   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   add_release  s    	
c         C@ sE   d |  j  | | | | f } |  j j |  } |  j |  | j   S(   Nu   %s/dist/%s/%s/%s/%s(   R!   R   R/   R)   R0   (   R"   R_   Rj   R   t   basenameR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   distribution  s    c         C@ s   | r% d |  j  | | | | f } n1 | rJ d |  j  | | | | f } n t d   |  j j |  } |  j |  | j   S(   Nu   %s/dist/%s/%s/%s/%su   %s/dist/%s/%s/%s/-/%su?   method remove_dist expects either 'basename' or '_id' arguments(   R!   t	   TypeErrorR   RN   R)   R0   (   R"   R_   Rj   R   R   t   _idR2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   remove_dist  s    c   
      C@ s   d |  j  | | | | f } | r2 i | d 6} n i  } |  j j | d | d t } |  j | d d d d g | j d k r | S| j d k r d S| j d k r t j | j d	 d
 t	 }	 |	 Sd S(   u  
        Download a package distribution

        :param login: the login of the package owner
        :param package_name: the name of the package
        :param version: the version string of the release
        :param basename: the basename of the distribution to download
        :param md5: (optional) an md5 hash of the download if given and the package has not changed
                    None will be returned

        :returns: a file like object or None
        u   %s/download/%s/%s/%s/%su   ETagR   t   allow_redirectsRZ   i   i.  i0  u   locationt   streamN(
   R!   R   R/   R   R)   RU   R?   R   R   RT   (
   R"   R_   Rj   R   R   t   md5R2   R   R3   t   res2(    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   download  s    u    u   mainc         C@ s  d |  j  | | | t |  f } | d k r7 i  } n  t | t  sU t d   n  t d | d | d | d |
 d |  } t |  \ } } |  j j | d | d	 | } |  j	 |  | j
   } | d
 } | d } | d k rt | d |	 \ } } }	 nK |	 d k rN| j   } | j d t j  | j   | }	 | j |  n  |	 | d <| | d <t | d i | | f d 6d | \ } } | j |  j   r|  j n t } | j | d | d |  j j d d d	 | } | j d k r,t j | j  t j d  t j d  t j d | j   n  d |  j  | | | t |  f } t d | d  } t |  \ } } |  j j | d | d	 | } |  j	 |  | j
   S(    uH  
        Upload a new distribution to a package release.

        :param login: the login of the package owner
        :param package_name: the name of the package
        :param version: the version string of the release
        :param basename: the basename of the distribution to download
        :param fd: a file like object to upload
        :param distribution_type: pypi or conda or ipynb, etc
        :param description: (optional) a short description about the file
        :param attrs: any extra attributes about the file (eg. build=1, pyversion='2.7', os='osx')

        u   %s/stage/%s/%s/%s/%su#   argument attrs must be a dictionaryt   distribution_typet   descriptionR}   t   dependenciest   channelsR>   R   u   post_urlu	   form_datat   sizei    u   Content-Lengthu   Content-MD5t   filesu   filet   callbackR   t   timeouti
   i<   i   u    u   Error uploading packageu   %s/commit/%s/%s/%s/%st   dist_idu   dist_idNiX  i  (   R!   R   R?   t
   isinstanceRw   R   R	   R   R@   R)   R0   R   t   tellt   seekt   ost   SEEK_ENDR   R    R   R   RU   RR   t   infot   textR   R1   (   R"   R_   Rj   R   R   t   fdR   R   R   R   R   R}   R   R   R2   RJ   R>   R   R3   t   objt   s3urlt   s3datat   _hexmd5t   b64md5t   spost   data_streamt   request_methodt   s3res(    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   upload  sP    "	



!	"c         C@ sQ   d |  j  } |  j j | d i | d 6| d 6| d 6} |  j |  | j   S(   Nu	   %s/searchRa   u   nameu   typeu   platform(   R!   R   R/   R)   R0   (   R"   t   queryRe   Rd   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   search4  s    c         C@ s>   d j  d |  j  } |  j j |  } |  j |  | j   S(   u.   Download the user current trial/paid licenses.u   {domain}/licenseR!   (   R   R!   R   R/   R)   R0   (   R"   R2   R3   (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   user_licenses>  s    N(   u   main('   t   __name__t
   __module__t   __doc__R?   RT   R%   t   propertyR   R.   R4   R:   R=   R   Rd   t   nodeR7   RK   RL   RM   RQ   R)   R`   Ri   Rk   Ro   Rp   Rq   Rs   R~   R   R   R   R   R   R   R   R   R   R   (    (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyR   "   s`   		
						
'&				"				'B
(%   t
   __future__R    R   R   Rb   R   R   t   warningst   loggingRd   t   sixR   t   six.moves.urllib.parseR   R   t   requests_extR   R   t   utilsR   R	   R
   t   utils.http_codesR   t   mixins.organizationsR   t   mixins.channelsR   t   mixins.packageR   t    t	   getLoggerRR   t   _versionR   R   R   (    (    (    sV   lib/python2.7/site-packages/anaconda_client-1.7.2-py2.7.egg/binstar_client/__init__.pyt   <module>   s4   
  &