
|_k                 @   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surrogateescapestrictzutf-8)r   PY3encode)serrors r   D/tmp/pip-build-3_sjtvrs/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 d  t j |   |  j d  k r |  j   rS |  j d  } | j |  _ q |  j d  } t	 j
 j | j d  |  _ n |  j d  |  j |  _ |  j d |  j  d  S)Nz3Upload_docs command is deprecated. Use RTD instead.r   buildZdocsr   zUsing upload directory %s)r   warnr
   finalize_optionsr   r   get_finalized_commandZbuilder_target_dirr    ospathjoin
build_baseensure_dirname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 rp | rp t d |  j   xp | D]h } t j j | |  } | t	 |  j  d   j
 t j j  } t j j | |  }	 | j | |	  qw 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namefullZrelativedestr   r   r   create_zipfileJ   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:   r6   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 | } | 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
        s3   --------------GHSKFJDLGDS7543FJKLFHRE75642756743254s   
--s   --s   
rO   z multipart/form-data; boundary=%s    )		functoolspartialrT   mapitems	itertoolschainfrom_iterabler'   )clsdataboundaryrO   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 r | j d  } d | } |  j |  \ } }	 |  j d	 |  j t j  t j j |  j  \ }
 } } } } } | r| r| st  |
 d
 k r8t j |  } n. |
 d k rVt j |  } n t d |
   d } yw | j   | j d |  |	 } | j d |  | j d t t |    | j d |  | j    | j! |  WnE t" j# k
 r*} z" |  j t |  t j$  d  SWYd  d  } ~ Xn X| j%   } | j& d k rl|  j d | j& | j' f t j  nw | j& d k r| j( d  } | d  k rd | j   } |  j d | t j  n# |  j d | j& | j' f t j$  |  j) r
t* d d | j   d d  d  S)Nrbz:actionZ
doc_uploadr:   content:asciiz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readrB   rC   rD   r%   r&   basenamer   usernamepasswordr   r   r   decoderb   r*   
repositoryr   INFOr   parseurlparseAssertionErrorr   HTTPConnectionHTTPSConnectionconnect
putrequest	putheaderstrr0   
endheaderssendsocketerrorERRORgetresponsestatusreason	getheadershow_responseprint)r   r5   frd   metar^   credentialsauthbodyctZschemanetlocurlparamsqueryZ	fragmentsconnra   er   locationr   r   r   rE      s`    	

'


	zupload_docs.upload_file)r   Nr   )r   Nr   )__name__
__module____qualname__DEFAULT_REPOSITORYdescriptionr
   user_optionsboolean_optionsr   sub_commandsr   r#   r=   rI   staticmethodrT   classmethodrb   rE   r   r   r   r   r      s"    		r   )__doc__base64r   	distutilsr   Zdistutils.errorsr   r%   r   r,   r@   rF   rZ   rV   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   pkg_resourcesr   r
   r   r   r   r   r   r   <module>   s    