3
g~D                 @   s  d 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Z	ddl
Z	ddlZddlmZ ddlmZmZmZmZmZmZmZmZ ddlmZm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
l$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3m4Z4 ej5e6Z7ej8j9j:Z;ee<e<f Z=e<ee< dddZ>G dd de?Z@eddddZAG dd de?ZBe<e'ddddZCe<e'eddd ZDe=ee< d!d"d#ZEe;e<e<d$d%d&ZFe<e<d'd(d)ZGe<e<d'd*d+ZHejId,ejJZKe<eLe<d-d.d/ZMe<e<dd0d1ZNe;e<e<ee# d2d3d4ZOG d5d6 d6ZPed7gee# f ed7gee# f d8d9d:ZQeQd7ee# d;d<d=ZRG d>d7 d7ZSdNe#ee<e?f eedO  dd@dAdBZTdPeeLeSdDdEdFZUdQe#ee' ed7 dGdHdIZVG dJdK dKeZWG dLdM dMZXdS )RzO
The main purpose of this module is to expose LinkCollector.collect_sources().
    N)Values)CallableIterableListMutableMapping
NamedTupleOptionalSequenceUnion)html5librequests)Response)
RetryErrorSSLError)NetworkConnectionError)Link)SearchScope)
PipSession)raise_for_status)is_archive_file)pairwiseredact_auth_from_url)vcs   )CandidatesFromPage
LinkSourcebuild_source)urlreturnc             C   s6   x0t jD ]&}| j j|r| t| dkr|S qW dS )zgLook for VCS schemes in the URL.

    Returns the matched VCS scheme, or None if there's no match.
    z+:N)r   Zschemeslower
startswithlen)r   scheme r#   [/var/www/tester-filtro-web/env/lib/python3.6/site-packages/pip/_internal/index/collector.py_match_vcs_scheme0   s    r%   c                   s&   e Zd Zeedd fddZ  ZS )_NotHTMLN)content_typerequest_descr   c                s   t  j|| || _|| _d S )N)super__init__r'   r(   )selfr'   r(   )	__class__r#   r$   r*   <   s    z_NotHTML.__init__)__name__
__module____qualname__strr*   __classcell__r#   r#   )r,   r$   r&   ;   s   r&   )responser   c             C   s.   | j jdd}|j jds*t|| jjdS )zCheck the Content-Type header to ensure the response contains HTML.

    Raises `_NotHTML` if the content type is not text/html.
    zContent-Type z	text/htmlN)headersgetr   r    r&   requestmethod)r2   r'   r#   r#   r$   _ensure_html_headerB   s    r8   c               @   s   e Zd ZdS )_NotHTTPN)r-   r.   r/   r#   r#   r#   r$   r9   L   s   r9   )r   sessionr   c             C   sF   t jj| \}}}}}|dkr$t |j| dd}t| t| dS )zSend a HEAD request to the URL, and ensure the response contains HTML.

    Raises `_NotHTTP` if the URL is not available for a HEAD request, or
    `_NotHTML` if the content type is not text/html.
    httphttpsT)allow_redirectsN>   r<   r;   )urllibparseurlsplitr9   headr   r8   )r   r:   r"   netlocpathqueryfragmentrespr#   r#   r$   _ensure_html_responseP   s    rG   c             C   sR   t t| jrt| |d tjdt|  |j| dddd}t| t	| |S )a  Access an HTML page with GET, and return the response.

    This consists of three parts:

    1. If the URL looks suspiciously like an archive, send a HEAD first to
       check the Content-Type is HTML, to avoid downloading a large file.
       Raise `_NotHTTP` if the content type cannot be determined, or
       `_NotHTML` if it is not HTML.
    2. Actually perform the request. Raise HTTP exceptions on network failures.
    3. Check the Content-Type header to make sure we got HTML, and raise
       `_NotHTML` otherwise.
    )r:   zGetting page %sz	text/htmlz	max-age=0)AcceptzCache-Control)r4   )
