o
    
j6hS                  	   @   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 ddlmZmZmZmZ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l0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z< erddlm=Z= ne>Z=e?e@ZAejBjCjDZEeeFeFf ZGdeFdeeF fddZHG dd deIZJde"ddfddZKG dd deIZLdeFd e-ddfd!d"ZMdeFd e-de"fd#d$ZNd%eGdeeF fd&d'ZOd(eEd)eFdeFfd*d+ZPd,eFdeFfd-d.ZQd,eFdeFfd/d0ZReSd1ejTZUd2eFd3eVdeFfd4d5ZWdeFdeFfd6d7ZXd8eeFeeF f d)eFd9eFdee) fd:d;ZYG d<d= d=ZZG d>d? d?e=Z[d@e[de[fdAdBZ\dCdDdee) fdEdFZ]e\dCdDdGeVdee) fdHdIZ^G dJdD dDZ_G dKdL dLeZ`	d]dMe)dNeeFeIf dOeedP  ddfdQdRZad^de"dTeVde_fdUdVZb	d]dMe)d ee- dedD fdWdXZcG dYdZ dZeZdG d[d\ d\ZedS )_zO
The main purpose of this module is to expose LinkCollector.collect_sources().
    N)
HTMLParser)Values)TYPE_CHECKINGAnyCallableDictIterableListMutableMapping
NamedTupleOptionalSequenceTupleUnion)html5librequests)Response)
RetryErrorSSLError)NetworkConnectionError)Link)SearchScope)
PipSession)raise_for_status)
deprecated)is_archive_file)pairwiseredact_auth_from_url)vcs   )CandidatesFromPage
LinkSourcebuild_source)Protocolurlreturnc                 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)r$   scheme r+   l/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/pip/_internal/index/collector.py_match_vcs_scheme;   s
   
r-   c                       s*   e Zd Zdededdf fddZ  ZS )_NotHTMLcontent_typerequest_descr%   Nc                    s   t  || || _|| _d S N)super__init__r/   r0   )selfr/   r0   	__class__r+   r,   r3   G   s   
z_NotHTML.__init__)__name__
__module____qualname__strr3   __classcell__r+   r+   r5   r,   r.   F   s    "r.   responsec                 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.   requestmethod)r<   r/   r+   r+   r,   _ensure_html_headerM   s   rD   c                   @   s   e Zd ZdS )_NotHTTPN)r7   r8   r9   r+   r+   r+   r,   rE   W   s    rE   sessionc                 C   sF   t j| \}}}}}|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urlsplitrE   headr   rD   )r$   rF   r*   netlocpathqueryfragmentrespr+   r+   r,   _ensure_html_response[   s   rS   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.
    rF   zGetting page %sr?   z	max-age=0)AcceptzCache-Control)r@   )
r   r   filenamerS   loggerdebugr   rA   r   rD   )r$   rF   rR   r+   r+   r,   _get_html_responsek   s   rY   r@   c                 C   s2   | rd| v rt | d \}}d|v r|d S dS )z=Determine if we have any encoding information in our headers.r=   charsetN)cgiparse_header)r@   r/   paramsr+   r+   r,   _get_encoding_from_headers   s
   r^   documentpage_urlc                 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.

    TODO: Remove when `html5lib` is dropped.
    z.//basehrefN)findallrA   )r_   r`   basera   r+   r+   r,   _determine_base_url   s   
rd   partc                 C      t jt j| S )zP
    Clean a "part" of a URL path (i.e. after splitting on "@" characters).
    )rJ   rK   quoteunquotere   r+   r+   r,   _clean_url_path_part   s   rj   c                 C   rf   )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).
    )rJ   rB   pathname2urlurl2pathnameri   r+   r+   r,   _clean_file_url_path   s   
rm   z(@|%2F)rO   is_local_pathc                 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>   )
rm   rj   _reserved_chars_resplitr   	itertoolschainappendupperjoin)rO   rn   
clean_funcpartscleaned_partsto_cleanreservedr+   r+   r,   _clean_url_path   s   

r{   c                 C   s6   t j| }|j }t|j|d}t 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.
    )rn   )rO   )rJ   rK   urlparserN   r{   rO   
urlunparse_replace)r$   resultrn   rO   r+   r+   r,   _clean_link   s   r   element_attribsbase_urlc                 C   sL   |  d}|s	dS ttj||}|  d}|  d}t||||d}|S )zW
    Convert an anchor element's attributes in a simple repository page to a Link.
    ra   Nzdata-requires-pythonzdata-yanked)
