U
    „}agÎ@  ã                   @   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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 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& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6 e�rHddlm7Z7 ne8Z7e 9e:¡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%„ZEG d&d'„ d'ƒZFG d(d)„ d)e7ƒZGeGeGd*œd+d,„ZHeHd-ee& d.œd/d0„ƒZIG d1d-„ d-ƒZJG d2d3„ d3eƒZKdCe&ee<e?f eed4  dd5œd6d7„ZLdDeeMeJd9œd:d;„ZNe&e*ed- d<œd=d>„ZOG d?d@„ d@eƒZPG dAdB„ dBƒZQdS )EzO
The main purpose of this module is to expose LinkCollector.collect_sources().
é    N)Ú
HTMLParser)ÚValues)ÚTYPE_CHECKINGÚCallableÚDictÚIterableÚListÚMutableMappingÚ
NamedTupleÚOptionalÚSequenceÚTupleÚUnion)Úrequests)ÚResponse)Ú
RetryErrorÚSSLError)ÚNetworkConnectionError)ÚLink)ÚSearchScope)Ú
PipSession)Úraise_for_status)Úis_archive_file©Úredact_auth_from_url)Úvcsé   )ÚCandidatesFromPageÚ
LinkSourceÚbuild_source)ÚProtocol©ÚurlÚreturnc                 C   s6   t jD ]*}|  ¡  |¡r| t|ƒ dk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   ZschemesÚlowerÚ
startswithÚlen)r"   Úscheme© r(   úU/var/www/html/publicvenv/lib/python3.8/site-packages/pip/_internal/index/collector.pyÚ_match_vcs_scheme7   s    

r*   c                       s&   e Zd Zeeddœ‡ fdd„Z‡  ZS )Ú_NotAPIContentN)Úcontent_typeÚrequest_descr#   c                    s   t ƒ  ||¡ || _|| _d S ©N)ÚsuperÚ__init__r,   r-   )Úselfr,   r-   ©Ú	__class__r(   r)   r0   C   s    z_NotAPIContent.__init__)Ú__name__Ú
__module__Ú__qualname__Ústrr0   Ú__classcell__r(   r(   r2   r)   r+   B   s   r+   )Úresponser#   c                 C   s6   | j  dd¡}| ¡ }| d¡r$dS t|| jjƒ‚dS )z°
    Check the Content-Type header to ensure the response contains a Simple
    API Response.

    Raises `_NotAPIContent` if the content type is not a valid content-type.
    úContent-TypeÚUnknown)z	text/htmlz#application/vnd.pypi.simple.v1+htmlú#application/vnd.pypi.simple.v1+jsonN)ÚheadersÚgetr$   r%   r+   ÚrequestÚmethod)r9   r,   Úcontent_type_lr(   r(   r)   Ú_ensure_api_headerI   s    ÿrB   c                   @   s   e Zd ZdS )Ú_NotHTTPN)r4   r5   r6   r(   r(   r(   r)   rC   _   s   rC   )r"   Úsessionr#   c                 C   sF   t j | ¡\}}}}}|dkr$tƒ ‚|j| dd�}t|ƒ t|ƒ dS )zõ
    Send a HEAD request to the URL, and ensure the response contains a simple
    API Response.

    Raises `_NotHTTP` if the URL is not available for a HEAD request, or
    `_NotAPIContent` if the content type is not a valid content type.
    >   ÚhttpÚhttpsT)Úallow_redirectsN)ÚurllibÚparseÚurlsplitrC   Úheadr   rB   )r"   rD   r'   ÚnetlocÚpathÚqueryÚfragmentÚrespr(   r(   r)   Ú_ensure_api_responsec   s    rQ   c                 C   sz   t t| ƒjƒrt| |d� t dt| ƒ¡ |j| d dddg¡ddœd	�}t	|ƒ t
