a
    e0h                     @   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}| d|S )Nstrictsurrogateescapezutf-8)r   PY2encode)serrors r   L/var/www/auris/lib/python3.9/site-packages/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])zshow-responseNz&display full response text from server)zupload-dir=Nzdirectory to uploadc                 C   s"   | j d u rtddD ]} dS 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 |  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d u rN|  r0| d}|j| _q`| d}tj	|j
d| _n| d | j| _d| jv rttd | 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 |d}z| | j t| jD ]x\}}}|| jkrP|sPd}t|| j |D ]H}tj||}|t	| jd  
tjj}	tj|	|}
|||
 qTq&W |  n
|  0 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fullrelativedestr   r   r   create_zipfileK   s    zupload_docs.create_zipfilec              	   C   sr   |   D ]}| | qt }| jj }tj	|d| }z"| 
| | | W t| nt| 0 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_dirr8   r3   r   r   r   run[   s    
zupload_docs.runc                 c   s   | \}}d| }t |ts |g}|D ]f}t |trL|d|d  7 }|d }nt|}|V  t|V  dV  |V  |r$|dd  dkr$dV  q$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|| }tj|}t||}	d|d }
d	|	|
fS )	z=
        Build up the MIME payload for the POST data
        s3   --------------GHSKFJDLGDS7543FJKLFHRE75642756743254s   
--s   --rK   )rP   z multipart/form-data; boundary=%sascii    )
	functoolspartialrU   mapitems	itertoolschainfrom_iterabledecoder"   )clsdataboundaryrP   Zend_boundaryZ	end_itemsZbuilderZpart_groupspartsZ
body_itemscontent_typer   r   r   _build_multipart}   s    zupload_docs._build_multipartc              
   C   s^  t |d}| }W d    n1 s(0    Y  | jj}d| tj||fd}t| j	d | j
 }t|}tjs|d}d| }| |\}}	d| j }
| |
tj tj| j\}}}}}}|s|s|rJ |dkrt|}n"|d	krt|}ntd
| d}zZ|  |d| |	}|d| |dtt| |d| |   |!| W n< t"j#y } z | t|tj$ W Y d }~d S d }~0 0 |% }|j&dkrd|j&|j'f }
| |
tj nb|j&dkr$|(d}|d u rd|  }d| }
| |
tj nd|j&|j'f }
| |
tj$ | j)rZt*d| d d S )NrbZ
doc_upload)z:actionr8   content:rV   zBasic zSubmitting documentation to %shttphttpszunsupported schema  POSTzContent-typezContent-lengthAuthorization   zServer response (%s): %si-  ZLocationzhttps://pythonhosted.org/%s/zUpload successful. Visit %szUpload failed (%s): %szK---------------------------------------------------------------------------)+openreadrA   rB   rC   r    r!   basenamer   usernamepasswordr   r   r   r_   re   r%   r'   r   INFOr   parseurlparser   HTTPConnectionHTTPSConnectionAssertionErrorconnect
putrequest	putheaderstrr-   
endheaderssendsocketerrorERRORgetresponsestatusreason	getheadershow_responseprint)r   r2   frg   metara   credentialsauthbodyctmsgZschemanetlocurlparamsquery	fragmentsconnrd   er   locationr   r   r   rD      sd    &




zupload_docs.upload_fileN)__name__
__module____qualname__DEFAULT_REPOSITORYdescriptionr
   user_optionsboolean_optionsr   sub_commandsr   r   r<   rH   staticmethodrU   classmethodre   rD   r   r   r   r   r      s(   


r   )__doc__base64r   	distutilsr   distutils.errorsr   r    r   r)   r?   rE   r\   rX   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   pkg_resourcesr   r
   r   r   r   r   r   r   <module>   s    