comes_fromrequires_pythonyanked_reason)rA   r   rJ   rK   urljoinr   )r   r`   r   ra   r$   	pyrequirer   linkr+   r+   r,   _create_link_from_element   s   


r   c                   @   s6   e Zd ZdddZdedefdd	Zdefd
dZdS )CacheablePageContentpageHTMLPager%   Nc                 C   s   |j sJ || _d S r1   )cache_link_parsingr   )r4   r   r+   r+   r,   r3     s   

zCacheablePageContent.__init__otherc                 C   s   t |t| o| jj|jjkS r1   )
isinstancetyper   r$   )r4   r   r+   r+   r,   __eq__  s   zCacheablePageContent.__eq__c                 C   s   t | jjS r1   )hashr   r$   r4   r+   r+   r,   __hash__  s   zCacheablePageContent.__hash__)r   r   r%   N)	r7   r8   r9   r3   objectboolr   intr   r+   r+   r+   r,   r     s    
r   c                   @   s&   e Zd Zdddedee fddZdS )
ParseLinksr   r   use_deprecated_html5libr%   c                 C   s   d S r1   r+   )r4   r   r   r+   r+   r,   __call__   s   zParseLinks.__call__N)r7   r8   r9   r   r   r   r   r+   r+   r+   r,   r     s    r   fnc                    sX   t jdddtdtdtt f fddt  dd	dtdtt f 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   r%   c                    s   t  | j|S r1   )listr   )r   r   )r   r+   r,   wrapper-  s   z'with_cached_html_pages.<locals>.wrapperr   r   c                    s"   | j r
t| |S t | |S r1   )r   r   r   )r   r   r   r   r+   r,   wrapper_wrapper3  s   z/with_cached_html_pages.<locals>.wrapper_wrapper)	functools	lru_cacher   r   r	   r   wraps)r   r   r+   r   r,   with_cached_html_pages&  s   
"r   r   r   c                 c   sZ    t j| j| jdd}| j}t||}|dD ]}t|j||d}|du r'q|V  qdS )z~
    Parse an HTML document, and yield its anchor elements as Link objects.

    TODO: Remove when `html5lib` is dropped.
    F)transport_encodingnamespaceHTMLElementsz.//ar`   r   N)	r   rK   contentencodingr$   rd   rb   r   attrib)r   r_   r$   r   anchorr   r+   r+   r,   _parse_links_html5lib<  s$   
r   r   c           
      c   s    | j pd}|s.d|}| jdt| }|| dkr.td| j ddddd d	}|r9t| E dH  dS t	 }|
| j| | j}|jpL|}|jD ]}t|||d
}	|	du r^qP|	V  qPdS )zP
    Parse an HTML document, and yield its anchor elements as Link objects.
    zutf-8z<!doctype html>Nz The HTML index page being used (z) is not a proper HTML 5 document. This is in violation of PEP 503 which requires these pages to be well-formed HTML 5 documents. Please reach out to the owners of this index page, and ask them to update this index page to a valid HTML 5 document.z22.2iI*  )reasonreplacementgone_inissueTr   )r   encoder   r)   decoder'   r   r$   r   HTMLLinkParserfeedr   anchorsr   )
r   r   r   expected_doctypeactual_startparserr$   r   r   r   r+   r+   r,   parse_linksU  s<   



r   c                   @   sD   e Zd ZdZ	ddedee dededdf
d	d
ZdefddZ	dS )r   z'Represents one page, along with its URLTr   r   r$   r   r%   Nc                 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   )r4   r   r   r$   r   r+   r+   r,   r3     s   
zHTMLPage.__init__c                 C   s
   t | jS r1   )r   r$   r   r+   r+   r,   __str__  s   
zHTMLPage.__str__T)
r7   r8   r9   __doc__bytesr   r:   r   r3   r   r+   r+   r+   r,   r     s    
c                       s   e Zd ZdZdededdf fddZdeddfd	d
Zdedee	ee
e f  ddfddZdee	ee
e f  de
e fddZdddZ  ZS )r   zf
    HTMLParser that keeps the first base HREF and a list of all anchor
    elements' attributes.
    argskwargsr%   Nc                    s(   t  j|i | d| _d | _g | _d S )NF)r2   r3   