|ƒ t d
t| ƒ|j dd¡¡ |S )aY  Access an Simple API response 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 or Simple API, to avoid downloading a
       large file. Raise `_NotHTTP` if the content type cannot be determined, or
       `_NotAPIContent` if it is not HTML or a Simple API.
    2. Actually perform the request. Raise HTTP exceptions on network failures.
    3. Check the Content-Type header to make sure we got a Simple API response,
       and raise `_NotAPIContent` otherwise.
    ©rD   zGetting page %sz, r<   z*application/vnd.pypi.simple.v1+html; q=0.1ztext/html; q=0.01z	max-age=0)ÚAcceptzCache-Control)r=   zFetched page %s as %sr:   r;   )r   r   ÚfilenamerQ   ÚloggerÚdebugr   r>   Újoinr   rB   r=   )r"   rD   rP   r(   r(   r)   Ú_get_simple_responseu   s,    ýÿëþýrX   )r=   r#   c                 C   s<   | r8d| kr8t j ¡ }| d |d< | d¡}|r8t|ƒS dS )z=Determine if we have any encoding information in our headers.r:   zcontent-typeÚcharsetN)ÚemailÚmessageÚMessageÚ	get_paramr7   )r=   ÚmrY   r(   r(   r)   Ú_get_encoding_from_headers´   s    

r_   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ÚIndexContentN©Úpager#   c                 C   s   |j s
t‚|| _d S r.   )Úcache_link_parsingÚAssertionErrorrc   ©r1   rc   r(   r(   r)   r0   À   s    
zCacheablePageContent.__init__)Úotherr#   c                 C   s   t |t| ƒƒo| jj|jjkS r.   )Ú
isinstanceÚtyperc   r"   )r1   rg   r(   r(   r)   Ú__eq__Ä   s    zCacheablePageContent.__eq__©r#   c                 C   s   t | jjƒS r.   )Úhashrc   r"   ©r1   r(   r(   r)   Ú__hash__Ç   s    zCacheablePageContent.__hash__)	r4   r5   r6   r0   ÚobjectÚboolrj   Úintrn   r(   r(   r(   r)   r`   ¿   s   r`   c                   @   s    e Zd Zdee dœdd„ZdS )Ú
ParseLinksra   rb   c                 C   s   d S r.   r(   rf   r(   r(   r)   Ú__call__Ì   s    zParseLinks.__call__N)r4   r5   r6   r   r   rs   r(   r(   r(   r)   rr   Ë   s   rr   )Úfnr#   c                    sL   t jdd�ttt dœ‡ fdd„ƒ‰t  ˆ ¡dtt dœ‡ ‡fdd	„ƒ}|S )
zÚ
    Given a function that parses an Iterable[Link] from an IndexContent, cache the
    function's result (keyed by CacheablePageContent), unless the IndexContent
    `page` has `page.cache_link_parsing == False`.
    N)Úmaxsize)Úcacheable_pager#   c                    s   t ˆ | jƒƒS r.   )Úlistrc   )rv   )rt   r(   r)   Úwrapper×   s    z*with_cached_index_content.<locals>.wrapperra   rb   c                    s   | j rˆt| ƒƒS tˆ | ƒƒS r.   )rd   r`   rw   )rc   ©rt   rx   r(   r)   Úwrapper_wrapperÛ   s    z2with_cached_index_content.<locals>.wrapper_wrapper)Ú	functoolsÚ	lru_cacher`   r   r   Úwraps)rt   rz   r(   ry   r)   Úwith_cached_index_contentÐ   s
    
r~   ra   rb   c           
      c   sº   | j  ¡ }| d¡rTt | j¡}| dg ¡D ]"}t || j	¡}|dkrHq,|V  q,dS t
| j	ƒ}| jpfd}| | j |¡¡ | j	}|jpˆ|}|jD ]$}	tj|	||d�}|dkr®q�|V  q�dS )z\
    Parse a Simple API's Index Content, and yield its anchor elements as Link objects.
    r<   ÚfilesNzutf-8)Úpage_urlÚbase_url)r,   r$   r%   ÚjsonÚloadsÚcontentr>   r   Ú	from_jsonr"   ÚHTMLLinkParserÚencodingÚfeedÚdecoder�   ÚanchorsÚfrom_element)
