B
    v9aÀ-  ã               @   s`   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	T d dl
mZ G dd„ deƒZdS )é    N)Úwarn)ÚPyPIRCCommand)Ú*)Úlogc               @   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)zlist-classifiersNz list the valid Trove classifiers)ÚstrictNzBWill stop the registering if the meta-data are not fully compliantÚverifyzlist-classifiersr   Úcheckc             C   s   dS )NT© )Úselfr
   r
   ú/usr/lib/python3.7/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   ÚdistributionZcommand_options)r   Zcheck_optionsr
   r
   r   r   $   s    
zregister.finalize_optionsc             C   sX   |   ¡  |  ¡  x|  ¡ D ]}|  |¡ qW | jr<|  ¡  n| jrL|  ¡  n|  ¡  d S )N)	r   Ú_set_configZget_sub_commandsZrun_commandZdry_runÚverify_metadatar   ÚclassifiersÚsend_metadata)r   Zcmd_namer
   r
   r   Úrun+   s    

zregister.runc             C   s8   t dtƒ | j d¡}| ¡  | j|_d|_| ¡  d S )Nzddistutils.command.register.check_metadata is deprecated,               use the check command insteadr	   r   )r   ÚPendingDeprecationWarningr   Zget_command_objZensure_finalizedr   r   r   )r   r	   r
   r
   r   Úcheck_metadata:   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 )	NÚusernameÚpasswordÚ
repositoryÚrealmTZpypiz%s not found in .pypircF)Z_read_pypircr   r   r   r   Ú
has_configZDEFAULT_REPOSITORYÚ
ValueError)r   Zconfigr
   r
   r   r   D   s    




zregister._set_configc             C   s*   | j d }tj |¡}t |  |¡¡ d S )Nz?:action=list_classifiers)r   ÚurllibÚrequestZurlopenr   ÚinfoÚ_read_pypi_response)r   ZurlZresponser
   r
   r   r   U   s    
zregister.classifiersc             C   s&   |   |  d¡¡\}}t d||¡ d S )Nr   zServer response (%s): %s)Úpost_to_serverÚbuild_post_datar   r$   )r   ÚcodeÚresultr
   r
   r   r   \   s    zregister.verify_metadatac       
      C   s  | j rd}| j}| j}nd}d }}d ¡ }x:||krf|  dtj¡ tƒ }|sTd}q.||kr.tdƒ q.W |dkr|x|s‚tdƒ}qtW x|s–t	 	d¡}q†W t
j ¡ }t
j | j¡d	 }| | j|||¡ |  |  d
¡|¡\}}|  d||f tj¡ |dkr | j r|| j_nj|  dtj¡ |  d|  ¡  tj¡ d}x&| ¡ dkr\tdƒ}|s8d}q8W | ¡ dkr |  ||¡ n„|dkr¬ddi}	d |	d<  |	d< |	d< d |	d< x|	d sÊtdƒ|	d< q°W x‚|	d |	d krNx|	d sþt	 	d¡|	d< qâW x|	d st	 	d¡|	d< qW |	d |	d krÎd|	d< d |	d< tdƒ qÎW x|	d sltdƒ|	d< qRW |  |	¡\}}|dkr–t d||¡ nt d¡ t d¡ nT|d kr dd!i}	d|	d< x|	d sâtd"ƒ|	d< qÈW |  |	¡\}}t d||¡ d S )#NÚ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 (%s): %séÈ   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Ú2z:actionÚuserÚnamer   ZemailZconfirmz
 Confirm: z!Password and confirm don't match!z
   EMail: 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Úannouncer   ÚINFOÚinputÚprintÚgetpassr"   r#   ZHTTPPasswordMgrÚparseZurlparser   Zadd_passwordr   r&   r'   r   Z_get_rc_fileÚlowerZ_store_pypircr$   )
r   Úchoicer   r   ÚchoicesÚauthZhostr(   r)   Údatar
   r
   r   r   c   sŒ    











zregister.send_metadatac             C   s   | j j}|d| ¡ | ¡ | ¡ | ¡ | ¡ | ¡ | ¡ | 	¡ | 
¡ | ¡ | ¡ | ¡ | ¡ | ¡ | ¡ dœ}|d s„|d s„|d rŒd|d< |S )Nz1.0)z:actionÚmetadata_versionr3   ÚversionZsummaryZ	home_pageZauthorZauthor_emailÚlicenseÚdescriptionÚkeywordsÚplatformr   Zdownload_urlÚprovidesÚrequiresÚ	obsoletesrG   rH   rI   z1.1rA   )r   ZmetadataZget_nameZget_versionZget_descriptionZget_urlZget_contactZget_contact_emailZget_licenceZget_long_descriptionZget_keywordsZget_platformsZget_classifiersZget_download_urlZget_providesZget_requiresZget_obsoletes)r   ÚactionÚmetar@   r
   r
   r   r'   Ý   s*    zregister.build_post_dataNc          
   C   sü  d|kr$|   d|d | jf tj¡ d}d| }|d }t ¡ }xŽ| ¡ D ]‚\}}t|ƒtg ƒtdƒfkrp|g}xZ|D ]R}t|ƒ}| 	|¡ | 	d| ¡ | 	d¡ | 	|¡ |rv|d	 d
krv| 	d¡ qvW qJW | 	|¡ | 	d¡ | 
¡  d¡}d| tt|ƒƒdœ}	tj | j||	¡}
tj tjj|d¡}d}y| |
¡}W nx tjjk
rˆ } z"| jrl|j ¡ }|j|jf}W d d }~X Y nJ tjjk
rº } zdt|ƒf}W d d }~X Y nX | jrÎ|  |¡}d}| jrød d|df¡}|   |tj¡ |S )Nr3   zRegistering %s to %sz3--------------GHSKFJDLGDS7543FJKLFHRE75642756743254z
--z--r
   z*
Content-Disposition: form-data; name="%s"z

éÿÿÿÿúÚ
zutf-8z/multipart/form-data; boundary=%s; charset=utf-8)zContent-typezContent-length)Zpassword_mgrr,   iô  )r-   ZOKzK---------------------------------------------------------------------------)r6   r   r   r7   ÚioÚStringIOÚitemsÚtypeÚstrÚwriteÚgetvalueÚencodeÚlenr"   r#   ZRequestZbuild_openerZHTTPBasicAuthHandlerÚopenÚerrorZ	HTTPErrorZshow_responseÚfpÚreadr(   ÚmsgZURLErrorr%   Újoin)r   r@   r?   ÚboundaryZsep_boundaryZend_boundaryZbodyÚkeyÚvalueZheadersZreqÚopenerr)   Úer\   r
   r
   r   r&   ù   sV    







zregister.post_to_server)N)Ú__name__Ú
__module__Ú__qualname__rD   r   Zuser_optionsZboolean_optionsZsub_commandsr   r   r   r   r   r   r   r   r'   r&   r
   r
   r
   r   r      s"   
zr   )r:   rO   Zurllib.parser"   Zurllib.requestÚwarningsr   Zdistutils.corer   Zdistutils.errorsZ	distutilsr   r   r
   r
   r
   r   Ú<module>   s   