
(h-                 @   s   d  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 Gd d   d e	  Z d S)	zhdistutils.command.register

Implements the Distutils 'register' command (register with the repository).
    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list-classifiersN list the valid Trove classifiersstrictBWill stop the registering if the meta-data are not fully compliantverifycheckc             C   s   d S)NT )selfr   r   L/tmp/pip-build-kmztzv_d/setuptools/setuptools/_distutils/command/register.py<lambda>   s    zregister.<lambda>c             C   s#   t  j |   d |  _ d |  _ d  S)Nr   )r   initialize_optionslist_classifiersr	   )r   r   r   r   r      s    	zregister.initialize_optionsc             C   s<   t  j |   d d |  j f d d i } | |  j j d <d  S)Nr	   r   restructuredtext   r   )zregisterr   )r   finalize_optionsr	   distributioncommand_options)r   check_optionsr   r   r   r   $   s    zregister.finalize_optionsc             C   sr   |  j    |  j   x! |  j   D] } |  j |  q! W|  j rN |  j   n  |  j rd |  j   n
 |  j   d  S)N)	r   _set_configget_sub_commandsrun_commanddry_runverify_metadatar   classifierssend_metadata)r   cmd_namer   r   r   run+   s    

		zregister.runc             C   sL   t  d t  |  j j d  } | j   |  j | _ d | _ | j   d S)zDeprecated API.zddistutils.command.register.check_metadata is deprecated,               use the check command insteadr   r   N)r   PendingDeprecationWarningr   get_command_objensure_finalizedr	   r   r!   )r   r   r   r   r   check_metadata:   s    

	zregister.check_metadatac             C   s   |  j    } | i  k rX | d |  _ | d |  _ | d |  _ | d |  _ d |  _ nO |  j d |  j f k r t d |  j   |  j d k r |  j |  _ d |  _ d	 S)
z: Reads 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   D   s    zregister._set_configc             C   s9   |  j  d } t j j |  } t j |  j |   d S)z8 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   U   s    zregister.classifiersc             C   s5   |  j  |  j d   \ } } t j d | |  d S)zF Send the metadata to the package index server to be checked.
        r   zServer response (%s): %sN)post_to_serverbuild_post_datar   r3   )r   coderesultr   r   r   r   \   s    zregister.verify_metadatac       
      C   s  |  j  r$ d } |  j } |  j } n d } d } } d j   } xQ | | k r |  j d t j  t   } | sz d } qC | | k rC t d  qC W| d k rx | s t d  } q Wx | s t	 j	 d  } q Wt
 j j   } t
 j j |  j  d	 } | j |  j | | |  |  j |  j d
  |  \ } } |  j d | | f t j  | d k r|  j  rx| |  j _ q|  j d t j  |  j d |  j   t j  d } x. | j   d k rt d  } | sd } qW| j   d k r|  j | |  n| d k rkd d i }	 d |	 d <|	 d <|	 d <d |	 d <x |	 d s`t d  |	 d <qCWx |	 d |	 d k rx! |	 d st	 j	 d  |	 d <q{Wx! |	 d st	 j	 d  |	 d <qW|	 d |	 d k rdd |	 d <d |	 d <t d  qdWx |	 d st d  |	 d <qW|  j |	  \ } } | d k rNt j d | |  qt j d  t j d   nk | d! k rd d" i }	 d |	 d <x |	 d st d#  |	 d <qW|  j |	  \ } } t j 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 4zWe 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   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'   emailNZ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getpassr0   r1   HTTPPasswordMgrparseurlparser(   add_passwordr)   r7   r8   r   _get_rc_filelower_store_pypircr3   )
r   choicer&   r'   choicesauthhostr9   r:   datar   r   r   r   c   s    		
				
		
	




zregister.send_metadatac          "   C   s   |  j  j } d | d d d | j   d | j   d | j   d | j   d | j   d	 | j   d
 | j   d | j	   d | j
   d | j   d | j   d | j   d | j   d | j   d | j   i } | d s | d s | d r d | d <| S)Nz:actionmetadata_versionz1.0rE   versionsummaryZ	home_pageauthorauthor_emaillicensedescriptionkeywordsplatformr   download_urlprovidesrequires	obsoletesz1.1)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metarY   r   r   r   r8      s*    
zregister.build_post_datac             C   s  d | k r0 |  j  d | d |  j f t j  d } d | } | d } t j   } x | j   D] \ } } t |  t g   t f   f k r | g } xu | D]m } t |  } | j	 |  | j	 d |  | j	 d  | j	 |  | r | d d	 k r | j	 d
  q Wqc W| j	 |  | j	 d
  | j
   j d  } d d | d t t |   i }	 t j j |  j | |	  }
 t j j t j j d |   } d } y | j |
  } Wn t j j k
 r} z/ |  j r| j j   } | j | j f } WYd d } ~ Xn[ t j j k
 rQ} z d t |  f } WYd d } ~ Xn X|  j rj|  j |  } d } |  j rd
 j d d | d d f  } |  j  | t j  | S)zC Post a query to the server, and return a string response.
        rE   zRegistering %s to %sz3--------------GHSKFJDLGDS7543FJKLFHRE75642756743254z
--z--z*
Content-Disposition: form-data; name="%s"z

r   
zutf-8zContent-typez/multipart/form-data; boundary=%s; charset=utf-8zContent-lengthpassword_mgrr=   Ni  r?   OK-K   )r?   zOK)rI   r(   r   rJ   ioStringIOitemstypestrwritegetvalueencodelenr0   r1   Requestbuild_openerHTTPBasicAuthHandleropenerror	HTTPErrorshow_responsefpreadr9   msgURLErrorr4   join)r   rY   rW   boundaryZsep_boundaryZend_boundarybodykeyvalueheadersreqopenerr:   er   r   r   r   r7      sV    



$	
		$%		 zregister.post_to_server)r   Nr   )zstrictNr
   )__name__
__module____qualname__r`   r   user_optionsboolean_optionssub_commandsr   r   r!   r%   r   r   r   r   r8   r7   r   r   r   r   r      s&     

zr   )__doc__rM   r   urllib.parser0   urllib.requestwarningsr   distutils.corer   Zdistutils.errors	distutilsr   r   r   r   r   r   <module>   s   
