Merge remote-tracking branch 'rzhxeo/f4m-drm'
[youtube-dl] / youtube_dl / compat.py
1 from __future__ import unicode_literals
2
3 import getpass
4 import optparse
5 import os
6 import re
7 import socket
8 import subprocess
9 import sys
10
11
12 try:
13     import urllib.request as compat_urllib_request
14 except ImportError:  # Python 2
15     import urllib2 as compat_urllib_request
16
17 try:
18     import urllib.error as compat_urllib_error
19 except ImportError:  # Python 2
20     import urllib2 as compat_urllib_error
21
22 try:
23     import urllib.parse as compat_urllib_parse
24 except ImportError:  # Python 2
25     import urllib as compat_urllib_parse
26
27 try:
28     from urllib.parse import urlparse as compat_urllib_parse_urlparse
29 except ImportError:  # Python 2
30     from urlparse import urlparse as compat_urllib_parse_urlparse
31
32 try:
33     import urllib.parse as compat_urlparse
34 except ImportError:  # Python 2
35     import urlparse as compat_urlparse
36
37 try:
38     import http.cookiejar as compat_cookiejar
39 except ImportError:  # Python 2
40     import cookielib as compat_cookiejar
41
42 try:
43     import html.entities as compat_html_entities
44 except ImportError:  # Python 2
45     import htmlentitydefs as compat_html_entities
46
47 try:
48     import html.parser as compat_html_parser
49 except ImportError:  # Python 2
50     import HTMLParser as compat_html_parser
51
52 try:
53     import http.client as compat_http_client
54 except ImportError:  # Python 2
55     import httplib as compat_http_client
56
57 try:
58     from urllib.error import HTTPError as compat_HTTPError
59 except ImportError:  # Python 2
60     from urllib2 import HTTPError as compat_HTTPError
61
62 try:
63     from urllib.request import urlretrieve as compat_urlretrieve
64 except ImportError:  # Python 2
65     from urllib import urlretrieve as compat_urlretrieve
66
67
68 try:
69     from subprocess import DEVNULL
70     compat_subprocess_get_DEVNULL = lambda: DEVNULL
71 except ImportError:
72     compat_subprocess_get_DEVNULL = lambda: open(os.path.devnull, 'w')
73
74 try:
75     import http.server as compat_http_server
76 except ImportError:
77     import BaseHTTPServer as compat_http_server
78
79 try:
80     from urllib.parse import unquote as compat_urllib_parse_unquote
81 except ImportError:
82     def compat_urllib_parse_unquote(string, encoding='utf-8', errors='replace'):
83         if string == '':
84             return string
85         res = string.split('%')
86         if len(res) == 1:
87             return string
88         if encoding is None:
89             encoding = 'utf-8'
90         if errors is None:
91             errors = 'replace'
92         # pct_sequence: contiguous sequence of percent-encoded bytes, decoded
93         pct_sequence = b''
94         string = res[0]
95         for item in res[1:]:
96             try:
97                 if not item:
98                     raise ValueError
99                 pct_sequence += item[:2].decode('hex')
100                 rest = item[2:]
101                 if not rest:
102                     # This segment was just a single percent-encoded character.
103                     # May be part of a sequence of code units, so delay decoding.
104                     # (Stored in pct_sequence).
105                     continue
106             except ValueError:
107                 rest = '%' + item
108             # Encountered non-percent-encoded characters. Flush the current
109             # pct_sequence.
110             string += pct_sequence.decode(encoding, errors) + rest
111             pct_sequence = b''
112         if pct_sequence:
113             # Flush the final pct_sequence
114             string += pct_sequence.decode(encoding, errors)
115         return string
116
117
118 try:
119     from urllib.parse import parse_qs as compat_parse_qs
120 except ImportError:  # Python 2
121     # HACK: The following is the correct parse_qs implementation from cpython 3's stdlib.
122     # Python 2's version is apparently totally broken
123
124     def _parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
125                    encoding='utf-8', errors='replace'):
126         qs, _coerce_result = qs, unicode
127         pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
128         r = []
129         for name_value in pairs:
130             if not name_value and not strict_parsing:
131                 continue
132             nv = name_value.split('=', 1)
133             if len(nv) != 2:
134                 if strict_parsing:
135                     raise ValueError("bad query field: %r" % (name_value,))
136                 # Handle case of a control-name with no equal sign
137                 if keep_blank_values:
138                     nv.append('')
139                 else:
140                     continue
141             if len(nv[1]) or keep_blank_values:
142                 name = nv[0].replace('+', ' ')
143                 name = compat_urllib_parse_unquote(
144                     name, encoding=encoding, errors=errors)
145                 name = _coerce_result(name)
146                 value = nv[1].replace('+', ' ')
147                 value = compat_urllib_parse_unquote(
148                     value, encoding=encoding, errors=errors)
149                 value = _coerce_result(value)
150                 r.append((name, value))
151         return r
152
153     def compat_parse_qs(qs, keep_blank_values=False, strict_parsing=False,
154                         encoding='utf-8', errors='replace'):
155         parsed_result = {}
156         pairs = _parse_qsl(qs, keep_blank_values, strict_parsing,
157                            encoding=encoding, errors=errors)
158         for name, value in pairs:
159             if name in parsed_result:
160                 parsed_result[name].append(value)
161             else:
162                 parsed_result[name] = [value]
163         return parsed_result
164
165 try:
166     compat_str = unicode  # Python 2
167 except NameError:
168     compat_str = str
169
170 try:
171     compat_chr = unichr  # Python 2
172 except NameError:
173     compat_chr = chr
174
175 try:
176     from xml.etree.ElementTree import ParseError as compat_xml_parse_error
177 except ImportError:  # Python 2.6
178     from xml.parsers.expat import ExpatError as compat_xml_parse_error
179
180 try:
181     from shlex import quote as shlex_quote
182 except ImportError:  # Python < 3.3
183     def shlex_quote(s):
184         if re.match(r'^[-_\w./]+$', s):
185             return s
186         else:
187             return "'" + s.replace("'", "'\"'\"'") + "'"
188
189
190 def compat_ord(c):
191     if type(c) is int:
192         return c
193     else:
194         return ord(c)
195
196
197 if sys.version_info >= (3, 0):
198     compat_getenv = os.getenv
199     compat_expanduser = os.path.expanduser
200 else:
201     # Environment variables should be decoded with filesystem encoding.
202     # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
203
204     def compat_getenv(key, default=None):
205         from .utils import get_filesystem_encoding
206         env = os.getenv(key, default)
207         if env:
208             env = env.decode(get_filesystem_encoding())
209         return env
210
211     # HACK: The default implementations of os.path.expanduser from cpython do not decode
212     # environment variables with filesystem encoding. We will work around this by
213     # providing adjusted implementations.
214     # The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
215     # for different platforms with correct environment variables decoding.
216
217     if os.name == 'posix':
218         def compat_expanduser(path):
219             """Expand ~ and ~user constructions.  If user or $HOME is unknown,
220             do nothing."""
221             if not path.startswith('~'):
222                 return path
223             i = path.find('/', 1)
224             if i < 0:
225                 i = len(path)
226             if i == 1:
227                 if 'HOME' not in os.environ:
228                     import pwd
229                     userhome = pwd.getpwuid(os.getuid()).pw_dir
230                 else:
231                     userhome = compat_getenv('HOME')
232             else:
233                 import pwd
234                 try:
235                     pwent = pwd.getpwnam(path[1:i])
236                 except KeyError:
237                     return path
238                 userhome = pwent.pw_dir
239             userhome = userhome.rstrip('/')
240             return (userhome + path[i:]) or '/'
241     elif os.name == 'nt' or os.name == 'ce':
242         def compat_expanduser(path):
243             """Expand ~ and ~user constructs.
244
245             If user or $HOME is unknown, do nothing."""
246             if path[:1] != '~':
247                 return path
248             i, n = 1, len(path)
249             while i < n and path[i] not in '/\\':
250                 i = i + 1
251
252             if 'HOME' in os.environ:
253                 userhome = compat_getenv('HOME')
254             elif 'USERPROFILE' in os.environ:
255                 userhome = compat_getenv('USERPROFILE')
256             elif 'HOMEPATH' not in os.environ:
257                 return path
258             else:
259                 try:
260                     drive = compat_getenv('HOMEDRIVE')
261                 except KeyError:
262                     drive = ''
263                 userhome = os.path.join(drive, compat_getenv('HOMEPATH'))
264
265             if i != 1:  # ~user
266                 userhome = os.path.join(os.path.dirname(userhome), path[1:i])
267
268             return userhome + path[i:]
269     else:
270         compat_expanduser = os.path.expanduser
271
272
273 if sys.version_info < (3, 0):
274     def compat_print(s):
275         from .utils import preferredencoding
276         print(s.encode(preferredencoding(), 'xmlcharrefreplace'))
277 else:
278     def compat_print(s):
279         assert isinstance(s, compat_str)
280         print(s)
281
282
283 try:
284     subprocess_check_output = subprocess.check_output
285 except AttributeError:
286     def subprocess_check_output(*args, **kwargs):
287         assert 'input' not in kwargs
288         p = subprocess.Popen(*args, stdout=subprocess.PIPE, **kwargs)
289         output, _ = p.communicate()
290         ret = p.poll()
291         if ret:
292             raise subprocess.CalledProcessError(ret, p.args, output=output)
293         return output
294
295 if sys.version_info < (3, 0) and sys.platform == 'win32':
296     def compat_getpass(prompt, *args, **kwargs):
297         if isinstance(prompt, compat_str):
298             from .utils import preferredencoding
299             prompt = prompt.encode(preferredencoding())
300         return getpass.getpass(prompt, *args, **kwargs)
301 else:
302     compat_getpass = getpass.getpass
303
304 # Old 2.6 and 2.7 releases require kwargs to be bytes
305 try:
306     def _testfunc(x):
307         pass
308     _testfunc(**{'x': 0})
309 except TypeError:
310     def compat_kwargs(kwargs):
311         return dict((bytes(k), v) for k, v in kwargs.items())
312 else:
313     compat_kwargs = lambda kwargs: kwargs
314
315
316 if sys.version_info < (2, 7):
317     def compat_socket_create_connection(address, timeout, source_address=None):
318         host, port = address
319         err = None
320         for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
321             af, socktype, proto, canonname, sa = res
322             sock = None
323             try:
324                 sock = socket.socket(af, socktype, proto)
325                 sock.settimeout(timeout)
326                 if source_address:
327                     sock.bind(source_address)
328                 sock.connect(sa)
329                 return sock
330             except socket.error as _:
331                 err = _
332                 if sock is not None:
333                     sock.close()
334         if err is not None:
335             raise err
336         else:
337             raise socket.error("getaddrinfo returns an empty list")
338 else:
339     compat_socket_create_connection = socket.create_connection
340
341
342 # Fix https://github.com/rg3/youtube-dl/issues/4223
343 # See http://bugs.python.org/issue9161 for what is broken
344 def workaround_optparse_bug9161():
345     op = optparse.OptionParser()
346     og = optparse.OptionGroup(op, 'foo')
347     try:
348         og.add_option('-t')
349     except TypeError:
350         real_add_option = optparse.OptionGroup.add_option
351
352         def _compat_add_option(self, *args, **kwargs):
353             enc = lambda v: (
354                 v.encode('ascii', 'replace') if isinstance(v, compat_str)
355                 else v)
356             bargs = [enc(a) for a in args]
357             bkwargs = dict(
358                 (k, enc(v)) for k, v in kwargs.items())
359             return real_add_option(self, *bargs, **bkwargs)
360         optparse.OptionGroup.add_option = _compat_add_option
361
362
363 __all__ = [
364     'compat_HTTPError',
365     'compat_chr',
366     'compat_cookiejar',
367     'compat_expanduser',
368     'compat_getenv',
369     'compat_getpass',
370     'compat_html_entities',
371     'compat_html_parser',
372     'compat_http_client',
373     'compat_http_server',
374     'compat_kwargs',
375     'compat_ord',
376     'compat_parse_qs',
377     'compat_print',
378     'compat_socket_create_connection',
379     'compat_str',
380     'compat_subprocess_get_DEVNULL',
381     'compat_urllib_error',
382     'compat_urllib_parse',
383     'compat_urllib_parse_unquote',
384     'compat_urllib_parse_urlparse',
385     'compat_urllib_request',
386     'compat_urlparse',
387     'compat_urlretrieve',
388     'compat_xml_parse_error',
389     'shlex_quote',
390     'subprocess_check_output',
391     'workaround_optparse_bug9161',
392 ]