a [f~D@sdZddlZddlZddlZddlZddlZddlZddlZddlZ 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@eddddZAGddde?ZBed7d7ZSdNe#eehttpshttpT)Zallow_redirectsN)urllibparseurlsplitr=headrr<)rr>r"netlocpathqueryfragmentrespr#r#r$_ensure_html_responsePs rJcCsRtt|jrt||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 %sr7z max-age=0)ZAcceptz Cache-Control)r8) rrfilenamerJloggerdebugrr9rr<)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.r5charsetN)cgiZ parse_header)r8r'paramsr#r#r$_get_encoding_from_headerss  rS)documentpage_urlrcCs.|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)findallr9)rTrUbaserVr#r#r$_determine_base_urls   rY)partrcCstjtj|S)zP Clean a "part" of a URL path (i.e. after splitting on "@" characters). )rArBquoteunquoterZr#r#r$_clean_url_path_partsr^cCstjtj|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_paths r`z(@|%2F))rF is_local_pathrcCs^|r t}nt}t|}g}tt|dgD]$\}}|||||q.d |S)z* Clean the path portion of a URL. r6) r`r^_reserved_chars_resplitr itertoolschainappendupperjoin)rFraZ clean_funcpartsZ cleaned_partsZto_cleanZreservedr#r#r$_clean_url_paths rjcCs6tj|}|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. )ra)rF)rArBurlparserErjrF urlunparse_replace)rresultrarFr#r#r$ _clean_links ro)anchorrUbase_urlrcCsL|d}|sdSttj||}|d}|d}t||||d}|S)zJ Convert an anchor element in a simple repository page to a Link. rVNzdata-requires-pythonz data-yanked)Z comes_fromZrequires_python yanked_reason)r9rorArBurljoinr)rprUrqrVrZ pyrequirerrlinkr#r#r$_create_link_from_elements   ruc@s:eZdZdddddZeedddZed d d ZdS) CacheablePageContentHTMLPageNpagercCs|js J||_dSr))cache_link_parsingry)r,ryr#r#r$r+s zCacheablePageContent.__init__)otherrcCst|t|o|jj|jjkSr)) isinstancetyperyr)r,r{r#r#r$__eq__ szCacheablePageContent.__eq__rcCs t|jjSr))hashryrr,r#r#r$__hash__szCacheablePageContent.__hash__) r/r0r1r+objectboolr~intrr#r#r#r$rvsrvrw)fnrcsLtjddtttdfdd tdttdfdd }|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|jSr))listry)r)rr#r$wrappersz'with_cached_html_pages..wrapperrwrxcs|jrt|St|Sr))rzrvr)ryrrr#r$wrapper_wrappers z/with_cached_html_pages..wrapper_wrapper) functools lru_cachervrrwraps)rrr#rr$with_cached_html_pagess rrxccsVtj|j|jdd}|j}t||}|dD]"}t|||d}|durJq.|Vq.dS)zP Parse an HTML document, and yield its anchor elements as Link objects. F)Ztransport_encodingZnamespaceHTMLElementsz.//a)rUrqN)r rBcontentencodingrrYrWru)ryrTrrqrprtr#r#r$ parse_links(s  rc@s:eZdZdZd eeeeeddddZeddd Z dS) rwz'Represents one page, along with its URLTN)rrrrzrcCs||_||_||_||_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)rrrrz)r,rrrrzr#r#r$r+CszHTMLPage.__init__rcCs t|jSr))rrrr#r#r$__str__VszHTMLPage.__str__)T) r/r0r1__doc__bytesrr2rr+rr#r#r#r$rw@s ).N)rtreasonmethrcCs|durtj}|d||dS)Nz%Could not fetch URL %s: %s - skipping)rMrN)rtrrr#r#r$_handle_get_page_failZsrT)r4rzrcCst|j}t|j||j|dS)N)rrrz)rSr8rwrr)r4rzrr#r#r$_make_html_pageds r)rtr>rc Cs|durtd|jddd}t|}|r@td||dStj|\}}}}}}|dkrt j tj |r|ds|d7}tj|d}td |zt||d }WnNtytd |Yn>ty }z"td ||j|jWYd}~n d}~0ty:}zt||WYd}~nd}~0tyh}zt||WYd}~nd}~0ty}z,d } | t|7} t|| tjdWYd}~nld}~0tjy}zt|d|WYd}~n6d}~0tjyt|dYn0t||j dSdS)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: )rzconnection error: z timed out)rz)! TypeErrorrrcr%rMZwarningrArBrkosrFisdirr:r_endswithrsrNrOr=r&r(r'rrrrr2infor ConnectionErrorZTimeoutrrz) rtr>rZ vcs_schemer"_rFrIexcrr#r#r$_get_html_pagens^     $$rc@s.eZdZUeeeed<eeeed<dS)CollectedSources find_links index_urlsN)r/r0r1r rr__annotations__r#r#r#r$rs rc@sxeZdZdZeeddddZedeee dddd Z e e e d d d Zeeed ddZe eedddZdS) 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))rr>)r,r>rr#r#r$r+szLinkCollector.__init__F)r>optionssuppress_no_indexrcCs`|jg|j}|jr8|s8tdddd|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$ z'LinkCollector.create..rr)r>r) Z index_urlZextra_index_urlsZno_indexrMrNrhrrcreater)clsr>rrrrrZlink_collectorr#r#r$rs"   zLinkCollector.creatercCs|jjSr))rrrr#r#r$rszLinkCollector.find_links)locationrcCst||jdS)z> Fetch an HTML page containing package links. rK)rr>)r,rr#r#r$ fetch_pageszLinkCollector.fetch_page) project_namecandidates_from_pagercstfddj|D}tfddjD}ttj rddt ||D}t |d|dg|}t d|tt|t|d S) Nc3s$|]}t|jjdddVqdS)FrZpage_validatorZ expand_dirrzNrr>Zis_secure_originrlocrr,r#r$rsz0LinkCollector.collect_sources..c3s$|]}t|jjdddVqdS)TrNrrrr#r$rscSs*g|]"}|dur|jdurd|jqS)Nz* )rt)rsr#r#r$ sz1LinkCollector.collect_sources..z' location(s) to search for versions of : r) collections OrderedDictrZget_index_urls_locationsvaluesrrMZ isEnabledForloggingDEBUGrdrer!rNrhrr)r,rrZindex_url_sourcesZfind_links_sourceslinesr#rr$collect_sourcess*    zLinkCollector.collect_sources)F)r/r0r1rrrr+ classmethodrrrpropertyrr2rrrrwrrrrr#r#r#r$rs(   r)N)T)N)YrrQrrrdrrre urllib.parserAZurllib.requestZxml.etree.ElementTreeZxmlZoptparsertypingrrrrrrr r Z pip._vendorr r 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/rMZetreeZ ElementTreeZElementZ HTMLElementr2ZResponseHeadersr% Exceptionr&r<r=rJrOrSrYr^r`compile IGNORECASErbrrjrorurvrrrwrrrrrr#r#r#r$s (              2         ?