r   r   filenamerG   loggerdebugr   r5   r   r8   )r   r:   rF   r#   r#   r$   _get_html_response`   s    rL   )r4   r   c             C   s2   | r.d| kr.t j| d \}}d|kr.|d S dS )z=Determine if we have any encoding information in our headers.zContent-TypecharsetN)cgiparse_header)r4   r'   paramsr#   r#   r$   _get_encoding_from_headers   s
    rQ   )documentpage_urlr   c             C   s.   x(| j dD ]}|jd}|dk	r|S qW |S )a  Determine the HTML document's base URL.

    This looks for a ``<base>`` tag in the HTML document. If present, its href
    attribute denotes the base URL of anchor tags in the document. If there is
    no such tag (or if it does not have a valid href attribute), the HTML
    file's URL is used as the base URL.

    :param document: An HTML document representation. The current
        implementation expects the result of ``html5lib.parse()``.
    :param page_url: The URL of the HTML document.
    z.//basehrefN)findallr5   )rR   rS   baserT   r#   r#   r$   _determine_base_url   s
    
rW   )partr   c             C   s   t jjt jj| S )zP
    Clean a "part" of a URL path (i.e. after splitting on "@" characters).
    )r>   r?   quoteunquote)rX   r#   r#   r$   _clean_url_path_part   s    r[   c             C   s   t jjt jj| S )z
    Clean the first part of a URL path that corresponds to a local
    filesystem path (i.e. the first part after splitting on "@" characters).
    )r>   r6   pathname2urlurl2pathname)rX   r#   r#   r$   _clean_file_url_path   s    
r^   z(@|%2F))rC   is_local_pathr   c             C   sb   |r
t }nt}tj| }g }x:ttj|dgD ]$\}}|j|| |j|j  q0W dj	|S )z*
    Clean the path portion of a URL.
    r3   )
r^   r[   _reserved_chars_resplitr   	itertoolschainappendupperjoin)rC   r_   Z
clean_funcpartsZcleaned_partsZto_cleanreservedr#   r#   r$   _clean_url_path   s    
ri   c             C   s6   t jj| }|j }t|j|d}t jj|j|dS )z
    Make sure a link is fully quoted.
    For example, if ' ' occurs in the URL, it will be replaced with "%20",
    and without double-quoting other characters.
    )r_   )rC   )r>   r?   urlparserB   ri   rC   
urlunparse_replace)r   resultr_   rC   r#   r#   r$   _clean_link   s    rn   )anchorrS   base_urlr   c             C   sL   | j d}|sdS ttjj||}| j d}| j d}t||||d}|S )zJ
    Convert an anchor element in a simple repository page to a Link.
    rT   Nzdata-requires-pythonzdata-yanked)
comes_fromrequires_pythonyanked_reason)r5   rn   r>   r?   urljoinr   )ro   rS   rp   rT   r   Z	pyrequirers   linkr#   r#   r$   _create_link_from_element   s    


rv   c               @   s:   e Zd ZdddddZeedddZed	d
dZdS )CacheablePageContentHTMLPageN)pager   c             C   s   |j s
t|| _d S )N)cache_link_parsingAssertionErrorry   )r+   ry   r#   r#   r$   r*     s    
zCacheablePageContent.__init__)otherr   c             C   s   t |t| o| jj|jjkS )N)
isinstancetypery   r   )r+   r|   r#   r#   r$   __eq__  s    zCacheablePageContent.__eq__)r   c             C   s   t | jjS )N)hashry   r   )r+   r#   r#   r$   __hash__  s    zCacheablePageContent.__hash__)	r-   r.   r/   r*   objectboolr   intr   r#   r#   r#   r$   rw     s   rw   rx   )fnr   c                sL   t jddttt d fddt j dtt d fdd	}|S )
z
    Given a function that parses an Iterable[Link] from an HTMLPage, cache the
    function's result (keyed by CacheablePageContent), unless the HTMLPage
    `page` has `page.cache_link_parsing == False`.
    N)maxsize)cacheable_pager   c                s   t  | jS )N)listry   )r   )r   r#   r$   wrapper  s    z'with_cached_html_pages.<locals>.wrapperrx   )ry   r   c                s   | j rt| S t | S )N)rz   rw   r   )ry   )r   r   r#   r$   wrapper_wrapper  s    z/with_cached_html_pages.<locals>.wrapper_wrapper)	functools	lru_cacherw   r   r   wraps)r   r   r#   )r   r   r$   with_cached_html_pages  s
    	