_seen_declr   r   )r4   r   r   r5   r+   r,   r3     s   
zHTMLLinkParser.__init__declc                 C   s   |  dkr
|   d| _d S )Nzdoctype htmlT)r'   _raise_errorr   )r4   r   r+   r+   r,   handle_decl  s   
zHTMLLinkParser.handle_decltagattrsc                 C   s`   | j s|   |dkr | jd u r | |}|d ur|| _d S d S |dkr.| jt| d S d S )Nrc   a)r   r   r   get_hrefr   rs   dict)r4   r   r   ra   r+   r+   r,   handle_starttag  s   

zHTMLLinkParser.handle_starttagc                 C   s"   |D ]\}}|dkr|  S qd S )Nra   r+   )r4   r   namevaluer+   r+   r,   r     s
   zHTMLLinkParser.get_hrefc                 C   s   t d)NzHTML doctype missing or incorrect. Expected <!DOCTYPE html>.

If you believe this error to be incorrect, try passing the command line option --use-deprecated=html5lib and please leave a comment on the pip issue at https://github.com/pypa/pip/issues/10825.)
ValueErrorr   r+   r+   r,   r     s   zHTMLLinkParser._raise_error)r%   N)r7   r8   r9   r   r   r3   r:   r   r	   r   r   r   r   r   r;   r+   r+   r5   r,   r     s    &&r   r   r   meth).Nc                 C   s   |d u rt j}|d| | d S )Nz%Could not fetch URL %s: %s - skipping)rW   rX   )r   r   r   r+   r+   r,   _handle_get_page_fail  s   r   Tr   c                 C   s   t | j}t| j|| j|dS )N)r   r$   r   )r^   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j|\}}}}}}|dkrPt	j
tj|rP|dsC|d7 }tj|d}td	| zt||d
}W n tyh   td|  Y d S  ty } ztd| |j|j W Y d }~d S d }~w ty } zt| | W Y d }~d S d }~w ty } zt| | W Y d }~d S d }~w ty } zd}	|	t|7 }	t| |	tjd W Y d }~d S d }~w tjy } zt| d|  W Y d }~d S d }~w tjy   t| d Y d S w t|| j 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 %srT   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$   rp   r-   rW   warningrJ   rK   r|   osrO   isdirrB   rl   endswithr   rX   rY   rE   r.   r0   r/   r   r   r   r   r:   infor   ConnectionErrorTimeoutr   r   )
r   rF   r$   
vcs_schemer*   _rO   rR   excr   r+   r+   r,   _get_html_page  sv   
r   c                   @   s.   e Zd ZU eee  ed< eee  ed< dS )CollectedSources
find_links
index_urlsN)r7   r8   r9   r   r   r!   __annotations__r+   r+   r+   r,   r     s   
 r   c                
   @   s   e Zd ZdZdededdfddZe	dded	ed
e	dd fddZ
edee fddZdedee fddZdededef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.
    rF   search_scoper%   Nc                 C   s   || _ || _d S r1   )r   rF   )r4   rF   r   r+   r+   r,   r3   +  s   
zLinkCollector.__init__Foptionssuppress_no_indexc                 C   s`   |j g|j }|jr|st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 r1   )r   ).0r$   r+   r+   r,   	<genexpr>C  s    z'LinkCollector.create.<locals>.<genexpr>r   r   )rF   r   )
	index_urlextra_index_urlsno_indexrW   rX   ru   r   r   creater   )clsrF   r   r   r   r   r   link_collectorr+   r+   r,   r   3  s"   

zLinkCollector.createc                 C   s   | j jS r1   )r   r   r   r+   r+   r,   r   T  s   zLinkCollector.find_linkslocationc                 C   s   t || jdS )z>
        Fetch an HTML page containing package links.
        rT   )r   rF   )r4   r   r+   r+   r,   
fetch_pageX  s   zLinkCollector.fetch_pageproject_namecandidates_from_pagec                    s   t  fddj|D  }t  fddjD  }ttj	rIdd t
||D }t| d| dg| }td| tt|t|d	S )
Nc                 3   &    | ]}t | jjd d dV  qdS )Fr   page_validator
expand_dirr   Nr"   rF   is_secure_originr   locr   r4   r+   r,   r   d      
z0LinkCollector.collect_sources.<locals>.<genexpr>c                 3   r   )Tr   Nr   r   r   r+   r,   r   n  r   c                 S   s*   g | ]}|d ur|j d urd|j  qS )Nz* )r   )r   sr+   r+   r,   
<listcomp>z  s
    
z1LinkCollector.collect_sources.<locals>.<listcomp>z' location(s) to search for versions of :
r   )collectionsOrderedDictr   get_index_urls_locationsvaluesr   rW   isEnabledForloggingDEBUGrq   rr   r)   rX   ru   r   r   )r4   r   r   index_url_sourcesfind_links_sourceslinesr+   r   r,   collect_sources^  s2   
	
	

zLinkCollector.collect_sources)F)r7   r8   r9   r   r   r   r3   classmethodr   r   r   propertyr	   r:   r   r   r   r   r   r    r   r  r+   r+   r+   r,   r   "  s<    
 r   r1   r   )fr   r[   r  r   rq   r	  r   reurllib.parserJ   urllib.requestxml.etree.ElementTreexmlhtml.parserr   optparser   typingr   r   r   r   r   r	   r
   r   r   r   r   r   pip._vendorr   r   pip._vendor.requestsr   pip._vendor.requests.exceptionsr   r   pip._internal.exceptionsr   pip._internal.models.linkr   !pip._internal.models.search_scoper   pip._internal.network.sessionr   pip._internal.network.utilsr   pip._internal.utils.deprecationr   pip._internal.utils.filetypesr   pip._internal.utils.miscr   r   pip._internal.vcsr   sourcesr    r!   r"   r#   r   	getLoggerr7   rW   etreeElementTreeElementHTMLElementr:   ResponseHeadersr-   	Exceptionr.   rD   rE   rS   rY   r^   rd   rj   rm   compile
IGNORECASEro   r   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   <module>   s    8


2	
/.




?