rc   rA   ÚdataÚfileÚlinkÚparserr‡   r"   r�   Úanchorr(   r(   r)   Úparse_linksä   s&    





r‘   c                   @   s<   e Zd ZdZd
eeee eeddœdd„Zedœdd	„Z	dS )ra   z5Represents one response (or page), along with its URLTN)r„   r,   r‡   r"   rd   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‡   r"   rd   )r1   r„   r,   r‡   r"   rd   r(   r(   r)   r0     s
    zIndexContent.__init__rk   c                 C   s
   t | jƒS r.   )r   r"   rm   r(   r(   r)   Ú__str__  s    zIndexContent.__str__)T)
r4   r5   r6   Ú__doc__Úbytesr7   r   rp   r0   r’   r(   r(   r(   r)   ra     s    úùc                       sn   e Zd ZdZeddœ‡ fdd„Zeeeeee f  ddœdd„Z	eeeee f  ee d	œd
d„Z
‡  ZS )r†   zf
    HTMLParser that keeps the first base HREF and a list of all anchor
    elements' attributes.
    Nr!   c                    s$   t ƒ jdd� || _d | _g | _d S )NT)Zconvert_charrefs)r/   r0   r"   r�   rŠ   )r1   r"   r2   r(   r)   r0   #  s    zHTMLLinkParser.__init__)ÚtagÚattrsr#   c                 C   sH   |dkr,| j d kr,|  |¡}|d k	rD|| _ n|dkrD| j t|ƒ¡ d S )NÚbaseÚa)r�   Úget_hrefrŠ   ÚappendÚdict)r1   r•   r–   Úhrefr(   r(   r)   Úhandle_starttag*  s    
zHTMLLinkParser.handle_starttag)r–   r#   c                 C   s"   |D ]\}}|dkr|  S qd S )Nrœ   r(   )r1   r–   ÚnameÚvaluer(   r(   r)   r™   2  s    
zHTMLLinkParser.get_href)r4   r5   r6   r“   r7   r0   r   r   r   r�   r™   r8   r(   r(   r2   r)   r†     s   "r†   ).N)rŽ   ÚreasonÚmethr#   c                 C   s   |d krt j}|d| |ƒ d S )Nz%Could not fetch URL %s: %s - skipping)rU   rV   )rŽ   r    r¡   r(   r(   r)   Ú_handle_get_simple_fail9  s    r¢   T)r9   rd   r#   c                 C   s&   t | jƒ}t| j| jd || j|d�S )Nr:   )r‡   r"   rd   )r_   r=   ra   r„   r"   )r9   rd   r‡   r(   r(   r)   Ú_make_index_contentC  s    
ûr£   )rŽ   rD   r#   c          
   
   C   sú  | j  dd¡d }t|ƒ}|r0t d|| ¡ d S tj |¡\}}}}}}|dkr�tj	 
