Current File : //lib/python3.9/site-packages/pip/_internal/index/__pycache__/collector.cpython-39.pyc
a

[��f~D�@sdZddlZddlZddlZddlZddlZddlZddlZddlZ	ddl
Z	ddlZddl
mZddlmZmZmZmZmZmZmZmZddlmZmZddlmZddlmZmZddl m!Z!dd	l"m#Z#dd
l$m%Z%ddl&m'Z'ddl(m)Z)dd
l*m+Z+ddl,m-Z-m.Z.ddl/m0Z0ddl1m2Z2m3Z3m4Z4e�5e6�Z7ej8j9j:Z;ee<e<fZ=e<ee<d�dd�Z>Gdd�de?�Z@edd�dd�ZAGdd�de?�ZBe<e'dd�dd�ZCe<e'ed�dd �ZDe=ee<d!�d"d#�ZEe;e<e<d$�d%d&�ZFe<e<d'�d(d)�ZGe<e<d'�d*d+�ZHe�Id,ejJ�ZKe<eLe<d-�d.d/�ZMe<e<d�d0d1�ZNe;e<e<ee#d2�d3d4�ZOGd5d6�d6�ZPed7gee#fed7gee#fd8�d9d:�ZQeQd7ee#d;�d<d=��ZRGd>d7�d7�ZSdNe#ee<e?feed?dd@�dAdB�ZTdOeeLeSdD�dEdF�ZUdPe#ee'ed7dG�dHdI�ZVGdJdK�dKe�ZWGdLdM�dM�ZXdS)QzO
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�returncCs6tjD]*}|���|�r|t|�dvr|SqdS)zgLook for VCS schemes in the URL.

    Returns the matched VCS scheme, or None if there's no match.
    z+:N)rZschemes�lower�
startswith�len)r�scheme�r#�A/usr/lib/python3.9/site-packages/pip/_internal/index/collector.py�_match_vcs_scheme0s

r%cs&eZdZeedd��fdd�Z�ZS)�_NotHTMLN)�content_type�request_descrcst��||�||_||_dS�N)�super�__init__r'r()�selfr'r(��	__class__r#r$r+<sz_NotHTML.__init__)�__name__�
__module__�__qualname__�strr+�
__classcell__r#r#r-r$r&;sr&)�responsercCs.|j�dd�}|���d�s*t||jj��dS)z�Check 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�getrr r&�request�method)r4r'r#r#r$�_ensure_html_headerBsr<c@seZdZdS)�_NotHTTPN)r/r0r1r#r#r#r$r=Lsr=)r�sessionrcCsFtj�|�\}}}}}|dvr$t��|j|dd�}t|�t|�dS)z�Send 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.
    >�https�httpT)Zallow_redirectsN)�urllib�parse�urlsplitr=�headrr<)rr>r"�netloc�path�query�fragment�respr#r#r$�_ensure_html_responsePsrJcCsRtt|�j�rt||d�t�dt|��|j|ddd�d�}t|�t	|�|S)aAccess 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>zGetting page %sr7z	max-age=0)ZAcceptz
Cache-Control)r8)
rr�filenamerJ�logger�debugrr9rr<)rr>rIr#r#r$�_get_html_response`s
��rO)r8rcCs2|r.d|vr.t�|d�\}}d|vr.|dSdS)z=Determine if we have any encoding information in our headers.r5�charsetN)�cgiZparse_header)r8r'�paramsr#r#r$�_get_encoding_from_headers�s
rS)�document�page_urlrcCs.|�d�D]}|�d�}|dur
|Sq
|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)�findallr9)rTrU�baserVr#r#r$�_determine_base_url�s


rY)�partrcCstj�tj�|��S)zP
    Clean a "part" of a URL path (i.e. after splitting on "@" characters).
    )rArB�quote�unquote�rZr#r#r$�_clean_url_path_part�sr^cCstj�tj�|��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).
    )rAr:Zpathname2url�url2pathnamer]r#r#r$�_clean_file_url_path�s
