diff options
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/caches/file_cache.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/caches/file_cache.py | 133 |
1 files changed, 0 insertions, 133 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/caches/file_cache.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/caches/file_cache.py deleted file mode 100644 index f7eb890..0000000 --- a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/caches/file_cache.py +++ /dev/null | |||
@@ -1,133 +0,0 @@ | |||
1 | import hashlib | ||
2 | import os | ||
3 | from textwrap import dedent | ||
4 | |||
5 | from ..cache import BaseCache | ||
6 | from ..controller import CacheController | ||
7 | |||
8 | try: | ||
9 | FileNotFoundError | ||
10 | except NameError: | ||
11 | # py2.X | ||
12 | FileNotFoundError = OSError | ||
13 | |||
14 | |||
15 | def _secure_open_write(filename, fmode): | ||
16 | # We only want to write to this file, so open it in write only mode | ||
17 | flags = os.O_WRONLY | ||
18 | |||
19 | # os.O_CREAT | os.O_EXCL will fail if the file already exists, so we only | ||
20 | # will open *new* files. | ||
21 | # We specify this because we want to ensure that the mode we pass is the | ||
22 | # mode of the file. | ||
23 | flags |= os.O_CREAT | os.O_EXCL | ||
24 | |||
25 | # Do not follow symlinks to prevent someone from making a symlink that | ||
26 | # we follow and insecurely open a cache file. | ||
27 | if hasattr(os, "O_NOFOLLOW"): | ||
28 | flags |= os.O_NOFOLLOW | ||
29 | |||
30 | # On Windows we'll mark this file as binary | ||
31 | if hasattr(os, "O_BINARY"): | ||
32 | flags |= os.O_BINARY | ||
33 | |||
34 | # Before we open our file, we want to delete any existing file that is | ||
35 | # there | ||
36 | try: | ||
37 | os.remove(filename) | ||
38 | except (IOError, OSError): | ||
39 | # The file must not exist already, so we can just skip ahead to opening | ||
40 | pass | ||
41 | |||
42 | # Open our file, the use of os.O_CREAT | os.O_EXCL will ensure that if a | ||
43 | # race condition happens between the os.remove and this line, that an | ||
44 | # error will be raised. Because we utilize a lockfile this should only | ||
45 | # happen if someone is attempting to attack us. | ||
46 | fd = os.open(filename, flags, fmode) | ||
47 | try: | ||
48 | return os.fdopen(fd, "wb") | ||
49 | except: | ||
50 | # An error occurred wrapping our FD in a file object | ||
51 | os.close(fd) | ||
52 | raise | ||
53 | |||
54 | |||
55 | class FileCache(BaseCache): | ||
56 | def __init__(self, directory, forever=False, filemode=0o0600, | ||
57 | dirmode=0o0700, use_dir_lock=None, lock_class=None): | ||
58 | |||
59 | if use_dir_lock is not None and lock_class is not None: | ||
60 | raise ValueError("Cannot use use_dir_lock and lock_class together") | ||
61 | |||
62 | try: | ||
63 | from pip._vendor.lockfile import LockFile | ||
64 | from pip._vendor.lockfile.mkdirlockfile import MkdirLockFile | ||
65 | except ImportError: | ||
66 | notice = dedent(""" | ||
67 | NOTE: In order to use the FileCache you must have | ||
68 | lockfile installed. You can install it via pip: | ||
69 | pip install lockfile | ||
70 | """) | ||
71 | raise ImportError(notice) | ||
72 | else: | ||
73 | if use_dir_lock: | ||
74 | lock_class = MkdirLockFile | ||
75 | |||
76 | elif lock_class is None: | ||
77 | lock_class = LockFile | ||
78 | |||
79 | self.directory = directory | ||
80 | self.forever = forever | ||
81 | self.filemode = filemode | ||
82 | self.dirmode = dirmode | ||
83 | self.lock_class = lock_class | ||
84 | |||
85 | @staticmethod | ||
86 | def encode(x): | ||
87 | return hashlib.sha224(x.encode()).hexdigest() | ||
88 | |||
89 | def _fn(self, name): | ||
90 | # NOTE: This method should not change as some may depend on it. | ||
91 | # See: https://github.com/ionrock/cachecontrol/issues/63 | ||
92 | hashed = self.encode(name) | ||
93 | parts = list(hashed[:5]) + [hashed] | ||
94 | return os.path.join(self.directory, *parts) | ||
95 | |||
96 | def get(self, key): | ||
97 | name = self._fn(key) | ||
98 | if not os.path.exists(name): | ||
99 | return None | ||
100 | |||
101 | with open(name, 'rb') as fh: | ||
102 | return fh.read() | ||
103 | |||
104 | def set(self, key, value): | ||
105 | name = self._fn(key) | ||
106 | |||
107 | # Make sure the directory exists | ||
108 | try: | ||
109 | os.makedirs(os.path.dirname(name), self.dirmode) | ||
110 | except (IOError, OSError): | ||
111 | pass | ||
112 | |||
113 | with self.lock_class(name) as lock: | ||
114 | # Write our actual file | ||
115 | with _secure_open_write(lock.path, self.filemode) as fh: | ||
116 | fh.write(value) | ||
117 | |||
118 | def delete(self, key): | ||
119 | name = self._fn(key) | ||
120 | if not self.forever: | ||
121 | try: | ||
122 | os.remove(name) | ||
123 | except FileNotFoundError: | ||
124 | pass | ||
125 | |||
126 | |||
127 | def url_to_file_path(url, filecache): | ||
128 | """Return the file cache path based on the URL. | ||
129 | |||
130 | This does not ensure the file exists! | ||
131 | """ | ||
132 | key = CacheController.cache_url(url) | ||
133 | return filecache._fn(key) | ||