a
    e0h6V                     @   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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  ddl!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,m-Z- e'rddl.Z/ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z> e/j?j@jAZBe6eCeCf ZDeEeFZGdd ZHG dd deIZJdd ZKG dd deIZLdd  ZMd!d" ZNd#d$ ZOd%d& ZPd'd( ZQd)d* ZReSd+ejTZUd,d- ZVd.d/ ZWd0d1 ZXG d2d3 d3eYZZd4d5 Z[e[d6d7 Z\G d8d9 d9eYZ]dJd:d;Z^dKd=d>Z_dLd?d@Z`dAdB ZadMdDdEZbG dFdG dGeYZcG dHdI dIeYZddS )NzM
The main purpose of this module is to expose LinkCollector.collect_links().
    N)OrderedDict)html5librequests)unescape)
RetryErrorSSLError)parse)request)NetworkConnectionErrorLink)SearchScope)raise_for_status)	lru_cache)is_archive_file)pairwiseredact_auth_from_url)MYPY_CHECK_RUNNING)path_to_urlurl_to_path)is_urlvcs)Values)CallableIterableListMutableMappingOptionalSequenceTupleUnion)Response)
PipSessionc                 C   s6   t jD ]*}|  |r| t| dv r|  S qdS )zgLook for VCS schemes in the URL.

    Returns the matched VCS scheme, or None if there's no match.
    z+:N)r   schemeslower
startswithlen)urlscheme r)   K/var/www/auris/lib/python3.9/site-packages/pip/_internal/index/collector.py_match_vcs_scheme8   s    

r+   c                       s   e Zd Z fddZ  ZS )_NotHTMLc                    s"   t t| || || _|| _d S N)superr,   __init__content_typerequest_desc)selfr0   r1   	__class__r)   r*   r/   E   s    z_NotHTML.__init__)__name__
__module____qualname__r/   __classcell__r)   r)   r3   r*   r,   D   s   r,   c                 C   s.   | j dd}| 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.
    Content-Type 	text/htmlN)headersgetr$   r%   r,   r	   method)responser0   r)   r)   r*   _ensure_html_headerL   s    r@   c                   @   s   e Zd ZdS )_NotHTTPN)r5   r6   r7   r)   r)   r)   r*   rA   W   s   rA   c                 C   sD   t | \}}}}}|dv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)urllib_parseurlsplitrA   headr   r@   )r'   sessionr(   netlocpathqueryfragmentrespr)   r)   r*   _ensure_html_response[   s    rN   c                 C   sR   t t| jrt| |d t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.
    rH   zGetting page %sr;   z	max-age=0)AcceptzCache-Control)r<   )
r   r   filenamerN   loggerdebugr   r=   r   r@   )r'   rH   rM   r)   r)   r*   _get_html_responsel   s    rT   c                 C   s2   | r.d| v r.t | d \}}d|v r.|d S dS )zBDetermine if we have any encoding information in our headers.
    r9   charsetN)cgiparse_header)r<   r0   paramsr)   r)   r*   _get_encoding_from_headers   s
    rY   c                 C   s.   |  dD ]}|d}|dur
|  S q
|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)findallr=   )documentpage_urlbaserZ   r)   r)   r*   _determine_base_url   s
    

r_   c                 C   s   t t | S )zP
    Clean a "part" of a URL path (i.e. after splitting on "@" characters).
    )rE   quoteunquotepartr)   r)   r*   _clean_url_path_part   s    rd   c                 C   s   t t | 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).
    )urllib_requestpathname2urlurl2pathnamerb   r)   r)   r*   _clean_file_url_path   s    rh   z(@|%2F)c                 C   s^   |r
t }nt}t| }g }tt|dgD ]$\}}||| ||  q.d	|S )z*
    Clean the path portion of a URL.
    r:   )
rh   rd   _reserved_chars_resplitr   	itertoolschainappendupperjoin)rJ   is_local_path
clean_funcpartscleaned_partsto_cleanreservedr)   r)   r*   _clean_url_path   s    
rv   c                 C   s2   t | }|j }t|j|d}t |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.
    )rp   )rJ   )rE   urlparserI   rv   rJ   
urlunparse_replace)r'   resultrp   rJ   r)   r)   r*   _clean_link   s    	
r{   c                 C   sf   |  d}|sdS tt||}|  d}|r8t|nd}|  d}|rRt|}t||||d}|S )zJ
    Convert an anchor element in a simple repository page to a Link.
    rZ   Nzdata-requires-pythonzdata-yanked)
comes_fromrequires_pythonyanked_reason)r=   r{   rE   urljoinr   r   )anchorr]   base_urlrZ   r'   	pyrequirer~   linkr)   r)   r*   _create_link_from_element   s     	


r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )CacheablePageContentc                 C   s   |j s
J || _d S r-   )cache_link_parsingpage)r2   r   r)   r)   r*   r/   !  s    
zCacheablePageContent.__init__c                 C   s   t |t| o| jj|jjkS r-   )
isinstancetyper   r'   )r2   otherr)   r)   r*   __eq__&  s    zCacheablePageContent.__eq__c                 C   s   t | jjS r-   )hashr   r'   r2   r)   r)   r*   __hash__+  s    zCacheablePageContent.__hash__N)r5   r6   r7   r/   r   r   r)   r)   r)   r*   r      s   r   c                    s2   t dd fddt  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)maxsizec                    s   t  | jS r-   )listr   )cacheable_page)fnr)   r*   wrapper:  s    z'with_cached_html_pages.<locals>.wrapperc                    s   | j rt| S t | S r-   )r   r   r   )r   r   r   r)   r*   wrapper_wrapper?  s    z/with_cached_html_pages.<locals>.wrapper_wrapper)r   	functoolswraps)r   r   r)   r   r*   with_cached_html_pages0  s
    
r   c                 c   sV   t j| j| jdd}| j}t||}|dD ]"}t|||d}|du rJq.|V  q.dS )zP
    Parse an HTML document, and yield its anchor elements as Link objects.
    F)transport_encodingnamespaceHTMLElementsz.//a)r]   r   N)r   r   contentencodingr'   r_   r[   r   )r   r\   r'   r   r   r   r)   r)   r*   parse_linksI  s     
r   c                   @   s"   e Zd ZdZdddZdd ZdS )	HTMLPagez'Represents one page, along with its URLTc                 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'   r   )r2   r   r   r'   r   r)   r)   r*   r/   e  s    zHTMLPage.__init__c                 C   s
   t | jS r-   )r   r'   r   r)   r)   r*   __str__y  s    zHTMLPage.__str__N)T)r5   r6   r7   __doc__r/   r   r)   r)   r)   r*   r   b  s    
r   c                 C   s   |d u rt j}|d| | d S )Nz%Could not fetch URL %s: %s - skipping)rR   rS   )r   reasonmethr)   r)   r*   _handle_get_page_fail~  s    r   Tc                 C   s   t | j}t| j|| j|dS )N)r   r'   r   )rY   r<   r   r   r'   )r?   r   r   r)   r)   r*   _make_html_page  s    
r   c           
   
   C   s  |d u rt d| jddd }t|}|r@td||  d S t|\}}}}}}|dkrtj	
t|r|ds|d7 }t|d}td	| zt||d
}W nN ty   td|  Y n> ty } z"td| |j|j W Y d }~nd }~0  ty4 } zt| | W Y d }~nd }~0  tyb } zt| | W Y d }~nd }~0  ty } z,d}	|	t|7 }	t| |	tjd W Y d }~nld }~0  tjy } zt| d| W Y d }~n6d }~0  tjy   t| d Y n0 t|| j dS d S )Nz?_get_html_page() missing 1 required keyword argument: 'session'#   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 %srO   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r   )!	TypeErrorr'   rj   r+   rR   warningrE   rw   osrJ   isdirre   rg   endswithr   rS   rT   rA   r,   r1   r0   r
   r   r   r   strinfor   ConnectionErrorformatTimeoutr   r   )
r   rH   r'   
vcs_schemer(   _rJ   rM   excr   r)   r)   r*   _get_html_page  sZ    


$$r   c                 C   s   t t| S )zQ
    Return a list of links, with duplicates removed and ordering preserved.
    )r   r   fromkeys)linksr)   r)   r*   _remove_duplicate_links  s    r   Fc                    s   g  g  fdd}| D ]}t j|}|d}|s<|r|rF|}nt|}t j|r|rt j|}t |D ]}|t j|| qtq|r	| qt
d| qt j|r|| qt
d| qt|r	| qt
d| q fS )z
    Divide a list of locations into two groups: "files" (archives) and "urls."

    :return: A pair of lists (files, urls).
    c                    s8   t | }tj|ddd dkr*| n
 | d S )NF)strictr   r;   )r   	mimetypes
guess_typerm   )rJ   r'   filesurlsr)   r*   	sort_path  s    z"group_locations.<locals>.sort_pathzfile:z(Path '%s' is ignored: it is a directory.z:Url '%s' is ignored: it is neither a file nor a directory.zQUrl '%s' is ignored. It is either a non-existing path or lacks a specific scheme.)r   rJ   existsr%   r   r   realpathlistdirro   rm   rR   r   isfiler   )	locations
expand_dirr   r'   rp   is_file_urlrJ   itemr)   r   r*   group_locations  sB    

r   c                   @   s   e Zd ZdZdd ZdS )CollectedLinksa  
    Encapsulates the return value of a call to LinkCollector.collect_links().

    The return value includes both URLs to project pages containing package
    links, as well as individual package Link objects collected from other
    sources.

    This info is stored separately as:

    (1) links from the configured file locations,
    (2) links from the configured find_links, and
    (3) urls to HTML project pages, as described by the PEP 503 simple
        repository API.
    c                 C   s   || _ || _|| _dS )z
        :param files: Links from file locations.
        :param find_links: Links from find_links.
        :param project_urls: URLs to HTML project pages, as described by
            the PEP 503 simple repository API.
        Nr   
find_linksproject_urls)r2   r   r   r   r)   r)   r*   r/   !  s    zCollectedLinks.__init__N)r5   r6   r7   r   r/   r)   r)   r)   r*   r     s   r   c                   @   sB   e Zd ZdZdd ZedddZedd Zd	d
 Z	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_links() method.
    c                 C   s   || _ || _d S r-   )search_scoperH   )r2   rH   r   r)   r)   r*   r/   <  s    zLinkCollector.__init__Fc                 C   s`   |j g|j }|jr8|s8tdddd |D  g }|jp@g }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 r-   )r   .0r'   r)   r)   r*   	<genexpr>Q      z'LinkCollector.create.<locals>.<genexpr>)r   
