]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/compat.py
Annotate changelog with (some) bugs closed.
[youtubedl] / youtube_dl / compat.py
1 from __future__ import unicode_literals
2
3 import collections
4 import getpass
5 import optparse
6 import os
7 import re
8 import shutil
9 import socket
10 import subprocess
11 import sys
12 import itertools
13
14
15 try:
16 import urllib.request as compat_urllib_request
17 except ImportError: # Python 2
18 import urllib2 as compat_urllib_request
19
20 try:
21 import urllib.error as compat_urllib_error
22 except ImportError: # Python 2
23 import urllib2 as compat_urllib_error
24
25 try:
26 import urllib.parse as compat_urllib_parse
27 except ImportError: # Python 2
28 import urllib as compat_urllib_parse
29
30 try:
31 from urllib.parse import urlparse as compat_urllib_parse_urlparse
32 except ImportError: # Python 2
33 from urlparse import urlparse as compat_urllib_parse_urlparse
34
35 try:
36 import urllib.parse as compat_urlparse
37 except ImportError: # Python 2
38 import urlparse as compat_urlparse
39
40 try:
41 import http.cookiejar as compat_cookiejar
42 except ImportError: # Python 2
43 import cookielib as compat_cookiejar
44
45 try:
46 import html.entities as compat_html_entities
47 except ImportError: # Python 2
48 import htmlentitydefs as compat_html_entities
49
50 try:
51 import http.client as compat_http_client
52 except ImportError: # Python 2
53 import httplib as compat_http_client
54
55 try:
56 from urllib.error import HTTPError as compat_HTTPError
57 except ImportError: # Python 2
58 from urllib2 import HTTPError as compat_HTTPError
59
60 try:
61 from urllib.request import urlretrieve as compat_urlretrieve
62 except ImportError: # Python 2
63 from urllib import urlretrieve as compat_urlretrieve
64
65
66 try:
67 from subprocess import DEVNULL
68 compat_subprocess_get_DEVNULL = lambda: DEVNULL
69 except ImportError:
70 compat_subprocess_get_DEVNULL = lambda: open(os.path.devnull, 'w')
71
72 try:
73 import http.server as compat_http_server
74 except ImportError:
75 import BaseHTTPServer as compat_http_server
76
77 try:
78 from urllib.parse import unquote_to_bytes as compat_urllib_parse_unquote_to_bytes
79 from urllib.parse import unquote as compat_urllib_parse_unquote
80 from urllib.parse import unquote_plus as compat_urllib_parse_unquote_plus
81 except ImportError: # Python 2
82 _asciire = (compat_urllib_parse._asciire if hasattr(compat_urllib_parse, '_asciire')
83 else re.compile('([\x00-\x7f]+)'))
84
85 # HACK: The following are the correct unquote_to_bytes, unquote and unquote_plus
86 # implementations from cpython 3.4.3's stdlib. Python 2's version
87 # is apparently broken (see https://github.com/rg3/youtube-dl/pull/6244)
88
89 def compat_urllib_parse_unquote_to_bytes(string):
90 """unquote_to_bytes('abc%20def') -> b'abc def'."""
91 # Note: strings are encoded as UTF-8. This is only an issue if it contains
92 # unescaped non-ASCII characters, which URIs should not.
93 if not string:
94 # Is it a string-like object?
95 string.split
96 return b''
97 if isinstance(string, unicode):
98 string = string.encode('utf-8')
99 bits = string.split(b'%')
100 if len(bits) == 1:
101 return string
102 res = [bits[0]]
103 append = res.append
104 for item in bits[1:]:
105 try:
106 append(compat_urllib_parse._hextochr[item[:2]])
107 append(item[2:])
108 except KeyError:
109 append(b'%')
110 append(item)
111 return b''.join(res)
112
113 def compat_urllib_parse_unquote(string, encoding='utf-8', errors='replace'):
114 """Replace %xx escapes by their single-character equivalent. The optional
115 encoding and errors parameters specify how to decode percent-encoded
116 sequences into Unicode characters, as accepted by the bytes.decode()
117 method.
118 By default, percent-encoded sequences are decoded with UTF-8, and invalid
119 sequences are replaced by a placeholder character.
120
121 unquote('abc%20def') -> 'abc def'.
122 """
123 if '%' not in string:
124 string.split
125 return string
126 if encoding is None:
127 encoding = 'utf-8'
128 if errors is None:
129 errors = 'replace'
130 bits = _asciire.split(string)
131 res = [bits[0]]
132 append = res.append
133 for i in range(1, len(bits), 2):
134 append(compat_urllib_parse_unquote_to_bytes(bits[i]).decode(encoding, errors))
135 append(bits[i + 1])
136 return ''.join(res)
137
138 def compat_urllib_parse_unquote_plus(string, encoding='utf-8', errors='replace'):
139 """Like unquote(), but also replace plus signs by spaces, as required for
140 unquoting HTML form values.
141
142 unquote_plus('%7e/abc+def') -> '~/abc def'
143 """
144 string = string.replace('+', ' ')
145 return compat_urllib_parse_unquote(string, encoding, errors)
146
147 try:
148 compat_str = unicode # Python 2
149 except NameError:
150 compat_str = str
151
152 try:
153 compat_basestring = basestring # Python 2
154 except NameError:
155 compat_basestring = str
156
157 try:
158 compat_chr = unichr # Python 2
159 except NameError:
160 compat_chr = chr
161
162 try:
163 from xml.etree.ElementTree import ParseError as compat_xml_parse_error
164 except ImportError: # Python 2.6
165 from xml.parsers.expat import ExpatError as compat_xml_parse_error
166
167
168 try:
169 from urllib.parse import parse_qs as compat_parse_qs
170 except ImportError: # Python 2
171 # HACK: The following is the correct parse_qs implementation from cpython 3's stdlib.
172 # Python 2's version is apparently totally broken
173
174 def _parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
175 encoding='utf-8', errors='replace'):
176 qs, _coerce_result = qs, compat_str
177 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
178 r = []
179 for name_value in pairs:
180 if not name_value and not strict_parsing:
181 continue
182 nv = name_value.split('=', 1)
183 if len(nv) != 2:
184 if strict_parsing:
185 raise ValueError("bad query field: %r" % (name_value,))
186 # Handle case of a control-name with no equal sign
187 if keep_blank_values:
188 nv.append('')
189 else:
190 continue
191 if len(nv[1]) or keep_blank_values:
192 name = nv[0].replace('+', ' ')
193 name = compat_urllib_parse_unquote(
194 name, encoding=encoding, errors=errors)
195 name = _coerce_result(name)
196 value = nv[1].replace('+', ' ')
197 value = compat_urllib_parse_unquote(
198 value, encoding=encoding, errors=errors)
199 value = _coerce_result(value)
200 r.append((name, value))
201 return r
202
203 def compat_parse_qs(qs, keep_blank_values=False, strict_parsing=False,
204 encoding='utf-8', errors='replace'):
205 parsed_result = {}
206 pairs = _parse_qsl(qs, keep_blank_values, strict_parsing,
207 encoding=encoding, errors=errors)
208 for name, value in pairs:
209 if name in parsed_result:
210 parsed_result[name].append(value)
211 else:
212 parsed_result[name] = [value]
213 return parsed_result
214
215 try:
216 from shlex import quote as shlex_quote
217 except ImportError: # Python < 3.3
218 def shlex_quote(s):
219 if re.match(r'^[-_\w./]+$', s):
220 return s
221 else:
222 return "'" + s.replace("'", "'\"'\"'") + "'"
223
224
225 def compat_ord(c):
226 if type(c) is int:
227 return c
228 else:
229 return ord(c)
230
231
232 if sys.version_info >= (3, 0):
233 compat_getenv = os.getenv
234 compat_expanduser = os.path.expanduser
235 else:
236 # Environment variables should be decoded with filesystem encoding.
237 # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
238
239 def compat_getenv(key, default=None):
240 from .utils import get_filesystem_encoding
241 env = os.getenv(key, default)
242 if env:
243 env = env.decode(get_filesystem_encoding())
244 return env
245
246 # HACK: The default implementations of os.path.expanduser from cpython do not decode
247 # environment variables with filesystem encoding. We will work around this by
248 # providing adjusted implementations.
249 # The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
250 # for different platforms with correct environment variables decoding.
251
252 if os.name == 'posix':
253 def compat_expanduser(path):
254 """Expand ~ and ~user constructions. If user or $HOME is unknown,
255 do nothing."""
256 if not path.startswith('~'):
257 return path
258 i = path.find('/', 1)
259 if i < 0:
260 i = len(path)
261 if i == 1:
262 if 'HOME' not in os.environ:
263 import pwd
264 userhome = pwd.getpwuid(os.getuid()).pw_dir
265 else:
266 userhome = compat_getenv('HOME')
267 else:
268 import pwd
269 try:
270 pwent = pwd.getpwnam(path[1:i])
271 except KeyError:
272 return path
273 userhome = pwent.pw_dir
274 userhome = userhome.rstrip('/')
275 return (userhome + path[i:]) or '/'
276 elif os.name == 'nt' or os.name == 'ce':
277 def compat_expanduser(path):
278 """Expand ~ and ~user constructs.
279
280 If user or $HOME is unknown, do nothing."""
281 if path[:1] != '~':
282 return path
283 i, n = 1, len(path)
284 while i < n and path[i] not in '/\\':
285 i = i + 1
286
287 if 'HOME' in os.environ:
288 userhome = compat_getenv('HOME')
289 elif 'USERPROFILE' in os.environ:
290 userhome = compat_getenv('USERPROFILE')
291 elif 'HOMEPATH' not in os.environ:
292 return path
293 else:
294 try:
295 drive = compat_getenv('HOMEDRIVE')
296 except KeyError:
297 drive = ''
298 userhome = os.path.join(drive, compat_getenv('HOMEPATH'))
299
300 if i != 1: # ~user
301 userhome = os.path.join(os.path.dirname(userhome), path[1:i])
302
303 return userhome + path[i:]
304 else:
305 compat_expanduser = os.path.expanduser
306
307
308 if sys.version_info < (3, 0):
309 def compat_print(s):
310 from .utils import preferredencoding
311 print(s.encode(preferredencoding(), 'xmlcharrefreplace'))
312 else:
313 def compat_print(s):
314 assert isinstance(s, compat_str)
315 print(s)
316
317
318 try:
319 subprocess_check_output = subprocess.check_output
320 except AttributeError:
321 def subprocess_check_output(*args, **kwargs):
322 assert 'input' not in kwargs
323 p = subprocess.Popen(*args, stdout=subprocess.PIPE, **kwargs)
324 output, _ = p.communicate()
325 ret = p.poll()
326 if ret:
327 raise subprocess.CalledProcessError(ret, p.args, output=output)
328 return output
329
330 if sys.version_info < (3, 0) and sys.platform == 'win32':
331 def compat_getpass(prompt, *args, **kwargs):
332 if isinstance(prompt, compat_str):
333 from .utils import preferredencoding
334 prompt = prompt.encode(preferredencoding())
335 return getpass.getpass(prompt, *args, **kwargs)
336 else:
337 compat_getpass = getpass.getpass
338
339 # Old 2.6 and 2.7 releases require kwargs to be bytes
340 try:
341 def _testfunc(x):
342 pass
343 _testfunc(**{'x': 0})
344 except TypeError:
345 def compat_kwargs(kwargs):
346 return dict((bytes(k), v) for k, v in kwargs.items())
347 else:
348 compat_kwargs = lambda kwargs: kwargs
349
350
351 if sys.version_info < (2, 7):
352 def compat_socket_create_connection(address, timeout, source_address=None):
353 host, port = address
354 err = None
355 for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
356 af, socktype, proto, canonname, sa = res
357 sock = None
358 try:
359 sock = socket.socket(af, socktype, proto)
360 sock.settimeout(timeout)
361 if source_address:
362 sock.bind(source_address)
363 sock.connect(sa)
364 return sock
365 except socket.error as _:
366 err = _
367 if sock is not None:
368 sock.close()
369 if err is not None:
370 raise err
371 else:
372 raise socket.error("getaddrinfo returns an empty list")
373 else:
374 compat_socket_create_connection = socket.create_connection
375
376
377 # Fix https://github.com/rg3/youtube-dl/issues/4223
378 # See http://bugs.python.org/issue9161 for what is broken
379 def workaround_optparse_bug9161():
380 op = optparse.OptionParser()
381 og = optparse.OptionGroup(op, 'foo')
382 try:
383 og.add_option('-t')
384 except TypeError:
385 real_add_option = optparse.OptionGroup.add_option
386
387 def _compat_add_option(self, *args, **kwargs):
388 enc = lambda v: (
389 v.encode('ascii', 'replace') if isinstance(v, compat_str)
390 else v)
391 bargs = [enc(a) for a in args]
392 bkwargs = dict(
393 (k, enc(v)) for k, v in kwargs.items())
394 return real_add_option(self, *bargs, **bkwargs)
395 optparse.OptionGroup.add_option = _compat_add_option
396
397 if hasattr(shutil, 'get_terminal_size'): # Python >= 3.3
398 compat_get_terminal_size = shutil.get_terminal_size
399 else:
400 _terminal_size = collections.namedtuple('terminal_size', ['columns', 'lines'])
401
402 def compat_get_terminal_size():
403 columns = compat_getenv('COLUMNS', None)
404 if columns:
405 columns = int(columns)
406 else:
407 columns = None
408 lines = compat_getenv('LINES', None)
409 if lines:
410 lines = int(lines)
411 else:
412 lines = None
413
414 try:
415 sp = subprocess.Popen(
416 ['stty', 'size'],
417 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
418 out, err = sp.communicate()
419 lines, columns = map(int, out.split())
420 except Exception:
421 pass
422 return _terminal_size(columns, lines)
423
424 try:
425 itertools.count(start=0, step=1)
426 compat_itertools_count = itertools.count
427 except TypeError: # Python 2.6
428 def compat_itertools_count(start=0, step=1):
429 n = start
430 while True:
431 yield n
432 n += step
433
434 __all__ = [
435 'compat_HTTPError',
436 'compat_basestring',
437 'compat_chr',
438 'compat_cookiejar',
439 'compat_expanduser',
440 'compat_get_terminal_size',
441 'compat_getenv',
442 'compat_getpass',
443 'compat_html_entities',
444 'compat_http_client',
445 'compat_http_server',
446 'compat_itertools_count',
447 'compat_kwargs',
448 'compat_ord',
449 'compat_parse_qs',
450 'compat_print',
451 'compat_socket_create_connection',
452 'compat_str',
453 'compat_subprocess_get_DEVNULL',
454 'compat_urllib_error',
455 'compat_urllib_parse',
456 'compat_urllib_parse_unquote',
457 'compat_urllib_parse_unquote_plus',
458 'compat_urllib_parse_unquote_to_bytes',
459 'compat_urllib_parse_urlparse',
460 'compat_urllib_request',
461 'compat_urlparse',
462 'compat_urlretrieve',
463 'compat_xml_parse_error',
464 'shlex_quote',
465 'subprocess_check_output',
466 'workaround_optparse_bug9161',
467 ]