diff options
author | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
---|---|---|
committer | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
commit | 68df54d6629ec019142eb149dd037774f2d11e7c (patch) | |
tree | 345bc22d46b4e01a4ba8303b94278952a4ed2b9e /venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/compat.py |
First commit
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/compat.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/compat.py | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/compat.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/compat.py new file mode 100644 index 0000000..064717d --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/compat.py | |||
@@ -0,0 +1,235 @@ | |||
1 | """Stuff that differs in different Python versions and platform | ||
2 | distributions.""" | ||
3 | from __future__ import absolute_import, division | ||
4 | |||
5 | import codecs | ||
6 | import locale | ||
7 | import logging | ||
8 | import os | ||
9 | import shutil | ||
10 | import sys | ||
11 | |||
12 | from pip._vendor.six import text_type | ||
13 | |||
14 | try: | ||
15 | import ipaddress | ||
16 | except ImportError: | ||
17 | try: | ||
18 | from pip._vendor import ipaddress # type: ignore | ||
19 | except ImportError: | ||
20 | import ipaddr as ipaddress # type: ignore | ||
21 | ipaddress.ip_address = ipaddress.IPAddress | ||
22 | ipaddress.ip_network = ipaddress.IPNetwork | ||
23 | |||
24 | |||
25 | __all__ = [ | ||
26 | "ipaddress", "uses_pycache", "console_to_str", "native_str", | ||
27 | "get_path_uid", "stdlib_pkgs", "WINDOWS", "samefile", "get_terminal_size", | ||
28 | ] | ||
29 | |||
30 | |||
31 | logger = logging.getLogger(__name__) | ||
32 | |||
33 | if sys.version_info >= (3, 4): | ||
34 | uses_pycache = True | ||
35 | from importlib.util import cache_from_source | ||
36 | else: | ||
37 | import imp | ||
38 | |||
39 | try: | ||
40 | cache_from_source = imp.cache_from_source # type: ignore | ||
41 | except AttributeError: | ||
42 | # does not use __pycache__ | ||
43 | cache_from_source = None | ||
44 | |||
45 | uses_pycache = cache_from_source is not None | ||
46 | |||
47 | |||
48 | if sys.version_info >= (3, 5): | ||
49 | backslashreplace_decode = "backslashreplace" | ||
50 | else: | ||
51 | # In version 3.4 and older, backslashreplace exists | ||
52 | # but does not support use for decoding. | ||
53 | # We implement our own replace handler for this | ||
54 | # situation, so that we can consistently use | ||
55 | # backslash replacement for all versions. | ||
56 | def backslashreplace_decode_fn(err): | ||
57 | raw_bytes = (err.object[i] for i in range(err.start, err.end)) | ||
58 | if sys.version_info[0] == 2: | ||
59 | # Python 2 gave us characters - convert to numeric bytes | ||
60 | raw_bytes = (ord(b) for b in raw_bytes) | ||
61 | return u"".join(u"\\x%x" % c for c in raw_bytes), err.end | ||
62 | codecs.register_error( | ||
63 | "backslashreplace_decode", | ||
64 | backslashreplace_decode_fn, | ||
65 | ) | ||
66 | backslashreplace_decode = "backslashreplace_decode" | ||
67 | |||
68 | |||
69 | def console_to_str(data): | ||
70 | """Return a string, safe for output, of subprocess output. | ||
71 | |||
72 | We assume the data is in the locale preferred encoding. | ||
73 | If it won't decode properly, we warn the user but decode as | ||
74 | best we can. | ||
75 | |||
76 | We also ensure that the output can be safely written to | ||
77 | standard output without encoding errors. | ||
78 | """ | ||
79 | |||
80 | # First, get the encoding we assume. This is the preferred | ||
81 | # encoding for the locale, unless that is not found, or | ||
82 | # it is ASCII, in which case assume UTF-8 | ||
83 | encoding = locale.getpreferredencoding() | ||
84 | if (not encoding) or codecs.lookup(encoding).name == "ascii": | ||
85 | encoding = "utf-8" | ||
86 | |||
87 | # Now try to decode the data - if we fail, warn the user and | ||
88 | # decode with replacement. | ||
89 | try: | ||
90 | s = data.decode(encoding) | ||
91 | except UnicodeDecodeError: | ||
92 | logger.warning( | ||
93 | "Subprocess output does not appear to be encoded as %s", | ||
94 | encoding, | ||
95 | ) | ||
96 | s = data.decode(encoding, errors=backslashreplace_decode) | ||
97 | |||
98 | # Make sure we can print the output, by encoding it to the output | ||
99 | # encoding with replacement of unencodable characters, and then | ||
100 | # decoding again. | ||
101 | # We use stderr's encoding because it's less likely to be | ||
102 | # redirected and if we don't find an encoding we skip this | ||
103 | # step (on the assumption that output is wrapped by something | ||
104 | # that won't fail). | ||
105 | # The double getattr is to deal with the possibility that we're | ||
106 | # being called in a situation where sys.__stderr__ doesn't exist, | ||
107 | # or doesn't have an encoding attribute. Neither of these cases | ||
108 | # should occur in normal pip use, but there's no harm in checking | ||
109 | # in case people use pip in (unsupported) unusual situations. | ||
110 | output_encoding = getattr(getattr(sys, "__stderr__", None), | ||
111 | "encoding", None) | ||
112 | |||
113 | if output_encoding: | ||
114 | s = s.encode(output_encoding, errors="backslashreplace") | ||
115 | s = s.decode(output_encoding) | ||
116 | |||
117 | return s | ||
118 | |||
119 | |||
120 | if sys.version_info >= (3,): | ||
121 | def native_str(s, replace=False): | ||
122 | if isinstance(s, bytes): | ||
123 | return s.decode('utf-8', 'replace' if replace else 'strict') | ||
124 | return s | ||
125 | |||
126 | else: | ||
127 | def native_str(s, replace=False): | ||
128 | # Replace is ignored -- unicode to UTF-8 can't fail | ||
129 | if isinstance(s, text_type): | ||
130 | return s.encode('utf-8') | ||
131 | return s | ||
132 | |||
133 | |||
134 | def get_path_uid(path): | ||
135 | """ | ||
136 | Return path's uid. | ||
137 | |||
138 | Does not follow symlinks: | ||
139 | https://github.com/pypa/pip/pull/935#discussion_r5307003 | ||
140 | |||
141 | Placed this function in compat due to differences on AIX and | ||
142 | Jython, that should eventually go away. | ||
143 | |||
144 | :raises OSError: When path is a symlink or can't be read. | ||
145 | """ | ||
146 | if hasattr(os, 'O_NOFOLLOW'): | ||
147 | fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW) | ||
148 | file_uid = os.fstat(fd).st_uid | ||
149 | os.close(fd) | ||
150 | else: # AIX and Jython | ||
151 | # WARNING: time of check vulnerability, but best we can do w/o NOFOLLOW | ||
152 | if not os.path.islink(path): | ||
153 | # older versions of Jython don't have `os.fstat` | ||
154 | file_uid = os.stat(path).st_uid | ||
155 | else: | ||
156 | # raise OSError for parity with os.O_NOFOLLOW above | ||
157 | raise OSError( | ||
158 | "%s is a symlink; Will not return uid for symlinks" % path | ||
159 | ) | ||
160 | return file_uid | ||
161 | |||
162 | |||
163 | def expanduser(path): | ||
164 | """ | ||
165 | Expand ~ and ~user constructions. | ||
166 | |||
167 | Includes a workaround for http://bugs.python.org/issue14768 | ||
168 | """ | ||
169 | expanded = os.path.expanduser(path) | ||
170 | if path.startswith('~/') and expanded.startswith('//'): | ||
171 | expanded = expanded[1:] | ||
172 | return expanded | ||
173 | |||
174 | |||
175 | # packages in the stdlib that may have installation metadata, but should not be | ||
176 | # considered 'installed'. this theoretically could be determined based on | ||
177 | # dist.location (py27:`sysconfig.get_paths()['stdlib']`, | ||
178 | # py26:sysconfig.get_config_vars('LIBDEST')), but fear platform variation may | ||
179 | # make this ineffective, so hard-coding | ||
180 | stdlib_pkgs = {"python", "wsgiref", "argparse"} | ||
181 | |||
182 | |||
183 | # windows detection, covers cpython and ironpython | ||
184 | WINDOWS = (sys.platform.startswith("win") or | ||
185 | (sys.platform == 'cli' and os.name == 'nt')) | ||
186 | |||
187 | |||
188 | def samefile(file1, file2): | ||
189 | """Provide an alternative for os.path.samefile on Windows/Python2""" | ||
190 | if hasattr(os.path, 'samefile'): | ||
191 | return os.path.samefile(file1, file2) | ||
192 | else: | ||
193 | path1 = os.path.normcase(os.path.abspath(file1)) | ||
194 | path2 = os.path.normcase(os.path.abspath(file2)) | ||
195 | return path1 == path2 | ||
196 | |||
197 | |||
198 | if hasattr(shutil, 'get_terminal_size'): | ||
199 | def get_terminal_size(): | ||
200 | """ | ||
201 | Returns a tuple (x, y) representing the width(x) and the height(y) | ||
202 | in characters of the terminal window. | ||
203 | """ | ||
204 | return tuple(shutil.get_terminal_size()) | ||
205 | else: | ||
206 | def get_terminal_size(): | ||
207 | """ | ||
208 | Returns a tuple (x, y) representing the width(x) and the height(y) | ||
209 | in characters of the terminal window. | ||
210 | """ | ||
211 | def ioctl_GWINSZ(fd): | ||
212 | try: | ||
213 | import fcntl | ||
214 | import termios | ||
215 | import struct | ||
216 | cr = struct.unpack_from( | ||
217 | 'hh', | ||
218 | fcntl.ioctl(fd, termios.TIOCGWINSZ, '12345678') | ||
219 | ) | ||
220 | except: | ||
221 | return None | ||
222 | if cr == (0, 0): | ||
223 | return None | ||
224 | return cr | ||
225 | cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) | ||
226 | if not cr: | ||
227 | try: | ||
228 | fd = os.open(os.ctermid(), os.O_RDONLY) | ||
229 | cr = ioctl_GWINSZ(fd) | ||
230 | os.close(fd) | ||
231 | except: | ||
232 | pass | ||
233 | if not cr: | ||
234 | cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80)) | ||
235 | return int(cr[1]), int(cr[0]) | ||