r   )ry   r   c             c   sZ   t j| j| jdd}| j}t||}x0|jdD ]"}t|||d}|dkrLq0|V  q0W dS )zP
    Parse an HTML document, and yield its anchor elements as Link objects.
    F)Ztransport_encodingZnamespaceHTMLElementsz.//a)rS   rp   N)r   r?   contentencodingr   rW   rU   rv   )ry   rR   r   rp   ro   ru   r#   r#   r$   parse_links(  s    
r   c               @   s:   e Zd ZdZd
eee eeddddZeddd	Z	dS )rx   z'Represents one page, along with its URLTN)r   r   r   rz   r   c             C   s   || _ || _|| _|| _dS )am  
        :param encoding: the encoding to decode the given content.
        :param url: the URL from which the HTML was downloaded.
        :param cache_link_parsing: whether links parsed from this page's url
                                   should be cached. PyPI index urls should
                                   have this set to False, for example.
        N)r   r   r   rz   )r+   r   r   r   rz   r#   r#   r$   r*   C  s    zHTMLPage.__init__)r   c             C   s
   t | jS )N)r   r   )r+   r#   r#   r$   __str__V  s    zHTMLPage.__str__)T)
r-   r.   r/   __doc__bytesr   r0   r   r*   r   r#   r#   r#   r$   rx   @  s   .)ru   reasonmethr   c             C   s   |d krt j}|d| | d S )Nz%Could not fetch URL %s: %s - skipping)rJ   rK   )ru   r   r   r#   r#   r$   _handle_get_page_failZ  s    r   T)r2   rz   r   c             C   s   t | j}t| j|| j|dS )N)r   r   rz   )rQ   r4   rx   r   r   )r2   rz   r   r#   r#   r$   _make_html_paged  s    
r   )ru   r:   r   c       
      C   s  |d krt d| jjddd }t|}|r@tjd||  d S tjj|\}}}}}}|dkrt	j
jtjj|r|jds|d7 }tjj|d}tjd	| yt||d
}W nF tk
r   tjd|  Y n4 tk
r
 } ztjd| |j|j W Y d d }~X n tk
r6 } zt| | W Y d d }~X n tk
rb } zt| | W Y d d }~X n tk
r } z&d}	|	t|7 }	t| |	tjd W Y d d }~X nd tjk
r } zt| d|  W Y d d }~X n0 tjk
r   t| d Y nX t|| j dS d S )Nz?_get_html_page() missing 1 required keyword argument: 'session'#r   r   zICannot look at %s URL %s because it does not support lookup as web pages.file/z
index.htmlz# file: URL is directory, getting %s)r:   z`Skipping page %s because it looks like an archive, and cannot be checked by a HTTP HEAD request.ziSkipping page %s because the %s request got Content-Type: %s.The only supported Content-Type is text/htmlz4There was a problem confirming the ssl certificate: )r   zconnection error: z	timed out)rz   )!	TypeErrorr   ra   r%   rJ   warningr>   r?   rj   osrC   isdirr6   r]   endswithrt   rK   rL   r9   r&   r(   r'   r   r   r   r   r0   infor   ConnectionErrorTimeoutr   rz   )
ru   r:   r   Z
vcs_schemer"   _rC   rF   excr   r#   r#   r$   _get_html_pagen  sV    
  r   c               @   s&   e Zd ZU eee  eee  dS )CollectedSourcesN)r-   r.   r/   r	   r   r   