index_urls)rH   r   )
	index_urlextra_index_urlsno_indexrR   rS   ro   r   r   creater   )clsrH   optionssuppress_no_indexr   r   r   link_collectorr)   r)   r*   r   E  s    

zLinkCollector.createc                 C   s   | j jS r-   )r   r   r   r)   r)   r*   r   `  s    zLinkCollector.find_linksc                 C   s   t || jdS )z>
        Fetch an HTML page containing package links.
        rO   )r   rH   )r2   locationr)   r)   r*   
fetch_pagee  s    zLinkCollector.fetch_pagec                    s    j }||}t|\}}t jdd\}}dd t||D }dd  jD }	 fddtdd |D d	d |D D }
t|
}
d
t|
|g}|
D ]}|	d| qt
d| t||	|
dS )zFind all available links for the given project name.

        :return: All the Link objects (unfiltered), as a CollectedLinks object.
        T)r   c                 S   s   g | ]}t |qS r)   r   r   r)   r)   r*   
<listcomp>y  s   z/LinkCollector.collect_links.<locals>.<listcomp>c                 S   s   g | ]}t |d qS )z-fr   r   r)   r)   r*   r   ~  r   c                    s   g | ]} j |r|qS r)   )rH   is_secure_origin)r   r   r   r)   r*   r     s   c                 s   s   | ]}t |d dV  qdS )Fr   Nr   r   r)   r)   r*   r     r   z.LinkCollector.collect_links.<locals>.<genexpr>c                 s   s   | ]}t |V  qd S r-   r   r   r)   r)   r*   r     r   z,{} location(s) to search for versions of {}:z* {}
r   )r   get_index_urls_locationsr   r   rk   rl   r   r   r&   rm   rR   rS   ro   r   )r2   project_namer   index_locationsindex_file_locindex_url_locfl_file_loc
fl_url_loc
file_linksfind_link_linksurl_locationslinesr   r)   r   r*   collect_linksl  s:    




zLinkCollector.collect_linksN)F)r5   r6   r7   r   r/   classmethodr   propertyr   r   r   r)   r)   r)   r*   r   3  s   	
r   )N)T)N)F)er   rV   r   rk   loggingr   r   recollectionsr   pip._vendorr   r   pip._vendor.distlib.compatr   Zpip._vendor.requests.exceptionsr   r   Zpip._vendor.six.moves.urllibr   rE   r	   re   pip._internal.exceptionsr
   pip._internal.models.linkr   !pip._internal.models.search_scoper   pip._internal.network.utilsr   pip._internal.utils.compatr   pip._internal.utils.filetypesr   pip._internal.utils.miscr   r   pip._internal.utils.typingr   pip._internal.utils.urlsr   r   pip._internal.vcsr   r   xml.etree.ElementTreexmloptparser   typingr   r   r   r   r   r   r   r    Zpip._vendor.requestsr!   pip._internal.network.sessionr"   etreeElementTreeElementZHTMLElementr   ZResponseHeaders	getLoggerr5   rR   r+   	Exceptionr,   r@   rA   rN   rT   rY   r_   rd   rh   compile
IGNORECASEri   rv   r{   r   objectr   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   sv   (

3	 
 



9	
;#