
    =e]                     2   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
m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d	lT 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!m"Z" ddl#m$Z$ ddl%m&Z&  ej'        d          Z( G d deee          Z)dS )    )absolute_importprint_functionunicode_literalsN)parse_version)quote)tqdm   )errors)__version__)*)ChannelsMixin)OrgMixin)PackageMixin)NullAuth)compute_hashjencode)STATUS_CODES)multipart_files_uploadbinstarc                   D   e Zd ZdZ	 	 d)dZed             Zd Zd Zd	 Z	d
 Z
dddddd ej                    fdZd Zd Zd Zd*dZd+dZd+dZ	 	 	 	 	 d,dZd Zd Zd Zd Zd+dZ	 	 	 	 	 	 	 d-dZd Zd Zd Zd Zd Zd  Z d+d!Z!d*d"Z"d+d#Z#	 	 d.d&Z$d*d'Z%d( Z&dS )/Binstarz
    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.
    Nhttps://api.anaconda.orgTc                 (   t          j                    | _        t          | j        j        d<   || j        _        t                      | j        _        || _	        d| _
        d                    t                    }| j        j                            |ddd           |r4| j        j                            dd                    |          i           |                    d          r
|d d	         }|                    d
          sd|z   }|| _        d S )Nx-binstar-api-versionFz*Anaconda-Client/{} (+https://anaconda.org)zapplication/json)z
User-AgentzContent-TypeAcceptAuthorizationztoken {}/)zhttp://https://r   )requestsSession_sessionr   headerssessionverifyr   authtoken_token_warning_sentformatupdateendswith
startswithdomain)selfr'   r-   r%   kwargs
user_agents         7lib/python3.11/site-packages/binstar_client/__init__.py__init__zBinstar.__init__*   s    !(**9D56$$JJ
#( AHHUU
$$$.(&
 &
 	 	 	  	VM!((/:;L;LU;S;S)TUUU??3 	!CRC[F  !899 	)&(F    c                     | j         S N)r"   )r.   s    r1   r$   zBinstar.sessionE   s
    }r3   c                    d}	 | j                             | j                  }n'# t          $ r}t	          j        |          |d}~ww xY w	 |                     |           dS # t          j        $ r}t	          j        |          |d}~ww xY w)z?Check if server is reachable or throw an exception if it isn't.zCAPI server is unavailable. Please check your API url configuration.N)r$   headr-   	Exceptionr
   ServerError_check_responseNotFound)r.   msgresponseerrors       r1   check_serverzBinstar.check_serverI   s    S	5|((55HH 	5 	5 	5$S))u4	5	5  ***** 	5 	5 	5$S))u4	5s,   $ 
AAAA# #B2BBc                     d| j         z  }| j                            |          }	 |                     |           |                                }|d         S # t
          $ r Y dS w xY w)Nz%s/authentication-typeauthentication_typepassword)r-   r$   getr:   jsonBinstarErrorr.   urlress      r1   rA   zBinstar.authentication_typeW   sw    &4ls##	  %%%((**C,-- 	 	 	::	s   0A 
A%$A%c                     	 ddl m}  | j         |            g|R i |S # t          $ r}t	          d          |d }~ww xY w)Nr   )HTTPKerberosAuthzKerberos authentication requires the requests-kerberos package to be installed:
    conda install requests-kerberos
or: 
    pip install requests-kerberos)requests_kerberosrJ   _authenticateImportErrorrE   )r.   argsr/   rJ   r>   s        r1   krb_authenticatezBinstar.krb_authenticatea   s    
	::::::%4%&6&6&8&8J4JJJ6JJJ 	 	 	4 
 	s   ! 
A ;A c                 (     | j         ||fg|R i |S r5   )rL   )r.   usernamerB   rN   r/   s        r1   authenticatezBinstar.authenticaten   s*    !t!8X"6HHHHHHHr3   strongFc
           	      :   d| j         z  }