r`z(@|%2F))rF�
is_local_pathrcCs^|r
t}nt}t�|�}g}tt�|dg��D]$\}}|�||��|�|���q.d�	|�S)z*
    Clean the path portion of a URL.
    r6)
r`r^�_reserved_chars_re�splitr�	itertools�chain�append�upper�join)rFraZ
clean_func�partsZ
cleaned_partsZto_cleanZreservedr#r#r$�_clean_url_path�s
rjcCs6tj�|�}|j}t|j|d�}tj�|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.
    )ra)rF)rArB�urlparserErjrF�
urlunparse�_replace)r�resultrarFr#r#r$�_clean_link�sro)�anchorrU�base_urlrcCsL|�d�}|sdSttj�||��}|�d�}|�d�}t||||d�}|S)zJ
    Convert an anchor element in a simple repository page to a Link.
    rVNzdata-requires-pythonzdata-yanked)Z
comes_fromZrequires_python�
yanked_reason)r9rorArB�urljoinr)rprUrqrVrZ	pyrequirerr�linkr#r#r$�_create_link_from_element�s


�ruc@s:eZdZddd�dd�Zeed�dd�Zed	�d
d�ZdS)�CacheablePageContent�HTMLPageN��pagercCs|js
J�||_dSr))�cache_link_parsingry)r,ryr#r#r$r+s
zCacheablePageContent.__init__)�otherrcCst|t|��o|jj|jjkSr))�
isinstance�typeryr)r,r{r#r#r$�__eq__szCacheablePageContent.__eq__�rcCst|jj�Sr))�hashryr�r,r#r#r$�__hash__szCacheablePageContent.__hash__)	r/r0r1r+�object�boolr~�intr�r#r#r#r$rvsrvrw)�fnrcsLtjdd�tttd��fdd���t���dttd���fdd	��}|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_pagercst�|j��Sr))�listry)r�)r�r#r$�wrappersz'with_cached_html_pages.<locals>.wrapperrwrxcs|jr�t|��St�|��Sr))rzrvr�)ry�r�r�r#r$�wrapper_wrappersz/with_cached_html_pages.<locals>.wrapper_wrapper)�	functools�	lru_cachervrr�wraps)r�r�r#r�r$�with_cached_html_pagess
	
r�rxccsVtj|j|jdd�}|j}t||�}|�d�D]"}t|||d�}|durJq.|Vq.dS)zP
    Parse an HTML document, and yield its anchor elements as Link objects.
    F)Ztransport_encodingZnamespaceHTMLElementsz.//a)rUrqN)rrB�content�encodingrrYrWru)ryrTrrqrprtr#r#r$�parse_links(s �
�r�c@s:eZdZdZd
eeeeedd�dd�Zed�dd	�Z	dS)rwz'Represents one page, along with its URLTN)r�r�rrzrcCs||_||_||_||_dS)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�rrz)r,r�r�rrzr#r#r$r+CszHTMLPage.__init__rcCs
t|j�Sr))rrr�r#r#r$�__str__VszHTMLPage.__str__)T)
r/r0r1�__doc__�bytesrr2r�r+r�r#r#r#r$rw@s��).N)rt�reason�methrcCs|durtj}|d||�dS)Nz%Could not fetch URL %s: %s - skipping)rMrN)rtr�r�r#r#r$�_handle_get_page_failZsr�T)r4rzrcCst|j�}t|j||j|d�S)N)r�rrz)rSr8rwr�r)r4rzr�r#r#r$�_make_html_pageds
�r�)rtr>rc