tj |¡¡r�| d¡sv|d7 }tj |d¡}t d|¡ zt||d	�}W �nD tk
rÄ   t d
| ¡ Y �n2 tk
rø } zt d| |j|j¡ W 5 d }~X Y nþ tk
�r$ } zt| |ƒ W 5 d }~X Y nÒ tk
�rP } zt| |ƒ W 5 d }~X Y n¦ tk
�r’ } z$d}	|	t|ƒ7 }	t| |	tjd� W 5 d }~X Y nd tjk
�rÆ } zt| d|› �ƒ W 5 d }~X Y n0 tjk
�ræ   t| dƒ Y nX t|| jd�S d S )Nú#r   r   zICannot look at %s URL %s because it does not support lookup as web pages.r�   ú/z
index.htmlz# file: URL is directory, getting %srR   z`Skipping page %s because it looks like an archive, and cannot be checked by a HTTP HEAD request.zºSkipping page %s because the %s request got Content-Type: %s. The only supported Content-Types are application/vnd.pypi.simple.v1+json, application/vnd.pypi.simple.v1+html, and text/htmlz4There was a problem confirming the ssl certificate: )r¡   zconnection error: z	timed out)rd   ) r"   Úsplitr*   rU   ÚwarningrH   rI   ÚurlparseÚosrM   Úisdirr?   Úurl2pathnameÚendswithÚurljoinrV   rX   rC   r+   r-   r,   r   r¢   r   r   r7   Úinfor   ÚConnectionErrorÚTimeoutr£   rd   )
rŽ   rD   r"   Z
vcs_schemer'   Ú_rM   rP   Úexcr    r(   r(   r)   Ú_get_index_contentP  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)r4   r5   r6   r   r   r   Ú__annotations__r(   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)rD   Úsearch_scoper#   c                 C   s   || _ || _d S r.   )r¹   rD   )r1   rD   r¹   r(   r(   r)   r0   ›  s    zLinkCollector.__init__F)rD   ÚoptionsÚsuppress_no_indexr#   c                 C   sd   |j g|j }|jr8|s8t dd dd„ |D ƒ¡¡ g }|jp@g }tj|||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>³  s     z'LinkCollector.create.<locals>.<genexpr>)rµ   r¶   Úno_index)rD   r¹   )
Ú	index_urlÚextra_index_urlsr¿   rU   rV   rW   rµ   r   Úcreater¸   )ÚclsrD   rº   r»   r¶   rµ   r¹   Úlink_collectorr(   r(   r)   rÂ   £  s$    
þ
ýþzLinkCollector.createrk   c                 C   s   | j jS r.   )r¹   rµ   rm   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.
        rR   )r³   rD   )r1   rÅ   r(   r(   r)   Úfetch_responseÉ  s    zLinkCollector.fetch_response)Úproject_nameÚcandidates_from_pager#   c                    sª   t  ‡ ‡‡fdd„ˆj ˆ¡D ƒ¡ ¡ }t  ‡ ‡‡fdd„ˆjD ƒ¡ ¡ }t tj	¡r–dd„ t
 ||¡D ƒ}t|ƒ› dˆ› d�g| }t d |¡¡ tt|ƒt|ƒd	�S )
Nc              	   3   s&   | ]}t |ˆ ˆjjd d ˆd�V  qdS )F©rÈ   Zpage_validatorZ
expand_dirrd   rÇ   N©r   rD   Zis_secure_origin©r½   Úloc©rÈ   rÇ   r1   r(   r)   r¾   Õ  s   	øúz0LinkCollector.collect_sources.<locals>.<genexpr>c              	   3   s&   | ]}t |ˆ ˆjjd d ˆd�V  qdS )TrÉ   NrÊ   rË   rÍ   r(   r)   r¾   à  s   	øúc                 S   s*   g | ]"}|d k	r|j d k	rd|j › �‘qS )Nz* )rŽ   )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µ   rU   ÚisEnabledForÚloggingÚDEBUGÚ	itertoolsÚchainr&   rV   rW   r´   rw   )r1   rÇ   rÈ   Zindex_url_sourcesZfind_links_sourcesÚlinesr(   rÍ   r)   Úcollect_sourcesÏ  s&    	
÷	÷
þÿýþzLinkCollector.collect_sources)F)r4   r5   r6   r“   r   r   r0   Úclassmethodr   rp   rÂ   Úpropertyr   r7   rµ   r   r   ra   rÆ   r   r´   rÛ   r(   r(   r(   r)   r¸   ’  s(   	ü üû!ür¸   )N)T)Rr“   rÒ   Úemail.messagerZ   r{   rØ   r‚   rÖ   r©   Úurllib.parserH   Úurllib.requestZhtml.parserr   Úoptparser   Útypingr   r   r   r   r   r	   r
   r   r   r   r   Úpip._vendorr   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   Úpip._internal.vcsr   Úsourcesr   r   r   r    ro   Ú	getLoggerr4   rU   r7   ZResponseHeadersr*   Ú	Exceptionr+   rB   rC   rQ   rX   r_   r`   rr   r~   r‘   ra   r†   r¢   rp   r£   r³   r´   r¸   r(   r(   r(   r)   Ú<module>   sv   4
? ý

ü ÿ þ=