o ƚRe~D @sRdZddlZddlZddlZddlZddlZddlZddlZddlZ ddl Z ddl Z ddl mZddlmZmZmZmZmZmZmZmZddlmZmZddlmZddlmZmZddl m!Z!dd l"m#Z#dd l$m%Z%dd l&m'Z'dd l(m)Z)dd l*m+Z+ddl,m-Z-m.Z.ddl/m0Z0ddl1m2Z2m3Z3m4Z4e5e6Z7e j8j9j:Z;eeGddde?Z@deddfddZAGddde?ZBdeZQeQd?dLsr>sessioncCsFtj|\}}}}}|dvrt|j|dd}t|t|dS)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. >httphttpsT)allow_redirectsN)urllibparseurlsplitr>headrr=)rr?r#netlocpathqueryfragmentrespr$r$r%_ensure_html_responsePs  rLcCsRtt|jr t||dtdt||j|dddd}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 %sr8z max-age=0)Acceptz Cache-Control)r9) rrfilenamerLloggerdebugrr:rr=)rr?rKr$r$r%_get_html_response`s rRr9cCs2|rd|vrt|d\}}d|vr|dSdS)z=Determine if we have any encoding information in our headers.r6charsetN)cgi parse_header)r9r(paramsr$r$r%_get_encoding_from_headerss rWdocumentpage_urlcCs.|dD]}|d}|dur|Sq|S)aDetermine the HTML document's base URL. This looks for a ```` 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.//basehrefN)findallr:)rXrYbaserZr$r$r%_determine_base_urls  r]partcCtjtj|S)zP Clean a "part" of a URL path (i.e. after splitting on "@" characters). )rCrDquoteunquoter^r$r$r%_clean_url_path_partsrccCr_)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). )rCr; pathname2url url2pathnamerbr$r$r%_clean_file_url_paths rfz(@|%2F)rH is_local_pathcCs^|rt}nt}t|}g}tt|dgD]\}}|||||qd |S)z* Clean the path portion of a URL. r7) rfrc_reserved_chars_resplitr itertoolschainappendupperjoin)rHrg clean_funcparts cleaned_partsto_cleanreservedr$r$r%_clean_url_paths  rtcCs6tj|}|j }t|j|d}tj|j|dS)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. )rg)rH)rCrDurlparserGrtrH urlunparse_replace)rresultrgrHr$r$r% _clean_links ryanchorbase_urlcCsL|d}|s dSttj||}|d}|d}t||||d}|S)zJ Convert an anchor element in a simple repository page to a Link. rZNzdata-requires-pythonz data-yanked) comes_fromrequires_python yanked_reason)r:ryrCrDurljoinr)rzrYr{rZr pyrequirer~linkr$r$r%_create_link_from_elements   rc@s6eZdZd ddZdedefdd Zdefd d ZdS) CacheablePageContentpageHTMLPagerNcCs|jsJ||_dSr*)cache_link_parsingr)r-rr$r$r%r,s  zCacheablePageContent.__init__othercCst|t|o|jj|jjkSr*) isinstancetyperr)r-rr$r$r%__eq__ szCacheablePageContent.__eq__cCs t|jjSr*)hashrrr-r$r$r%__hash__s zCacheablePageContent.__hash__)rrrN) r0r1r2r,objectboolrintrr$r$r$r%rs rfnrcsPtjdddtdttffdd tdddttffd 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)maxsizecacheable_pagercst|jSr*)listr)r)rr$r%wrappersz'with_cached_html_pages..wrapperrrcs|jr t|St|Sr*)rrr)rrrr$r%wrapper_wrappers  z/with_cached_html_pages..wrapper_wrapper) functools lru_cacherrrwraps)rrr$rr%with_cached_html_pagess rrccsXtj|j|jdd}|j}t||}|dD]}t|||d}|dur&q|VqdS)zP Parse an HTML document, and yield its anchor elements as Link objects. F)transport_encodingnamespaceHTMLElementsz.//a)rYr{N)r rDcontentencodingrr]r[r)rrXrr{rzrr$r$r% parse_links(s$ rc @sDeZdZdZ d dedeedededdf d d Zdefd d Z dS)rz'Represents one page, along with its URLTrrrrrNcCs||_||_||_||_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)rrrr)r-rrrrr$r$r%r,Cs zHTMLPage.__init__cCs t|jSr*)rrrr$r$r%__str__Vs zHTMLPage.__str__T) r0r1r2__doc__bytesrr3rr,rr$r$r$r%r@s rreasonmeth).NcCs|durtj}|d||dS)Nz%Could not fetch URL %s: %s - skipping)rPrQ)rrrr$r$r%_handle_get_page_failZsrTrcCst|j}t|j||j|dS)N)rrr)rWr9rrr)r5rrr$r$r%_make_html_pageds rc Cs |durtd|jddd}t|}|r td||dStj|\}}}}}}|dkrPt j tj |rP|dsC|d7}tj|d}td |zt||d }Wntyhtd |YdSty}ztd ||j|jWYd}~dSd}~wty}z t||WYd}~dSd}~wty}z t||WYd}~dSd}~wty}zd } | t|7} t|| tjdWYd}~dSd}~wtjy}zt|d|WYd}~dSd}~wtjyt|dYdSwt||j dS)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 %srMz`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: )rzconnection error: z timed out)r)! TypeErrorrrir&rPwarningrCrDruosrHisdirr;reendswithrrQrRr>r'r)r(rrrrr3infor ConnectionErrorTimeoutrr) rr?r vcs_schemer#_rHrKexcrr$r$r%_get_html_pagensv       rc@s.eZdZUeeeed<eeeed<dS)CollectedSources find_links index_urlsN)r0r1r2r rr__annotations__r$r$r$r%rs rc @seZdZdZdededdfddZe dded ed e ddfd d Z e de e fd dZdedeefddZde dedefddZdS) 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. r? search_scoperNcCs||_||_dSr*)rr?)r-r?rr$r$r%r,s zLinkCollector.__init__Foptionssuppress_no_indexcCs`|jg|j}|jr|stdddd|Dg}|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% sz'LinkCollector.create..rr)r?r) index_urlextra_index_urlsno_indexrPrQrnrrcreater)clsr?rrrrrlink_collectorr$r$r%rs"  zLinkCollector.createcCs|jjSr*)rrrr$r$r%rszLinkCollector.find_linkslocationcCst||jdS)z> Fetch an HTML page containing package links. rM)rr?)r-rr$r$r% fetch_pageszLinkCollector.fetch_page project_namecandidates_from_pagecstfddj|D}tfddjD}ttj rIddt ||D}t |d|dg|}t d|tt|t|d S) Nc3&|]}t|jjdddVqdS)Frpage_validator expand_dirrNrr?is_secure_originrlocrr-r$r%r z0LinkCollector.collect_sources..c3r)TrNrrrr$r%rrcSs*g|]}|dur|jdurd|jqS)Nz* )r)rsr$r$r% s  z1LinkCollector.collect_sources..z' location(s) to search for versions of : r) collections OrderedDictrget_index_urls_locationsvaluesrrP isEnabledForloggingDEBUGrjrkr"rQrnrr)r-rrindex_url_sourcesfind_links_sourceslinesr$rr%collect_sourcess2       zLinkCollector.collect_sources)F)r0r1r2rrrr, classmethodrrrpropertyrr3rrrrrrrrr$r$r$r%rs<   rr*r)YrrTrrrjrrre urllib.parserCurllib.requestxml.etree.ElementTreexmloptparsertypingrrrrrrr r pip._vendorr r Zpip._vendor.requestsr Zpip._vendor.requests.exceptionsrrpip._internal.exceptionsrpip._internal.models.linkr!pip._internal.models.search_scoperpip._internal.network.sessionrpip._internal.network.utilsrpip._internal.utils.filetypesrpip._internal.utils.miscrrpip._internal.vcsrsourcesrrr getLoggerr0rPetree ElementTreeElement HTMLElementr3ResponseHeadersr& Exceptionr'r=r>rLrRrWr]rcrfcompile IGNORECASErhrrtryrrrrrrrrrrr$r$r$r%s (             2          ?