
^Wc           @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z y d  d l m Z Wn! e k
 r d  d l	 m Z n Xd d l
 m Z d d l m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z e j e  Z d Z d Z d	 e f d
     YZ d S(   iN(   t   Threadi   (   t   DistlibException(   t   HTTPBasicAuthHandlert   Requestt   HTTPPasswordMgrt   urlparset   build_openert   string_types(   t   cached_propertyt   zip_dirt   ServerProxys   https://pypi.python.org/pypit   pypit   PackageIndexc           B   s   e  Z d  Z d Z d d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d d	  Z d d
  Z d d  Z d d d d d d  Z d   Z d d  Z d d  Z d d d  Z d   Z d   Z d d  Z RS(   sc   
    This class represents a package index compatible with PyPI, the Python
    Package Index.
    s.   ----------ThIs_Is_tHe_distlib_index_bouNdaRY_$c         C   s  | p	 t  |  _ |  j   t |  j  \ } } } } } } | sX | sX | sX | d k rn t d |  j   n  d |  _ d |  _ d |  _ d |  _	 d |  _
 t t j d  j } x` d D]X }	 y> t j |	 d g d | d	 | }
 |
 d
 k r |	 |  _ Pn  Wq t k
 rq Xq WWd QXd S(   s   
        Initialise an instance.

        :param url: The URL of the index. If not specified, the URL for PyPI is
                    used.
        t   httpt   httpss   invalid repository: %st   wt   gpg2t   gpgs	   --versiont   stdoutt   stderri    N(   s   https   https(   R   s   gpg(   t   DEFAULT_INDEXt   urlt   read_configurationR   R   t   Nonet   password_handlert   ssl_verifierR   t   gpg_homet	   rpc_proxyt   opent   ost   devnullt
   subprocesst
   check_callt   OSError(   t   selfR   t   schemet   netloct   patht   paramst   queryt   fragt   sinkt   st   rc(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   __init__$   s(    
!							c         C   s3   d d l  m } d d l m } |   } | |  S(   ss   
        Get the distutils command for interacting with PyPI configurations.
        :return: the command.
        i(   t   Distribution(   t   PyPIRCCommand(   t   distutils.coreR-   t   distutils.configR.   (   R"   R-   R.   t   d(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   _get_pypirc_command@   s    	c         C   sy   |  j    } |  j | _ | j   } | j d  |  _ | j d  |  _ | j d d  |  _ | j d |  j  |  _ d S(   s   
        Read the PyPI access configuration as supported by distutils, getting
        PyPI to do the acutal work. This populates ``username``, ``password``,
        ``realm`` and ``url`` attributes from the configuration.
        t   usernamet   passwordt   realmR   t
   repositoryN(   R2   R   R6   t   _read_pypirct   getR3   R4   R5   (   R"   t   ct   cfg(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyR   J   s    c         C   s0   |  j    |  j   } | j |  j |  j  d S(   s   
        Save the PyPI access configuration. You must have set ``username`` and
        ``password`` attributes before calling this method.

        Again, distutils is used to do the actual work.
        N(   t   check_credentialsR2   t   _store_pypircR3   R4   (   R"   R9   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   save_configurationY   s    
c         C   s   |  j  d k s |  j d k r- t d   n  t   } t |  j  \ } } } } } } | j |  j | |  j  |  j  t	 |  |  _
 d S(   sp   
        Check that ``username`` and ``password`` have been set, and raise an
        exception if not.
        s!   username and password must be setN(   R3   R   R4   R   R   R   R   t   add_passwordR5   R   R   (   R"   t   pmt   _R$   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyR;   e   s    	!c         C   s   |  j    | j   | j   } d | d <|  j | j   g   } |  j |  } d | d <|  j | j   g   } |  j |  S(   sq  
        Register a distribution on PyPI, using the provided metadata.

        :param metadata: A :class:`Metadata` instance defining at least a name
                         and version number for the distribution to be
                         registered.
        :return: The HTTP response received from PyPI upon submission of the
                request.
        t   verifys   :actiont   submit(   R;   t   validatet   todictt   encode_requestt   itemst   send_request(   R"   t   metadataR1   t   requestt   response(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   registerq   s    




c         C   sj   xY t  r[ | j   } | s Pn  | j d  j   } | j |  t j d | | f  q W| j   d S(   sr  
        Thread runner for reading lines of from a subprocess into a buffer.

        :param name: The logical name of the stream (used for logging only).
        :param stream: The stream to read from. This will typically a pipe
                       connected to the output stream of a subprocess.
        :param outbuf: The list to append the read lines to.
        s   utf-8s   %s: %sN(   t   Truet   readlinet   decodet   rstript   appendt   loggert   debugt   close(   R"   t   namet   streamt   outbufR*   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   _reader   s    		c         C   s   |  j  d d d g } | d k r- |  j } n  | rI | j d | g  n  | d k	 rn | j d d d g  n  t j   } t j j | t j j	 |  d  } | j d	 d
 d | d | | g  t
 j d d j |   | | f S(   s  
        Return a suitable command for signing a file.

        :param filename: The pathname to the file to be signed.
        :param signer: The identifier of the signer of the file.
        :param sign_password: The passphrase for the signer's
                              private key used for signing.
        :param keystore: The path to a directory which contains the keys
                         used in verification. If not specified, the
                         instance's ``gpg_home`` attribute is used instead.
        :return: The signing command as a list suitable to be
                 passed to :class:`subprocess.Popen`.
        s   --status-fdt   2s   --no-ttys	   --homedirs   --batchs   --passphrase-fdt   0s   .ascs   --detach-signs   --armors   --local-users   --outputs   invoking: %st    N(   R   R   R   t   extendt   tempfilet   mkdtempR   R%   t   joint   basenameRQ   RR   (   R"   t   filenamet   signert   sign_passwordt   keystoret   cmdt   tdt   sf(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   get_sign_command   s    %c   	      C   s
  i t  j d 6t  j d 6} | d k	 r6 t  j | d <n  g  } g  } t  j | |  } t d |  j d d | j | f  } | j   t d |  j d d | j | f  } | j   | d k	 r | j	 j
 |  | j	 j   n  | j   | j   | j   | j | | f S(   s  
        Run a command in a child process , passing it any input data specified.

        :param cmd: The command to run.
        :param input_data: If specified, this must be a byte string containing
                           data to be sent to the child process.
        :return: A tuple consisting of the subprocess' exit code, a list of
                 lines read from the subprocess' ``stdout``, and a list of
                 lines read from the subprocess' ``stderr``.
        R   R   t   stdint   targett   argsN(   R   t   PIPER   t   PopenR    RW   R   t   startR   Rh   t   writeRS   t   waitR^   t
   returncode(	   R"   Rd   t
   input_datat   kwargsR   R   t   pt   t1t   t2(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   run_command   s&    
$
$



c   
      C   se   |  j  | | | |  \ } } |  j | | j d   \ } } }	 | d k ra t d |   n  | S(   sR  
        Sign a file.

        :param filename: The pathname to the file to be signed.
        :param signer: The identifier of the signer of the file.
        :param sign_password: The passphrase for the signer's
                              private key used for signing.
        :param keystore: The path to a directory which contains the keys
                         used in signing. If not specified, the instance's
                         ``gpg_home`` attribute is used instead.
        :return: The absolute pathname of the file where the signature is
                 stored.
        s   utf-8i    s&   sign command failed with error code %s(   Rg   Rv   t   encodeR   (
   R"   R`   Ra   Rb   Rc   Rd   t   sig_fileR+   R   R   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt	   sign_file   s    	t   sdistt   sourcec         C   s  |  j    t j j |  s/ t d |   n  | j   | j   } d }	 | r |  j sj t	 j
 d  q |  j | | | |  }	 n  t | d   }
 |
 j   } Wd QXt j |  j   } t j |  j   } | j i d d 6d d 6| d	 6| d
 6| d 6| d 6 d t j j |  | f g } |	 rt |	 d   }
 |
 j   } Wd QX| j d t j j |	  | f  t j t j j |	   n  |  j | j   |  } |  j |  S(   s  
        Upload a release file to the index.

        :param metadata: A :class:`Metadata` instance defining at least a name
                         and version number for the file to be uploaded.
        :param filename: The pathname of the file to be uploaded.
        :param signer: The identifier of the signer of the file.
        :param sign_password: The passphrase for the signer's
                              private key used for signing.
        :param filetype: The type of the file being uploaded. This is the
                        distutils command which produced that file, e.g.
                        ``sdist`` or ``bdist_wheel``.
        :param pyversion: The version of Python which the release relates
                          to. For code compatible with any Python, this would
                          be ``source``, otherwise it would be e.g. ``3.2``.
        :param keystore: The path to a directory which contains the keys
                         used in signing. If not specified, the instance's
                         ``gpg_home`` attribute is used instead.
        :return: The HTTP response received from PyPI upon submission of the
                request.
        s   not found: %ss)   no signing program available - not signedt   rbNt   file_uploads   :actiont   1t   protcol_versiont   filetypet	   pyversiont
   md5_digestt   sha256_digestt   contentt   gpg_signature(   R;   R   R%   t   existsR   RC   RD   R   R   RQ   t   warningRy   R   t   readt   hashlibt   md5t	   hexdigestt   sha256t   updateR_   RP   t   shutilt   rmtreet   dirnameRE   RF   RG   (   R"   RH   R`   Ra   Rb   R   R   Rc   R1   Rx   t   ft	   file_dataR   R   t   filest   sig_dataRI   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   upload_file   s>    

		
c   
      C   s   |  j    t j j |  s/ t d |   n  t j j | d  } t j j |  si t d |   n  | j   | j | j	 } } t
 |  j   } d	 d | f d | f g } d | | f g } |  j | |  }	 |  j |	  S(
   s2  
        Upload documentation to the index.

        :param metadata: A :class:`Metadata` instance defining at least a name
                         and version number for the documentation to be
                         uploaded.
        :param doc_dir: The pathname of the directory which contains the
                        documentation. This should be the directory that
                        contains the ``index.html`` for the documentation.
        :return: The HTTP response received from PyPI upon submission of the
                request.
        s   not a directory: %rs
   index.htmls   not found: %rs   :actiont
   doc_uploadRT   t   versionR   (   s   :actionR   (   R;   R   R%   t   isdirR   R^   R   RC   RT   R   R	   t   getvalueRE   RG   (
   R"   RH   t   doc_dirt   fnRT   R   t   zip_datat   fieldsR   RI   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   upload_documentation'  s    

c         C   s|   |  j  d d d g } | d k r- |  j } n  | rI | j d | g  n  | j d | | g  t j d d j |   | S(	   s|  
        Return a suitable command for verifying a file.

        :param signature_filename: The pathname to the file containing the
                                   signature.
        :param data_filename: The pathname to the file containing the
                              signed data.
        :param keystore: The path to a directory which contains the keys
                         used in verification. If not specified, the
                         instance's ``gpg_home`` attribute is used instead.
        :return: The verifying command as a list suitable to be
                 passed to :class:`subprocess.Popen`.
        s   --status-fdRX   s   --no-ttys	   --homedirs   --verifys   invoking: %sRZ   N(   R   R   R   R[   RQ   RR   R^   (   R"   t   signature_filenamet   data_filenameRc   Rd   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   get_verify_commandC  s    c         C   sn   |  j  s t d   n  |  j | | |  } |  j |  \ } } } | d k rd t d |   n  | d k S(   s6  
        Verify a signature for a file.

        :param signature_filename: The pathname to the file containing the
                                   signature.
        :param data_filename: The pathname to the file containing the
                              signed data.
        :param keystore: The path to a directory which contains the keys
                         used in verification. If not specified, the
                         instance's ``gpg_home`` attribute is used instead.
        :return: True if the signature was verified, else False.
        s0   verification unavailable because gpg unavailablei    i   s(   verify command failed with error code %s(   i    i   (   R   R   R   Rv   (   R"   R   R   Rc   Rd   R+   R   R   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   verify_signature[  s    		c         C   s  | d k r" d } t j d  nM t | t t f  rF | \ } } n d } t t |    } t j d |  t | d  } |  j	 t
 |   } z | j   }	 d }
 d } d } d } d |	 k r t |	 d	  } n  | r | | |
 |  n  xy t rp| j |
  } | sPn  | t |  7} | j |  | rJ| j |  n  | d
 7} | r | | |
 |  q q WWd | j   XWd QX| d k r| | k  rt d | | f   n  | r| j   } | | k rt d | | | | f   n  t j d |  n  d S(   s  
        This is a convenience method for downloading a file from an URL.
        Normally, this will be a file from the index, though currently
        no check is made for this (i.e. a file can be downloaded from
        anywhere).

        The method is just like the :func:`urlretrieve` function in the
        standard library, except that it allows digest computation to be
        done during download and checking that the downloaded data
        matched any expected value.

        :param url: The URL of the file to be downloaded (assumed to be
                    available via an HTTP GET request).
        :param destfile: The pathname where the downloaded file is to be
                         saved.
        :param digest: If specified, this must be a (hasher, value)
                       tuple, where hasher is the algorithm used (e.g.
                       ``'md5'``) and ``value`` is the expected value.
        :param reporthook: The same as for :func:`urlretrieve` in the
                           standard library.
        s   No digest specifiedR   s   Digest specified: %st   wbi    ii    s   content-lengths   Content-Lengthi   Ns1   retrieval incomplete: got only %d out of %d bytess.   %s digest mismatch for %s: expected %s, got %ss   Digest verified: %s(   R   RQ   RR   t
   isinstancet   listt   tuplet   getattrR   R   RG   R   t   infot   intRL   R   t   lenRn   R   RS   R   R   (   R"   R   t   destfilet   digestt
   reporthookt   digestert   hashert   dfpt   sfpt   headerst	   blocksizet   sizeR   t   blocknumt   blockt   actual(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   download_filet  sV    	
c         C   sW   g  } |  j  r" | j |  j   n  |  j r> | j |  j  n  t |   } | j |  S(   s   
        Send a standard library :class:`Request` to PyPI and return its
        response.

        :param req: The request to send.
        :return: The HTTP response from PyPI (a standard library HTTPResponse).
        (   R   RP   R   R   R   (   R"   t   reqt   handlerst   opener(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyRG     s    		c         C   s<  g  } |  j  } xy | D]q \ } } t | t t f  sC | g } n  xA | D]9 } | j d | d | j d  d | j d  f  qJ Wq WxG | D]? \ } }	 }
 | j d | d | |	 f j d  d |
 f  q W| j d | d d f  d j |  } d | } i | d 6t t |   d	 6} t	 |  j
 | |  S(
   s&  
        Encode fields and files for posting to an HTTP server.

        :param fields: The fields to send as a list of (fieldname, value)
                       tuples.
        :param files: The files to send as a list of (fieldname, filename,
                      file_bytes) tuple.
        s   --s)   Content-Disposition: form-data; name="%s"s   utf-8t    s8   Content-Disposition: form-data; name="%s"; filename="%s"s   
s   multipart/form-data; boundary=s   Content-types   Content-length(   t   boundaryR   R   R   R[   Rw   R^   t   strR   R   R   (   R"   R   R   t   partsR   t   kt   valuest   vt   keyR`   t   valuet   bodyt   ctR   (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyRE     s4    	
c         C   sb   t  | t  r i | d 6} n  |  j d  k rI t |  j d d |  _ n  |  j j | | p^ d  S(   NRT   t   timeoutg      @t   and(   R   R   R   R   R
   R   t   search(   R"   t   termst   operator(    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyR     s
    N(   t   __name__t
   __module__t   __doc__R   R   R,   R2   R   R=   R;   RK   RW   Rg   Rv   Ry   R   R   R   R   R   RG   RE   R   (    (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyR      s*   	
					#8	M		+(   R   t   loggingR   R   R   R\   t	   threadingR    t   ImportErrort   dummy_threadingR   R   t   compatR   R   R   R   R   R   t   utilR   R	   R
   t	   getLoggerR   RQ   R   t   DEFAULT_REALMt   objectR   (    (    (    sj   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/distlib/index.pyt   <module>   s    .