Cs|durtd��|j�dd�d}t|�}|r@t�d||�dStj�|�\}}}}}}|dkr�t	j
�tj�
|��r�|�d�s�|d7}tj�|d�}t�d	|�zt||d
�}W�nNty�t�d|�Y�n>t�y}z"t�d||j|j�WYd}~�nd}~0t�y:}zt||�WYd}~n�d}~0t�yh}zt||�WYd}~n�d}~0t�y�}z,d
}	|	t|�7}	t||	tjd�WYd}~nld}~0tj�y�}zt|d|���WYd}~n6d}~0tj�yt|d�Yn0t||j d�SdS)Nz?_get_html_page() missing 1 required keyword argument: 'session'�#rrzICannot look at %s URL %s because it does not support lookup as web pages.�file�/z
index.htmlz# file: URL is directory, getting %srKz`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�zconnection error: z	timed out)rz)!�	TypeErrorrrcr%rMZwarningrArBrk�osrF�isdirr:r_�endswithrsrNrOr=r&r(r'rr�rrr2�infor�ConnectionErrorZTimeoutr�rz)
rtr>rZ
vcs_schemer"�_rFrI�excr�r#r#r$�_get_html_pagens^��
�
�$$r�c@s.eZdZUeeeed<eeeed<dS)�CollectedSources�
find_links�
index_urlsN)r/r0r1r	rr�__annotations__r#r#r#r$r��s
r�c@sxeZdZdZeedd�dd�Zedeee	dd�dd	��Z
eee
d
�dd��Zeeed
�dd�Ze
eed�dd�ZdS)�
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_scopercCs||_||_dSr))r�r>)r,r>r�r#r#r$r+�szLinkCollector.__init__F)r>�options�suppress_no_indexrcCs`|jg|j}|jr8|s8t�dd�dd�|D���g}|jp@g}tj||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.
        zIgnoring indexes: %s�,css|]}t|�VqdSr))r)�.0rr#r#r$�	<genexpr>��z'LinkCollector.create.<locals>.<genexpr>�r�r�)r>r�)
Z	index_urlZextra_index_urlsZno_indexrMrNrhr�r�creater�)�clsr>r�r�r�r�r�Zlink_collectorr#r#r$r��s"
�
��zLinkCollector.creatercCs|jjSr))r�r�r�r#r#r$r��szLinkCollector.find_links)�locationrcCst||jd�S)z>
        Fetch an HTML page containing package links.
        rK)r�r>)r,r�r#r#r$�
fetch_page�szLinkCollector.fetch_page)�project_name�candidates_from_pagercs�t���fdd��j�|�D����}t���fdd��jD����}t�tj	�r�dd�t
�||�D�}t|��d|�d�g|}t�
d�|��tt|�t|�d	�S)
Nc3s$|]}t|��jjddd�VqdS)F�r�Zpage_validatorZ
expand_dirrzN�rr>Zis_secure_origin�r��loc�r�r,r#r$r��s��z0LinkCollector.collect_sources.<locals>.<genexpr>c3s$|]}t|��jjddd�VqdS)Tr�Nr�r�r�r#r$r��s��cSs*g|]"}|dur|jdurd|j���qS)Nz* )rt)r��sr#r#r$�
<listcomp>
s�z1LinkCollector.collect_sources.<locals>.<listcomp>z' location(s) to search for versions of �:�
r�)�collections�OrderedDictr�Zget_index_urls_locations�valuesr�rMZisEnabledFor�logging�DEBUGrdrer!rNrhr�r�)r,r�r�Zindex_url_sourcesZfind_links_sources�linesr#r�r$�collect_sources�s*
�
�
�
����zLinkCollector.collect_sources)F)r/r0r1r�rrr+�classmethodrr�r��propertyrr2r�rrrwr�rr�r�r#r#r#r$r��s(	��� �r�)N)T)N)Yr�rQr�r�rdr�r��re�urllib.parserAZurllib.requestZxml.etree.ElementTreeZxmlZoptparser�typingrrrrrrr	r
Zpip._vendorrrZpip._vendor.requestsr
Zpip._vendor.requests.exceptionsrrZpip._internal.exceptionsrZpip._internal.models.linkrZ!pip._internal.models.search_scoperZpip._internal.network.sessionrZpip._internal.network.utilsrZpip._internal.utils.filetypesrZpip._internal.utils.miscrrZpip._internal.vcsrZsourcesrrrZ	getLoggerr/rMZetreeZElementTreeZElementZHTMLElementr2ZResponseHeadersr%�	Exceptionr&r<r=rJrOrSrYr^r`�compile�
IGNORECASErbr�rjrorurvr�r�rwr�r�r�r�r�r#r#r#r$�<module>s�(


2	�
��

�
��?