3
#+]                 @   s   d Z ddlmZ ddlmZ ddl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ZddlmZ ddlmZmZ ddlmZ d	d
lmZ dd ZG dd deZdS )zpupload_docs

Implements a Distutils 'upload_docs' subcommand (upload documentation to
PyPI's pythonhosted.org).
    )standard_b64encode)log)DistutilsOptionErrorN)six)http_clienturllib)iter_entry_points   )uploadc             C   s   t jr
dnd}| jd|S )Nsurrogateescapestrictzutf-8)r   PY3encode)serrors r   F/tmp/pip-install-652y2hkr/setuptools/setuptools/command/upload_docs.py_encode   s    r   c               @   s   e Zd ZdZdZdddej fddgZejZdd ZdefgZ	dd Z
dd Zdd Zdd Zedd Zedd Zdd ZdS )upload_docszhttps://pypi.python.org/pypi/zUpload documentation to PyPIzrepository=rzurl of repository [default: %s]show-responseN&display full response text from serverupload-dir=directory to uploadc             C   s$   | j d kr xtddD ]}dS W d S )Nzdistutils.commandsbuild_sphinxT)
upload_dirr   )selfepr   r   r   
has_sphinx/   s    
zupload_docs.has_sphinxr   c             C   s   t j|  d | _d | _d S )N)r
   initialize_optionsr   
target_dir)r   r   r   r   r   6   s    
zupload_docs.initialize_optionsc             C   s   t j|  | jd krN| j r0| jd}|j| _q`| jd}tjj	|j
d| _n| jd | j| _d| jkrttjd | jd| j  d S )Nr   buildZdocsr   zpypi.python.orgz3Upload_docs command is deprecated. Use RTD instead.zUsing upload directory %s)r
   finalize_optionsr   r   get_finalized_commandZbuilder_target_dirr    ospathjoin
build_baseensure_dirname
repositoryr   warnannounce)r   r   r!   r   r   r   r"   ;   s    







zupload_docs.finalize_optionsc             C   s   t j|d}z| j| j xtj| jD ]~\}}}|| jkrT| rTd}t|| j xP|D ]H}tjj||}|t	| jd  j
tjj}	tjj|	|}
|j||
 qZW q(W W d |j  X d S )Nwz'no files found in upload directory '%s')zipfileZipFilemkpathr    r$   walkr   r%   r&   lenlstripsepwriteclose)r   filenamezip_filerootdirsfilestmplnamefullZrelativedestr   r   r   create_zipfileK   s    
zupload_docs.create_zipfilec             C   sl   x| j  D ]}| j| q
W tj }| jjj }tjj	|d| }z| j
| | j| W d tj| X d S )Nz%s.zip)get_sub_commandsrun_commandtempfilemkdtempdistributionmetadataget_namer$   r%   r&   r?   upload_fileshutilrmtree)r   cmd_nameZtmp_dirr<   r7   r   r   r   run[   s    
zupload_docs.runc             c   s   | \}}d| }t |ts |g}xn|D ]f}t |trN|d|d  7 }|d }nt|}|V  t|V  dV  |V  |r&|dd  dkr&dV  q&W d S )	Nz*
Content-Disposition: form-data; name="%s"z; filename="%s"r   r	   s   

      
)
isinstancelisttupler   )itemsep_boundarykeyvaluestitlevaluer   r   r   _build_parti   s    




zupload_docs._build_partc             C   sn   d}d| }|d }|df}t j| j|d}t||j }tjj|}tj||}	d|jd }
dj	|	|
fS )	z=
        Build up the MIME payload for the POST data
        s3   --------------GHSKFJDLGDS7543FJKLFHRE75642756743254s   
--s   --rM   )rS   z multipart/form-data; boundary=%sascii    )
	functoolspartialrX   mapitems	itertoolschainfrom_iterabledecoder&   )clsdataboundaryrS   Zend_boundaryZ	end_itemsZbuilderZpart_groupspartsZ
body_itemscontent_typer   r   r   _build_multipart}   s    zupload_docs._build_multipartc             C   sP  t |d}|j }W d Q R X | jj}d|j tjj||fd}t| j	d | j
 }t|}tjrn|jd}d| }| j|\}}	d| j }
| j|
tj tjj| j\}}}}}}| r| r| st|dkrtj|}n |d	krtj|}ntd
| d}yZ|j  |jd| |	}|jd| |jdtt| |jd| |j   |j!| W n6 t"j#k
r } z| jt|tj$ d S d }~X nX |j% }|j&dkrd|j&|j'f }
| j|
tj nb|j&dkr|j(d}|d krd|j  }d| }
| j|
tj nd|j&|j'f }
| j|
tj$ | j)rLt*dd |j dd  d S )NrbZ
doc_upload)z:actionr<   content:rY   zBasic zSubmitting documentation to %shttphttpszunsupported schema  POSTzContent-typezContent-lengthAuthorization   zServer response (%s): %si-  Locationzhttps://pythonhosted.org/%s/zUpload successful. Visit %szUpload failed (%s): %s-K   )+openreadrD   rE   rF   r$   r%   basenamer   usernamepasswordr   r   r   rb   rh   r)   r+   r   INFOr   parseurlparseAssertionErrorr   HTTPConnectionHTTPSConnectionconnect
putrequest	putheaderstrr1   
endheaderssendsocketerrorERRORgetresponsestatusreason	getheadershow_responseprint)r   r6   frj   metard   credentialsauthbodyctmsgZschemanetlocurlparamsqueryZ	fragmentsconnrg   er   locationr   r   r   rG      s`    



zupload_docs.upload_file)r   Nr   )r   Nr   )__name__
__module____qualname__DEFAULT_REPOSITORYdescriptionr
   user_optionsboolean_optionsr   sub_commandsr   r"   r?   rK   staticmethodrX   classmethodrh   rG   r   r   r   r   r      s"   
 
r   )__doc__base64r   	distutilsr   distutils.errorsr   r$   r   r-   rB   rH   r_   r[   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   pkg_resourcesr   r
   r   r   r   r   r   r   <module>   s    