||||	||||d}t          |          \  }}| j                            |
|||          }|                     |           |                                }|d         }| j        j                            dd|z  i           |S )a?  
        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.
        %s/authentications)scopesnotenote_urlhostnameuserzmax-agestrengthzfail-if-exists)r&   datar#   r'   r   ztoken %s)r-   r   r$   postr:   rD   r#   r*   )r.   r&   applicationapplication_urlfor_userrV   max_ager[   fail_if_already_existsrY   rG   payloadr\   r#   rH   r'   s                   r1   rL   zBinstar._authenticateq   s    , #dk2#[o'#%'%;= =  ((gl$T7KKS!!!hhjjG##_jE6J$KLLLr3   c                     | j          d}t          j        |d          }|                     |           |                                S )Nz/scopes<   )timeout)r-   r    rC   r:   rD   rF   s      r1   list_scopeszBinstar.list_scopes   sH    %%%l3+++S!!!xxzzr3   c                     d| j         z  }| j                            |          }|                     |           |                                S )z9Retrieve information on the current authentication token.z%s/authenticationr-   r$   rC   r:   rD   rF   s      r1   authenticationzBinstar.authentication   sE    !T[1ls##S!!!xxzzr3   c                     d| j         z  }| j                            |          }|                     |           |                                S )z0Get a list of the current authentication tokens.rU   ri   rF   s      r1   authenticationszBinstar.authentications   sG     #dk2ls##S!!!xxzzr3   c                     |r|r| j         d|d|}n| j         d|}n
| j         d}| j                            |          }|                     |dg           dS )zS
        Remove the current authentication or the one given by `auth_name`
        z/authentications/org/z/name/z/authentications/name/z/authentications   Nr-   r$   deleter:   )r.   	auth_nameorganizationrG   rH   s        r1   remove_authenticationzBinstar.remove_authentication   s      	8 N=A[[[,,,XaXab6:kkk99M*.+++7Cl!!#&&S3%(((((r3   c           
         |dgn|}|j                             dd          }t          |          t          t                    k    r!t                              d|t                     | j        s6d|j         v r-t                              d|j         d                    d| _        d|j         v rt                              d	           d
|j         v rt                              d           |j        |vrt          j        |j        d          \  }}|d|d|j	        j
        d|j	        j        d|j        d
}	 |                                }n# t          $ r i }Y nw xY w|                    d|          }t          j        }|j        dk    rt          j        }nG|j        dk    rt          j        }n/|j        dk    rt          j        }n|j        dk    rt          j        } |||j                  d S )N   r   z0.2.1zThe 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 binstarzConda-Token-WarningzToken warning: %sTzX-Anaconda-Lockdownz2Anaconda repository is currently in LOCKDOWN mode.zX-Anaconda-Read-Onlyz3Anaconda repository is currently in READ ONLY mode.)?zUndefined errorz: z ([z] z -> )r>     i  i  i  )r#   rC   pvr   loggerwarningr(   status_coder   requestmethodrG   rD   r8   r
   rE   Unauthorizedr;   Conflictr9   )	r.   rH   allowedapi_versionshortlongr<   r\   ErrClss	            r1   r:   zBinstar._check_response   s   "?3%%koo&=wGGk??R__,,NN`	   ' 	,,AS[,P,PNN.<Q0RSSS'+D$ CK//NNOPPP!S[00NNPQQQ?'))&*3?<TUUKE4.3eeTTT3;;M;M;Ms{`c`o`o`opCxxzz    ((7C((C(F#%%,C''C''C''+&co...) *)s   >E E"!E"c                    |r| j          d| }n'| j        r| j          d}nt          j        dd          | j                            || j        j                  }|                     |           |                                S )z
        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.
        z/user/z/userzPAuthentication token is missing. Please, use `anaconda login` to reauthenticate.rx   r%   )	r-   r'   r
   r   r$   rC   r%   r:   rD   )r.   loginrG   rH   s       r1   rZ   zBinstar.user   s      	i[////CCZ 	i['''CC%bdgi i i ls4<+>??S!!!xxzzr3   c                 Z   |rd                     | j        |          }nd                     | j                  }t          j                    }|r||d<   |r||d<   |r||d<   |r||d<   | j                            ||          }|                     |           |                                S )a  
        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')
        z{0}/packages/{1}z{0}/packagesplatformpackage_typetypeaccessparams)r)   r-   collectionsOrderedDictr$   rC   r:   rD   )	r.   r   r   r   type_r   rG   	argumentsrH   s	            r1   user_packageszBinstar.user_packages   s    .  	5$++DK??CC ''44C+--	 	-$,Ij! 	5(4In% 	& %If 	)"(Ihls955S!!!xxzzr3   c                     | j         d|d|}| j                            |          }|                     |           |                                S )z
        Get information about a specific package

        :param login: the login of the package owner
        :param package_name: the name of the package
        	/package/r   ri   )r.   r   package_namerG   rH   s        r1   packagezBinstar.package'  sS     %)KKKEls##S!!!xxzzr3   c                     | j         d|d|d|}| j                            |          }|                     |dg           d S N