find_links
index_urlsr#   r#   r#   r$   r     s   
r   c               @   sx   e Zd ZdZeeddddZedeee	d ddd	Z
eee d
ddZeee dddZeeedddZdS )LinkCollectorz
    Responsible for collecting Link objects from all configured locations,
    making network requests as needed.

    The class's main method is its collect_sources() method.
    N)r:   search_scoper   c             C   s   || _ || _d S )N)r   r:   )r+   r:   r   r#   r#   r$   r*     s    zLinkCollector.__init__F)r:   optionssuppress_no_indexr   c             C   sb   |j g|j }|jr:| r:tjddjdd |D  g }|jpBg }tj||d}t	||d}|S )z
        :param session: The Session to use to make requests.
        :param suppress_no_index: Whether to ignore the --no-index option
            when constructing the SearchScope object.
        zIgnoring indexes: %s,c             s   s   | ]}t |V  qd S )N)r   ).0r   r#   r#   r$   	<genexpr>  s    z'LinkCollector.create.<locals>.<genexpr>)r   r   )r:   r   )
	index_urlextra_index_urlsno_indexrJ   rK   rf   r   r   creater   )clsr:   r   r   r   r   r   link_collectorr#   r#   r$   r     s    
zLinkCollector.create)r   c             C   s   | j jS )N)r   r   )r+   r#   r#   r$   r     s    zLinkCollector.find_links)locationr   c             C   s   t || jdS )z>
        Fetch an HTML page containing package links.
        )r:   )r   r:   )r+   r   r#   r#   r$   
fetch_page  s    zLinkCollector.fetch_page)project_namecandidates_from_pager   c                s   t j fddjj|D j }t j fddjD j }tjtj	rdd t
j||D }t| d| dg| }tjdj| tt|t|d	S )
Nc             3   s$   | ]}t | jjd d dV  qdS )F)r   page_validator
expand_dirrz   N)r   r:   is_secure_origin)r   loc)r   r+   r#   r$   r     s   z0LinkCollector.collect_sources.<locals>.<genexpr>c             3   s$   | ]}t | jjd d dV  qdS )T)r   r   r   rz   N)r   r:   r   )r   r   )r   r+   r#   r$   r     s   c             S   s*   g | ]"}|d k	r|j d k	rd|j  qS )Nz* )ru   )r   sr#   r#   r$   
<listcomp>  s   z1LinkCollector.collect_sources.<locals>.<listcomp>z' location(s) to search for versions of :
)r   r   )collectionsOrderedDictr   Zget_index_urls_locationsvaluesr   rJ   isEnabledForloggingDEBUGrb   rc   r!   rK   rf   r   r   )r+   r   r   Zindex_url_sourcesZfind_links_sourceslinesr#   )r   r+   r$   collect_sources  s    zLinkCollector.collect_sources)F)r-   r.   r/   r   r   r   r*   classmethodr   r   r   propertyr   r0   r   r   r   rx   r   r   r   r   r#   r#   r#   r$   r     s   r   )N).N)T)N)Yr   rN   r   r   rb   r   r   reurllib.parser>   urllib.requestZxml.etree.ElementTreexmloptparser   typingr   r   r   r   r   r   r	   r
   pip._vendorr   r   Zpip._vendor.requestsr   Zpip._vendor.requests.exceptionsr   r   pip._internal.exceptionsr   pip._internal.models.linkr   Z!pip._internal.models.search_scoper   pip._internal.network.sessionr   Zpip._internal.network.utilsr   pip._internal.utils.filetypesr   pip._internal.utils.miscr   r   pip._internal.vcsr   sourcesr   r   r   	getLoggerr-   rJ   etreeZElementTreeZElementZHTMLElementr0   ZResponseHeadersr%   	Exceptionr&   r8   r9   rG   rL   rQ   rW   r[   r^   compile
IGNORECASEr`   r   ri   rn   rv   rw   r   r   rx   r   r   r   r   r   r#   r#   r#   r$   <module>   sr   (


2	
=