Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/pip/_internal/models/search_scope.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import itertools | |
2 import logging | |
3 import os | |
4 import posixpath | |
5 import urllib.parse | |
6 | |
7 from pip._vendor.packaging.utils import canonicalize_name | |
8 | |
9 from pip._internal.models.index import PyPI | |
10 from pip._internal.utils.compat import has_tls | |
11 from pip._internal.utils.misc import normalize_path, redact_auth_from_url | |
12 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
13 | |
14 if MYPY_CHECK_RUNNING: | |
15 from typing import List | |
16 | |
17 | |
18 logger = logging.getLogger(__name__) | |
19 | |
20 | |
21 class SearchScope: | |
22 | |
23 """ | |
24 Encapsulates the locations that pip is configured to search. | |
25 """ | |
26 | |
27 __slots__ = ["find_links", "index_urls"] | |
28 | |
29 @classmethod | |
30 def create( | |
31 cls, | |
32 find_links, # type: List[str] | |
33 index_urls, # type: List[str] | |
34 ): | |
35 # type: (...) -> SearchScope | |
36 """ | |
37 Create a SearchScope object after normalizing the `find_links`. | |
38 """ | |
39 # Build find_links. If an argument starts with ~, it may be | |
40 # a local file relative to a home directory. So try normalizing | |
41 # it and if it exists, use the normalized version. | |
42 # This is deliberately conservative - it might be fine just to | |
43 # blindly normalize anything starting with a ~... | |
44 built_find_links = [] # type: List[str] | |
45 for link in find_links: | |
46 if link.startswith('~'): | |
47 new_link = normalize_path(link) | |
48 if os.path.exists(new_link): | |
49 link = new_link | |
50 built_find_links.append(link) | |
51 | |
52 # If we don't have TLS enabled, then WARN if anyplace we're looking | |
53 # relies on TLS. | |
54 if not has_tls(): | |
55 for link in itertools.chain(index_urls, built_find_links): | |
56 parsed = urllib.parse.urlparse(link) | |
57 if parsed.scheme == 'https': | |
58 logger.warning( | |
59 'pip is configured with locations that require ' | |
60 'TLS/SSL, however the ssl module in Python is not ' | |
61 'available.' | |
62 ) | |
63 break | |
64 | |
65 return cls( | |
66 find_links=built_find_links, | |
67 index_urls=index_urls, | |
68 ) | |
69 | |
70 def __init__( | |
71 self, | |
72 find_links, # type: List[str] | |
73 index_urls, # type: List[str] | |
74 ): | |
75 # type: (...) -> None | |
76 self.find_links = find_links | |
77 self.index_urls = index_urls | |
78 | |
79 def get_formatted_locations(self): | |
80 # type: () -> str | |
81 lines = [] | |
82 redacted_index_urls = [] | |
83 if self.index_urls and self.index_urls != [PyPI.simple_url]: | |
84 for url in self.index_urls: | |
85 | |
86 redacted_index_url = redact_auth_from_url(url) | |
87 | |
88 # Parse the URL | |
89 purl = urllib.parse.urlsplit(redacted_index_url) | |
90 | |
91 # URL is generally invalid if scheme and netloc is missing | |
92 # there are issues with Python and URL parsing, so this test | |
93 # is a bit crude. See bpo-20271, bpo-23505. Python doesn't | |
94 # always parse invalid URLs correctly - it should raise | |
95 # exceptions for malformed URLs | |
96 if not purl.scheme and not purl.netloc: | |
97 logger.warning( | |
98 'The index url "%s" seems invalid, ' | |
99 'please provide a scheme.', redacted_index_url) | |
100 | |
101 redacted_index_urls.append(redacted_index_url) | |
102 | |
103 lines.append('Looking in indexes: {}'.format( | |
104 ', '.join(redacted_index_urls))) | |
105 | |
106 if self.find_links: | |
107 lines.append( | |
108 'Looking in links: {}'.format(', '.join( | |
109 redact_auth_from_url(url) for url in self.find_links)) | |
110 ) | |
111 return '\n'.join(lines) | |
112 | |
113 def get_index_urls_locations(self, project_name): | |
114 # type: (str) -> List[str] | |
115 """Returns the locations found via self.index_urls | |
116 | |
117 Checks the url_name on the main (first in the list) index and | |
118 use this url_name to produce all locations | |
119 """ | |
120 | |
121 def mkurl_pypi_url(url): | |
122 # type: (str) -> str | |
123 loc = posixpath.join( | |
124 url, | |
125 urllib.parse.quote(canonicalize_name(project_name))) | |
126 # For maximum compatibility with easy_install, ensure the path | |
127 # ends in a trailing slash. Although this isn't in the spec | |
128 # (and PyPI can handle it without the slash) some other index | |
129 # implementations might break if they relied on easy_install's | |
130 # behavior. | |
131 if not loc.endswith('/'): | |
132 loc = loc + '/' | |
133 return loc | |
134 | |
135 return [mkurl_pypi_url(url) for url in self.index_urls] |