/packages/r   z/collaborators/rn   )r-   r$   putr:   r.   ownerr   collaboratorrG   rH   s         r1   package_add_collaboratorz Binstar.package_add_collaborator3  sU    6:kkk555,,,XdXdels##S3%(((((r3   c                     | j         d|d|d|}| j                            |          }|                     |dg           d S r   ro   r   s         r1   package_remove_collaboratorz#Binstar.package_remove_collaborator8  sU    6:kkk555,,,XdXdel!!#&&S3%(((((r3   c                     | j         d|d|d}| j                            |          }|                     |dg           |                                S )Nr   r   z/collaboratorsru   ri   )r.   r   r   rG   rH   s        r1   package_collaboratorszBinstar.package_collaborators=  sW    37;;;|||Tls##S3%(((xxzzr3   c                     d| j         z  }d|pdi}| j                            ||          }|                     |           |                                S )Nz%s/package_listingmodified_after )r\   ri   )r.   r   rG   r\   rH   s        r1   all_packageszBinstar.all_packagesD  sY    "dk2 ."6B7ls..S!!!xxzzr3   c
                 Z   |	g n|	j         g}
| j        d|d|}|pi }||d<   |
|d<   |||d|d<   t          |          dt          |pi           d	}t	          |          \  }}| j                            |||
          }|                     |           |                                S )aI  
        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
        Nr   r   summarypackage_types)namerG   familylicenseF)publicpublishpublic_attrsr\   r#   )	valuer-   booldictr   r$   r]   r:   rD   )r.   r   r   r   r   r   license_urllicense_familyattrsr   r   rG   rc   r\   r#   rH   s                   r1   add_packagezBinstar.add_packageK  s    0 +29K8L$(KKKE"i!.o$
 
i "&\\eTRWR][]M^M^__((gl$@@S!!!xxzzr3   c                    d                     | j        ||          }dt          |          i}t          |          \  }}| j                            |||          }|                     |           |                                S )z
        Update public_attrs of the package on a users account

        :param login: the login of the package owner
        :param package_name: the name of the package to be updated
        :param attrs: A dictionary of attributes to update
        z{}/package/{}/{}r   r   r)   r-   r   r   r$   patchr:   rD   )	r.   r   r   r   rG   rc   r\   r#   rH   s	            r1   update_packagezBinstar.update_packagew  s|     !''ULII!4;;/((gl  4 AAS!!!xxzzr3   c                    d                     | j        |||          }dt          |          i}t          |          \  }}| j                            |||          }	|                     |	           |	                                S )a<  
        Update release public_attrs of the package on a users account

        :param login: the login of the package owner
        :param package_name: the name of the package to be updated
        :param version: version of the package to update
        :param attrs: A dictionary of attributes to update
        z{}/release/{}/{}/{}r   r   r   )
