
(h                 @   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 Gd 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strictsurrogateescapezutf-8)r   PY2encode)serrors r   D/tmp/pip-build-kmztzv_d/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 SWd  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 rs |  j   rF |  j d  } | j |  _ q |  j d  } t j j	 | j
 d  |  _ n |  j d  |  j |  _ d |  j k r t 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  x t j |  j  D] \ } } } | |  j k rv | rv d } t | |  j   xp | D]h } t j j | |  } | t	 |  j  d   j
 t j j  }	 t j j |	 |  }
 | j | |
  q} Wq8 WWd  | j   Xd  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   s   x! |  j    D] } |  j |  q Wt j   } |  j j j   } t j j	 | d |  } z |  j
 |  |  j |  Wd  t j |  Xd  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 } x | D]{ } t  | t  ri | d | d 7} | d } n t |  } | Vt |  Vd V| V| r5 | d d   d k r5 d Vq5 Wd  S)	Nz*
Content-Disposition: form-data; name="%s"z; filename="%s"r   r	   s   

s   s   
)
isinstancelisttupler   )itemsep_boundarykeyvaluestitlevaluer   r   r   _build_parti   s    
	zupload_docs._build_partc             C   s   d } d | j  d  } | d } | d f } t j |  j d | } t | | j    } t j j |  } t j | |  }	 d | }
 d j	 |	  |
 f S)	z=
        Build up the MIME payload for the POST data
        z3--------------GHSKFJDLGDS7543FJKLFHRE75642756743254s   
--asciis   --s   
rQ   z multipart/form-data; boundary=%s    )
r   	functoolspartialrV   mapitems	itertoolschainfrom_iterabler&   )clsdataboundaryrQ   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   } | j   } Wd  QRX|  j j } d d d | j   d t j j |  | f i } t |  j	 d |  j
  } t |  } t j s | j d  } d | } |  j |  \ } }	 d	 |  j }
 |  j |
 t j  t j j |  j  \ } } } } } } | r| r| s t  | d
 k r>t j |  } n. | d k r\t j |  } n t d |   d } yw | j   | j d |  |	 } | j d |  | j d t t |    | j d |  | j    | j! |  WnE t" j# k
 r0} z" |  j t |  t j$  d  SWYd  d  } ~ Xn X| j%   } | j& d k rxd | j& | j' f }
 |  j |
 t j  n | j& d k r| j( d  } | d  k rd | j   } d | }
 |  j |
 t j  n) d | j& | j' f }
 |  j |
 t j$  |  j) r"t* d d | j   d d  d  S)Nrbz:actionZ
doc_uploadr<   content:rW   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): %s-K   )+openreadrD   rE   rF   r$   r%   basenamer   usernamepasswordr   r   r   decodere   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   frg   metara   credentialsauthbodyctmsgZschemanetlocurlparamsqueryZ	fragmentsconnrd   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   staticmethodrV   classmethodre   rG   r   r   r   r   r      s"    		r   )__doc__base64r   	distutilsr   Zdistutils.errorsr   r$   r   r-   rB   rH   r]   rY   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   pkg_resourcesr   r
   r   r   r   r   r   r   <module>   s    