diff options
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/cache.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/cache.py | 202 |
1 files changed, 0 insertions, 202 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/cache.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/cache.py deleted file mode 100644 index 5547d73..0000000 --- a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/cache.py +++ /dev/null | |||
@@ -1,202 +0,0 @@ | |||
1 | """Cache Management | ||
2 | """ | ||
3 | |||
4 | import errno | ||
5 | import hashlib | ||
6 | import logging | ||
7 | import os | ||
8 | |||
9 | from pip._vendor.packaging.utils import canonicalize_name | ||
10 | |||
11 | from pip._internal import index | ||
12 | from pip._internal.compat import expanduser | ||
13 | from pip._internal.download import path_to_url | ||
14 | from pip._internal.utils.temp_dir import TempDirectory | ||
15 | from pip._internal.wheel import InvalidWheelFilename, Wheel | ||
16 | |||
17 | logger = logging.getLogger(__name__) | ||
18 | |||
19 | |||
20 | class Cache(object): | ||
21 | """An abstract class - provides cache directories for data from links | ||
22 | |||
23 | |||
24 | :param cache_dir: The root of the cache. | ||
25 | :param format_control: A pip.index.FormatControl object to limit | ||
26 | binaries being read from the cache. | ||
27 | :param allowed_formats: which formats of files the cache should store. | ||
28 | ('binary' and 'source' are the only allowed values) | ||
29 | """ | ||
30 | |||
31 | def __init__(self, cache_dir, format_control, allowed_formats): | ||
32 | super(Cache, self).__init__() | ||
33 | self.cache_dir = expanduser(cache_dir) if cache_dir else None | ||
34 | self.format_control = format_control | ||
35 | self.allowed_formats = allowed_formats | ||
36 | |||
37 | _valid_formats = {"source", "binary"} | ||
38 | assert self.allowed_formats.union(_valid_formats) == _valid_formats | ||
39 | |||
40 | def _get_cache_path_parts(self, link): | ||
41 | """Get parts of part that must be os.path.joined with cache_dir | ||
42 | """ | ||
43 | |||
44 | # We want to generate an url to use as our cache key, we don't want to | ||
45 | # just re-use the URL because it might have other items in the fragment | ||
46 | # and we don't care about those. | ||
47 | key_parts = [link.url_without_fragment] | ||
48 | if link.hash_name is not None and link.hash is not None: | ||
49 | key_parts.append("=".join([link.hash_name, link.hash])) | ||
50 | key_url = "#".join(key_parts) | ||
51 | |||
52 | # Encode our key url with sha224, we'll use this because it has similar | ||
53 | # security properties to sha256, but with a shorter total output (and | ||
54 | # thus less secure). However the differences don't make a lot of | ||
55 | # difference for our use case here. | ||
56 | hashed = hashlib.sha224(key_url.encode()).hexdigest() | ||
57 | |||
58 | # We want to nest the directories some to prevent having a ton of top | ||
59 | # level directories where we might run out of sub directories on some | ||
60 | # FS. | ||
61 | parts = [hashed[:2], hashed[2:4], hashed[4:6], hashed[6:]] | ||
62 | |||
63 | return parts | ||
64 | |||
65 | def _get_candidates(self, link, package_name): | ||
66 | can_not_cache = ( | ||
67 | not self.cache_dir or | ||
68 | not package_name or | ||
69 | not link | ||
70 | ) | ||
71 | if can_not_cache: | ||
72 | return [] | ||
73 | |||
74 | canonical_name = canonicalize_name(package_name) | ||
75 | formats = index.fmt_ctl_formats( | ||
76 | self.format_control, canonical_name | ||
77 | ) | ||
78 | if not self.allowed_formats.intersection(formats): | ||
79 | return [] | ||
80 | |||
81 | root = self.get_path_for_link(link) | ||
82 | try: | ||
83 | return os.listdir(root) | ||
84 | except OSError as err: | ||
85 | if err.errno in {errno.ENOENT, errno.ENOTDIR}: | ||
86 | return [] | ||
87 | raise | ||
88 | |||
89 | def get_path_for_link(self, link): | ||
90 | """Return a directory to store cached items in for link. | ||
91 | """ | ||
92 | raise NotImplementedError() | ||
93 | |||
94 | def get(self, link, package_name): | ||
95 | """Returns a link to a cached item if it exists, otherwise returns the | ||
96 | passed link. | ||
97 | """ | ||
98 | raise NotImplementedError() | ||
99 | |||
100 | def _link_for_candidate(self, link, candidate): | ||
101 | root = self.get_path_for_link(link) | ||
102 | path = os.path.join(root, candidate) | ||
103 | |||
104 | return index.Link(path_to_url(path)) | ||
105 | |||
106 | def cleanup(self): | ||
107 | pass | ||
108 | |||
109 | |||
110 | class SimpleWheelCache(Cache): | ||
111 | """A cache of wheels for future installs. | ||
112 | """ | ||
113 | |||
114 | def __init__(self, cache_dir, format_control): | ||
115 | super(SimpleWheelCache, self).__init__( | ||
116 | cache_dir, format_control, {"binary"} | ||
117 | ) | ||
118 | |||
119 | def get_path_for_link(self, link): | ||
120 | """Return a directory to store cached wheels for link | ||
121 | |||
122 | Because there are M wheels for any one sdist, we provide a directory | ||
123 | to cache them in, and then consult that directory when looking up | ||
124 | cache hits. | ||
125 | |||
126 | We only insert things into the cache if they have plausible version | ||
127 | numbers, so that we don't contaminate the cache with things that were | ||
128 | not unique. E.g. ./package might have dozens of installs done for it | ||
129 | and build a version of 0.0...and if we built and cached a wheel, we'd | ||
130 | end up using the same wheel even if the source has been edited. | ||
131 | |||
132 | :param link: The link of the sdist for which this will cache wheels. | ||
133 | """ | ||
134 | parts = self._get_cache_path_parts(link) | ||
135 | |||
136 | # Store wheels within the root cache_dir | ||
137 | return os.path.join(self.cache_dir, "wheels", *parts) | ||
138 | |||
139 | def get(self, link, package_name): | ||
140 | candidates = [] | ||
141 | |||
142 | for wheel_name in self._get_candidates(link, package_name): | ||
143 | try: | ||
144 | wheel = Wheel(wheel_name) | ||
145 | except InvalidWheelFilename: | ||
146 | continue | ||
147 | if not wheel.supported(): | ||
148 | # Built for a different python/arch/etc | ||
149 | continue | ||
150 | candidates.append((wheel.support_index_min(), wheel_name)) | ||
151 | |||
152 | if not candidates: | ||
153 | return link | ||
154 | |||
155 | return self._link_for_candidate(link, min(candidates)[1]) | ||
156 | |||
157 | |||
158 | class EphemWheelCache(SimpleWheelCache): | ||
159 | """A SimpleWheelCache that creates it's own temporary cache directory | ||
160 | """ | ||
161 | |||
162 | def __init__(self, format_control): | ||
163 | self._temp_dir = TempDirectory(kind="ephem-wheel-cache") | ||
164 | self._temp_dir.create() | ||
165 | |||
166 | super(EphemWheelCache, self).__init__( | ||
167 | self._temp_dir.path, format_control | ||
168 | ) | ||
169 | |||
170 | def cleanup(self): | ||
171 | self._temp_dir.cleanup() | ||
172 | |||
173 | |||
174 | class WheelCache(Cache): | ||
175 | """Wraps EphemWheelCache and SimpleWheelCache into a single Cache | ||
176 | |||
177 | This Cache allows for gracefully degradation, using the ephem wheel cache | ||
178 | when a certain link is not found in the simple wheel cache first. | ||
179 | """ | ||
180 | |||
181 | def __init__(self, cache_dir, format_control): | ||
182 | super(WheelCache, self).__init__( | ||
183 | cache_dir, format_control, {'binary'} | ||
184 | ) | ||
185 | self._wheel_cache = SimpleWheelCache(cache_dir, format_control) | ||
186 | self._ephem_cache = EphemWheelCache(format_control) | ||
187 | |||
188 | def get_path_for_link(self, link): | ||
189 | return self._wheel_cache.get_path_for_link(link) | ||
190 | |||
191 | def get_ephem_path_for_link(self, link): | ||
192 | return self._ephem_cache.get_path_for_link(link) | ||
193 | |||
194 | def get(self, link, package_name): | ||
195 | retval = self._wheel_cache.get(link, package_name) | ||
196 | if retval is link: | ||
197 | retval = self._ephem_cache.get(link, package_name) | ||
198 | return retval | ||
199 | |||
200 | def cleanup(self): | ||
201 | self._wheel_cache.cleanup() | ||
202 | self._ephem_cache.cleanup() | ||