r.   r   r   versionr   rG   rc   r\   r#   rH   s
             r1   update_releasezBinstar.update_release  s~     $**4;|WUU!4;;/((gl  4 AAS!!!xxzzr3   c                     | j         d|d|}| j                            |          }|                     |dg           d S )Nr   r   rn   ro   )r.   rQ   r   rG   rH   s        r1   remove_packagezBinstar.remove_package  sM    $(KKK<<Hl!!#&&S3%(((((r3   c                     | j         d|d|d|}| j                            |          }|                     |           |                                S )z
        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
        	/release/r   ri   )r.   r   r   r   rG   rH   s         r1   releasezBinstar.release  sY     (,{{{EEE<<<Qls##S!!!xxzzr3   c                     | j         d|d|d|}| j                            |          }|                     |dg           dS )z
        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
        r   r   rn   Nro   )r.   rQ   r   r   rG   rH   s         r1   remove_releasezBinstar.remove_release  sU     (,{{{HHHlllGGTl!!#&&S3%(((((r3   c                    | j         d|d|d|}|si }||dd}|                    |           t          |          \  }	}
| j                            ||	|
          }|                     |           |                                S )an  
        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 NOTE: describe
        :param announce: An announcement that will be posted to all package watchers
        r   r   N)requirementsannouncedescriptionr   )r-   r*   r   r$   r]   r:   rD   )r.   r   r   r   r   r   release_attrsrG   rc   r\   r#   rH   s               r1   add_releasezBinstar.add_release  s     (,{{{EEE<<<Q 	M ) 
 

 	}%%%((gl$@@S!!!xxzzr3   c           	          | j         d|d|d|d|	}| j                            |          }|                     |           |                                S )N/dist/r   ri   )r.   r   r   r   basenamerG   rH   s          r1   distributionzBinstar.distribution  s_    '+{{{EEE<<<RZRZ[ls##S!!!xxzzr3   c           	         |r| j         d|d|d|d|	}n'|r| j         d|d|d|d|	}nt          d          | j                            |          }|                     |           |                                S )Nr   r   z/-/z?method remove_dist expects either 'basename' or '_id' arguments)r-   	TypeErrorr$   rp   r:   rD   )r.   r   r   r   r   _idrG   rH   s           r1   remove_distzBinstar.remove_dist  s     	_+/;;;|||WWWV^V^_CC 	_-1[[[%%%wwwX[X[\CC]^^^l!!#&&S!!!xxzzr3   c           	      @   | j         d|d|d|d|	}|rd|i}ni }| j                            ||d          }|                     |g d           |j        dk    r|S |j        d	k    rd
S |j        dk    r$t          j        |j        d         dd          }	|	S d
S )a  
        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
        z
/download/r   ETagF)r#   allow_redirects)ru   .  0  )r   ru   r   Nr   locationTi  )streamrf   )r-   r$   rC   r:   r|   r    r#   )
r.   r   r   r   r   md5rG   r#   rH   res2s
             r1   downloadzBinstar.download  s     ,0;;;|||WWWV^V^_ 	soGGGlsGUKKS///:::?c!!J?c!!4?c!!
 <J 7l[[[DKtr3   r   mainc                    | j         d|d|d|dt          |          	}|i }t          |t                    st	          d          |	|	n!t          ||
t          j                  d         }	t          |t                    s|j	        }||||||	d}t          |          \  }}| j                            |||          }|                     |           |                                }|d	         }|d
         }|t          ||
          \  }}}
