U
    àÄDf.  ã                   @   sx   d 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mZ ddlmZ G dd	„ d	eƒZd
d„ ZdS )zhdistutils.command.register

Implements the Distutils 'register' command (register with the repository).
é    N)Úlog)Úwarné   ©Úalways_iterable)ÚPyPIRCCommandc                   @   sŽ   e Zd ZdZejddg Zejdddg Zddd	„ fgZd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd„ZdS ) Úregisterz7register the distribution with the Python package index)úlist-classifiersNz list the valid Trove classifiers)ÚstrictNzBWill stop the registering if the meta-data are not fully compliantÚverifyr	   r
   Úcheckc                 C   s   dS )NT© ©Úselfr   r   úJ/tmp/pip-unpacked-wheel-wqy06g0s/setuptools/_distutils/command/register.pyÚ<lambda>$   ó    zregister.<lambda>c                 C   s   t  | ¡ d| _d| _d S )Nr   )r   Úinitialize_optionsÚlist_classifiersr
   r   r   r   r   r   &   s    
zregister.initialize_optionsc                 C   s*   t  | ¡ d| jfddœ}|| jjd< d S )Nr   )r   é   )r
   Úrestructuredtextr   )r   Úfinalize_optionsr
   ÚdistributionÚcommand_options)r   Úcheck_optionsr   r   r   r   +   s
    
þzregister.finalize_optionsc                 C   sT   |   ¡  |  ¡  |  ¡ D ]}|  |¡ q| jr8|  ¡  n| jrH|  ¡  n|  ¡  d S ©N)	r   Ú_set_configÚget_sub_commandsÚrun_commandÚdry_runÚverify_metadatar   ÚclassifiersÚsend_metadata)r   Úcmd_namer   r   r   Úrun4   s    

zregister.runc                 C   s8   t dtƒ | j d¡}| ¡  | j|_d|_| ¡  dS )zDeprecated API.zVdistutils.command.register.check_metadata is deprecated; use the check command insteadr   r   N)r   ÚDeprecationWarningr   Úget_command_objÚensure_finalizedr
   r   r$   )r   r   r   r   r   Úcheck_metadataC   s    ýzregister.check_metadatac                 C   sz   |   ¡ }|i kr@|d | _|d | _|d | _|d | _d| _n6| jd| jfkr^td| j ƒ‚| jdkrp| j| _d| _d	S )
z0Reads the configuration file and set attributes.ÚusernameÚpasswordÚ
repositoryÚrealmTÚpypiz%s not found in .pypircFN)Ú_read_pypircr)   r*   r+   r,   Ú
has_configÚDEFAULT_REPOSITORYÚ
ValueError)r   Úconfigr   r   r   r   P   s    




