3 Re@W@sdZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z ddl mZddlmZmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lm Z ddl!m"Z"ddl#m$Z$m%Z%ddl&m'Z'ddl(m)Z)m*Z*ddl+m,Z,m-Z-e'rddl.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8ddl9Z:ddl;mZ>e:j?j@jAZBe4eCeCfZDejEeFZGddZHddZIGdddeJZKddZLGdd d eJZMd!d"ZNd#d$ZOd%d&ZPd'd(ZQd)d*ZRd+d,ZSejTd-ejUZVd.d/ZWd0d1ZXd2d3ZYGd4d5d5eZZ[d6d7Z\e\d8d9Z]Gd:d;d;eZZ^dLdrBrA) urllib_parseurlsplitr@headr r?)r&sessionr'netlocpathqueryfragmentrespr(r(r)_ensure_html_responsefs rMcCsLt|rt||dtjdt||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. )rGzGetting page %sz text/htmlz max-age=0)Acceptz Cache-Control)r;)r.rMloggerdebugrr<r r?)r&rGrLr(r(r)_get_html_responsews  rQcCs2|r.d|kr.tj|d\}}d|kr.|dSdS)zBDetermine if we have any encoding information in our headers. z Content-TypecharsetN)cgi parse_header)r;r2paramsr(r(r)_get_encoding_from_headerss  rVcCs.x(|jdD]}|jd}|dk r |Sq W|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<)documentpage_urlbaserWr(r(r)_determine_base_urls  r\cCstjtj|S)zP Clean a "part" of a URL path (i.e. after splitting on "@" characters). )rDquoteunquote)partr(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). )urllib_request pathname2url url2pathname)r_r(r(r)_clean_file_url_paths rdz(@|%2F)cCsb|r t}nt}tj|}g}x:ttj|dgD]$\}}|j|||j|jq0Wdj |S)z* Clean the path portion of a URL. r:) rdr`_reserved_chars_resplitr itertoolschainappendupperjoin)rI is_local_path clean_funcparts cleaned_partsto_cleanreservedr(r(r)_clean_url_paths rrcCs2tj|}|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. )rl)rI)rDurlparserHrrrI urlunparse_replace)r&resultrlrIr(r(r) _clean_links rwcCsf|jd}|sdSttj||}|jd}|r8t|nd}|jd}|rRt|}t||||d}|S)zJ Convert an anchor element in a simple repository page to a Link. rWNzdata-requires-pythonz data-yanked) comes_fromrequires_python yanked_reason)r<rwrDurljoinrr )anchorrZbase_urlrWr& pyrequirerzlinkr(r(r)_create_link_from_element s   rc@s$eZdZddZddZddZdS)CacheablePageContentcCs|js t||_dS)N)cache_link_parsingAssertionErrorpage)r4rr(r(r)r1,s zCacheablePageContent.__init__cCst|t|o|jj|jjkS)N) isinstancetyperr&)r4otherr(r(r)__eq__1szCacheablePageContent.__eq__cCs t|jjS)N)hashrr&)r4r(r(r)__hash__6szCacheablePageContent.__hash__N)r6r7r8r1rrr(r(r(r)r+srcs2tddfddtjfdd}|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)maxsizecst|jS)N)listr)cacheable_page)fnr(r)wrapperEsz'with_cached_html_pages..wrappercs|jrt|St|S)N)rrr)r)rrr(r)wrapper_wrapperJs z/with_cached_html_pages..wrapper_wrapper)r functoolswraps)rrr()rrr)with_cached_html_pages;s rccsZtj|j|jdd}|j}t||}x0|jdD]"}t|||d}|dkrLq0|Vq0WdS)zP Parse an HTML document, and yield its anchor elements as Link objects. F)transport_encodingnamespaceHTMLElementsz.//a)rZr}N)rrcontentencodingr&r\rXr)rrYr&r}r|rr(r(r) parse_linksTs rc@s"eZdZdZdddZddZdS) HTMLPagez'Represents one page, along with its URLTcCs||_||_||_||_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)rrr&r)r4rrr&rr(r(r)r1pszHTMLPage.__init__cCs t|jS)N)rr&)r4r(r(r)__str__szHTMLPage.__str__N)T)r6r7r8__doc__r1rr(r(r(r)rms rcCs|dkrtj}|d||dS)Nz%Could not fetch URL %s: %s - skipping)rOrP)rreasonmethr(r(r)_handle_get_page_failsrTcCst|j}t|j||j|dS)N)rr&r)rVr;rrr&)r>rrr(r(r)_make_html_pages  rc Cs|dkrtd|jjddd}t|}|r@tjd||dStj|\}}}}}}|dkrtj j t j |r|j ds|d7}tj|d}tjd |yt||d }WnFtk rtjd |Yn4tk r}ztjd ||j|jWYdd}~Xntk r0}zt||WYdd}~Xntk r\}zt||WYdd}~Xntk r}z&d } | t|7} t|| tjdWYdd}~Xndtjk r}zt|dj|WYdd}~Xn0tjk rt|dYnXt||j dSdS)Nz?_get_html_page() missing 1 required keyword argument: 'session'#rzICannot look at %s URL %s because it does not support lookup as web pages.file/z index.htmlz# file: URL is directory, getting %s)rGz`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)! TypeErrorr&rfr*rOwarningrDrsosrIisdirrarcr,r{rPrQr@r/r3r2r rrrstrinforConnectionErrorformatTimeoutrr) rrGr& vcs_schemer'_rIrLexcrr(r(r)_get_html_pagesP        rcCsttj|S)zQ Return a list of links, with duplicates removed and ordering preserved. )rrfromkeys)linksr(r(r)_remove_duplicate_linkssrFcsggfdd}x|D]}tjj|}|jd}|s>|r|rH|}nt|}tjj|r|rtjj|}xBtj|D]}|tjj||qxWq|rj |qt j d|qtjj |r||qt j d|qt |rj |qt j d|qWfS)z Divide a list of locations into two groups: "files" (archives) and "urls." :return: A pair of lists (files, urls). cs8t|}tj|ddddkr*j|n j|dS)NF)strictrz text/html)r mimetypes guess_typeri)rIr&)filesurlsr(r) sort_paths z"group_locations..sort_pathzfile:z(Path '%s' is ignored: it is a directory.z:Url '%s' is ignored: it is neither a file nor a directory.zQUrl '%s' is ignored. It is either a non-existing path or lacks a specific scheme.)rrIexistsr$rrrealpathlistdirrkrirOrisfiler) locations expand_dirrr&rl is_file_urlrIitemr()rrr)group_locationss<           rc@seZdZdZddZdS)CollectedLinksa Encapsulates the return value of a call to LinkCollector.collect_links(). The return value includes both URLs to project pages containing package links, as well as individual package Link objects collected from other sources. This info is stored separately as: (1) links from the configured file locations, (2) links from the configured find_links, and (3) urls to HTML project pages, as described by the PEP 503 simple repository API. cCs||_||_||_dS)z :param files: Links from file locations. :param find_links: Links from find_links. :param project_urls: URLs to HTML project pages, as described by the PEP 503 simple repository API. N)r find_links project_urls)r4rrrr(r(r)r1,s zCollectedLinks.__init__N)r6r7r8rr1r(r(r(r)rsrc@sBeZdZdZddZedddZeddZd d Z 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_links() method. cCs||_||_dS)N) search_scoperG)r4rGrr(r(r)r1GszLinkCollector.__init__FcCsb|jg|j}|jr:| r:tjddjdd|Dg}|jpBg}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|VqdS)N)r).0r&r(r(r) \sz'LinkCollector.create..)r index_urls)rGr) index_urlextra_index_urlsno_indexrOrPrkrr creater)clsrGoptionssuppress_no_indexrrrlink_collectorr(r(r)rPs    zLinkCollector.createcCs|jjS)N)rr)r4r(r(r)rkszLinkCollector.find_linkscCst||jdS)z> Fetch an HTML page containing package links. )rG)rrG)r4locationr(r(r) fetch_pagepszLinkCollector.fetch_pagec sj}|j|}t|\}}tjdd\}}ddtj||D}ddjD} fddtjdd|Dd d|DD} t| } d jt| |g} x| D]} | j d j| qWt j d j | t || | d S)zFind all available links for the given project name. :return: All the Link objects (unfiltered), as a CollectedLinks object. T)rcSsg|] }t|qSr()r )rr&r(r(r) sz/LinkCollector.collect_links..cSsg|]}t|dqS)z-f)r )rr&r(r(r)rscsg|]}jj|r|qSr()rGis_secure_origin)rr)r4r(r)rscss|]}t|ddVqdS)F)rN)r )rr&r(r(r)rsz.LinkCollector.collect_links..css|]}t|VqdS)N)r )rr&r(r(r)rsz,{} location(s) to search for versions of {}:z* {} )rrr)rget_index_urls_locationsrrrgrhrrr%rirOrPrkr) r4 project_namerindex_locationsindex_file_loc index_url_loc fl_file_loc fl_url_loc file_linksfind_link_links url_locationslinesrr()r4r) collect_linksws(    zLinkCollector.collect_linksN)F) r6r7r8rr1 classmethodrpropertyrrrr(r(r(r)r>s   r)N)T)N)F)frrSrrgloggingrrre collectionsr pip._vendorrrpip._vendor.distlib.compatrZpip._vendor.requests.exceptionsrrZpip._vendor.six.moves.urllibrrDr rapip._internal.exceptionsr pip._internal.models.linkr !pip._internal.models.search_scoper pip._internal.network.utilsr pip._internal.utils.compatrpip._internal.utils.filetypesrpip._internal.utils.miscrrpip._internal.utils.typingrpip._internal.utils.urlsrrpip._internal.vcsrroptparsertypingrrrrrrrrxml.etree.ElementTreexmlZpip._vendor.requestsr pip._internal.network.sessionr!etree ElementTreeElementZ HTMLElementrZResponseHeaders getLoggerr6rOr*r. Exceptionr/r?r@rMrQrVr\r`rdcompile IGNORECASErerrrwrobjectrrrrrrrrrrrr(r(r(r)st            (         3      9 ;#