nb|
`|                                }|                    dt"          j                   |                                |z
  }
|                    |           t          |
          |d<   ||d<   t#          j        |                                          j        }t-          |ddd          5 }t/          ||d||fi|| j        j                  }ddd           n# 1 swxY w Y   |j        dk    rwt4                              |j                   t;          j        |j                  }d}|                    d          j        dk    rd}tA          j!        d|z  |j                  | j         d|d|d|dt          |          	}d|d         i}t          |          \  }}| j                            |||          }|                     |           |                                S )a  
        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 release: the version string of the release
        :param basename: the basename of the distribution to download
        :param file: 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 md5: (optional) base64 encoded md5 hash calculated from package file
        :param sha256: (optional) base64 encoded sha256 hash calculated from package file
        :param size: (optional) size of package file in bytes
        :param dependencies: (optional) list package dependencies
        :param attrs: any extra attributes about the file (eg. build=1, pyversion='2.7', os='osx')
        :param channels: list of labels package will be available from
        z/stage/r   Nz#argument attrs must be a dictionary)sizehash_algorithmr   )distribution_typer   r   dependencieschannelssha256r   post_url	form_data)r   zContent-LengthzContent-MD5BTi   )totalunit
unit_scaleunit_divisorfiler   rn   r   CodeInvalidDigestz0 The Content-MD5 or checksum value is not valid.zError uploading package!%sz/commit/dist_id)"r-   r   
isinstancer   r   r   hashlibr   strr   r   r$   r]   r:   rD   tellseekosSEEK_ENDfstatfilenost_sizer   r   r%   r|   rz   infotextET
fromstringfindr
   rE   )r.   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   rc   r\   r#   rH   objs3urls3data_hexmd5b64md5spos	file_sizeprogresss3res	xml_errormsg_tails                                 r1   uploadzBinstar.upload  s]   ( )-UUULLL'''SXYaSbSbSbc=E%&& 	CABBB!-<4`g`n3o3o3opq3r+S11 	8 1 7 "3&( 
 
  ((gl$@@S!!!hhjjJ[!;$0D$A$A$A!GVTT\99;;DIIa%%%99;;%DIIdOOO#&t99  &}HT[[]]++3		4PPP 	,T\*v4(898|*, , ,E	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,
 ##KK
###ej11IH~~f%%*o==M%&BX&MuO`aaa)-eee\\\777TYZbTcTcTcdc)n-((gl$@@S!!!xxzzs   #G<<H H c                     ||j         }d| j        z  }| j                            ||||d          }|                     |           |                                S )Nz	%s/search)r   r   r   r   )r   r-   r$   rC   r:   rD   )r.   queryr   r   rG   rH   s         r1   searchzBinstar.search`  so    #'-LDK'ls  ,
 ,
  
 	S!!!xxzzr3   c                     d                     | j                  }| j                            |          }|                     |           |                                S )z.Download the user current trial/paid licenses.z{domain}/license)r-   )r)   r-   r$   rC   r:   rD   rF   s      r1   user_licenseszBinstar.user_licensesm  sQ     ''t{';;ls##S!!!xxzzr3   )Nr   T)NNr5   )NNNNN)NNTNNNN)r   NNNNNr   )'__name__
__module____qualname____doc__r2   propertyr$   r?   rA   rO   rR   	_platformnoderL   rg   rj   rl   rs   r:   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r3   r1   r   r   "   s         &@   6   X5 5 5    I I I '+#!"'-2-y~//$ $ $ $L      ) ) ) )*/ */ */ */X   , * * * *X
 
 
) ) )
) ) )
      * * * *X      ) ) )  
) 
) 
)  8      % % % %P [_!O O O Ob       r3   r   )*
__future__r   r   r   r   r   loggingr   r   r  defusedxml.ElementTreeElementTreer   r    pkg_resourcesr   ry   six.moves.urllib.parser   r   r   r
   	__about__r   mixins.channelsr   mixins.organizationsr   mixins.packager   requests_extr   utilsr   r   utils.http_codesr   utils.multipart_uploaderr   	getLoggerrz   r   r  r3   r1   <module>r+     s  
 I H H H H H H H H H       				     # # # # # #  - - - - - - ( ( ( ( ( (             " " " " " "     * * * * * * * * * * * * ( ( ( ( ( ( " " " " " " ( ( ( ( ( ( ( ( * * * * * * < < < < < <		9	%	%P	 P	 P	 P	 P	h| P	 P	 P	 P	 P	r3   