zregister._set_configc                 C   s*   | j d }tj |¡}t |  |¡¡ dS )z.Fetch the list of classifiers from the server.z?:action=list_classifiersN)r+   ÚurllibÚrequestÚurlopenr   ÚinfoÚ_read_pypi_response)r   ÚurlÚresponser   r   r   r!   `   s    
zregister.classifiersc                 C   s&   |   |  d¡¡\}}t d||¡ dS )z<Send the metadata to the package index server to be checked.r   úServer response (%s): %sN)Úpost_to_serverÚbuild_post_datar   r6   )r   ÚcodeÚresultr   r   r   r    f   s    zregister.verify_metadatac           
      C   sà  | j rd}| j}| j}nd}d }}d ¡ }||krd|  dtj¡ tƒ }|sRd}q,||kr,tdƒ q,|dkrp|s|tdƒ}qn|sŒt	 	d¡}q|t
j ¡ }t
j | j¡d	 }| | j|||¡ |  |  d
¡|¡\}}|  d|› d|› tj¡ |dkrÜ| j r|| j_nf|  dtj¡ |  d|  ¡  tj¡ d}| ¡ dkrRtdƒ}|s.d}q.| ¡ dkrÜ|  ||¡ nl|dkrŒddi}	d |	d<  |	d< |	d< d|	d< |	d s¼tdƒ|	d< q¢|	d |	d kr4|	d sêt	 	d¡|	d< qÎ|	d st	 	d¡|	d< qê|	d |	d kr¼d|	d< d|	d< tdƒ q¼|	d sNtdƒ|	d< q4|  |	¡\}}|dkrvt d ||¡ nt d!¡ t d"¡ nP|d#krÜdd$i}	d|	d< |	d sÀtd%ƒ|	d< q¦|  |	¡\}}t d ||¡ dS )&a  Send the metadata to the package index server.

        Well, do the following:
        1. figure who the user is, and then
        2. send the data as a Basic auth'ed POST.

        First we try to read the username/password from $HOME/.pypirc,
        which is a ConfigParser-formatted file with a section
        [distutils] containing username and password entries (both
        in clear text). Eg:

            [distutils]
            index-servers =
                pypi

            [pypi]
            username: fred
            password: sekrit

        Otherwise, to figure who the user is, we offer the user three
        choices:

         1. use existing login,
         2. register as a new user, or
         3. set the password to a random string and email the user.

        Ú1ÚxÚ z1 2 3 4zâWe need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]: z&Please choose one of the four options!z
Username: z
Password: r   ZsubmitzServer response (z): éÈ   zAI can store your PyPI login so future submissions will be faster.z (the login will be stored in %s)ÚXZynzSave your login (y/N)?ÚnÚyÚ2ú:actionÚuserÚnamer*   ÚemailNZconfirmz
 Confirm: z!Password and confirm don't match!z
   EMail: r:   z"You will receive an email shortly.z7Follow the instructions in it to complete registration.Ú3Zpassword_resetzYour email address: )r/   r)   r*   ÚsplitÚannounceÚloggingÚINFOÚinputÚprintÚgetpassr3   r4   ÚHTTPPasswordMgrÚparseÚurlparser+   Úadd_passwordr,   r;   r<   r   Ú_get_rc_fileÚlowerÚ_store_pypircr   r6   )
r   Úchoicer)   r*   ÚchoicesÚauthÚhostr=   r>   Údatar   r   r   r"   l   s”    ø






û
þ








zregister.send_metadatac                 C   s   | j j}|d| ¡ | ¡ | ¡ | ¡ | ¡ | ¡ | ¡ | 	¡ | 
¡ | ¡ | ¡ | ¡ | ¡ | ¡ | ¡ dœ}|d s„|d s„|d rŒd|d< |S )Nz1.0)rG   Úmetadata_versionrI   ÚversionÚsummaryZ	home_pageÚauthorÚauthor_emailÚlicenseÚdescriptionÚkeywordsÚplatformr!   Údownload_urlÚprovidesÚrequiresÚ	obsoletesri   rj   rk   z1.1r_   )r   ÚmetadataÚget_nameÚget_versionÚget_descriptionÚget_urlÚget_contactÚget_contact_emailÚget_licenceÚget_long_descriptionÚget_keywordsÚget_platformsÚget_classifiersÚget_download_urlÚget_providesÚget_requiresÚget_obsoletes)r   ÚactionÚmetar^   r   r   r   r<   í   s,    îzregister.build_post_dataNc              
   C   sØ  d|kr$|   d |d | j¡tj¡ d}d| }|d }t ¡ }| ¡ D ]b\}}tt	t
|ƒƒD ]J}	| |¡ | d| ¡ | d¡ | |	¡ |	r^|	d d	kr^| d
¡ q^qH| |¡ | d
¡ | ¡  d¡}d| t	t|ƒƒdœ}
tj | j||
¡}tj tjj|d¡}d}z| |¡}W nx tjjk
rd } z"| jrH|j ¡ }|j|jf}W 5 d}~X Y nJ tjjk
r– } zdt	|ƒf}W 5 d}~X Y nX | jrª|  |¡}d}| jrÔd
 d|df¡}|   |tj¡ |S )z9Post a query to the server, and return a string response.rI   zRegistering {} to {}z3--------------GHSKFJDLGDS7543FJKLFHRE75642756743254z
--z--z*
Content-Disposition: form-data; name="%s"z

éÿÿÿÿúÚ
zutf-8z/multipart/form-data; boundary=%s; charset=utf-8)zContent-typezContent-length)Úpassword_mgrrA   Niô  )rB   ÚOKzK---------------------------------------------------------------------------)rM   Úformatr+   rN   rO   ÚioÚStringIOÚitemsÚmapÚstrÚmake_iterableÚwriteÚgetvalueÚencodeÚlenr3   r4   ÚRequestÚbuild_openerÚHTTPBasicAuthHandlerÚopenÚerrorÚ	HTTPErrorÚshow_responseÚfpÚreadr=   ÚmsgÚURLErrorr7   Újoin)r   r^   r\   ÚboundaryZsep_boundaryZend_boundaryÚbodyÚkeyÚvaluesÚvalueÚheadersÚreqÚopenerr>   Úer—   r   r   r   r;   	  sZ    þ




ÿ
ýÿ

zregister.post_to_server)N)Ú__name__Ú
__module__Ú__qualname__re   r   Úuser_optionsÚboolean_optionsÚsub_commandsr   r   r$   r(   r   r!   r    r"   r<   r;   r   r   r   r   r      s,   þý	 r   c                 C   s   | d krd gS t | ƒS r   r   )r   r   r   r   r‰   @  s    r‰   )Ú__doc__rR   r„   rN   Úurllib.parser3   Úurllib.requestZdistutils._logr   Úwarningsr   Ú
_itertoolsr   Úcorer   r   r‰   r   r   r   r   Ú<module>   s     .