2016-07-07 05:37:29 -04:00
|
|
|
# coding: utf-8
|
2014-11-02 05:36:29 -05:00
|
|
|
|
2021-07-19 16:51:55 -04:00
|
|
|
import asyncio
|
2018-01-23 09:53:01 -05:00
|
|
|
import base64
|
2014-12-11 22:01:08 -05:00
|
|
|
import ctypes
|
2014-11-02 05:23:40 -05:00
|
|
|
import getpass
|
2021-07-19 16:51:55 -04:00
|
|
|
import html
|
|
|
|
import html.parser
|
|
|
|
import http
|
|
|
|
import http.client
|
|
|
|
import http.cookiejar
|
|
|
|
import http.cookies
|
|
|
|
import http.server
|
2017-09-05 22:24:34 -04:00
|
|
|
import itertools
|
2014-11-19 12:21:58 -05:00
|
|
|
import optparse
|
2014-11-02 05:23:40 -05:00
|
|
|
import os
|
2014-11-23 04:49:19 -05:00
|
|
|
import re
|
2015-09-05 11:40:44 -04:00
|
|
|
import shlex
|
2015-02-28 15:42:16 -05:00
|
|
|
import shutil
|
2015-01-10 13:55:36 -05:00
|
|
|
import socket
|
2016-04-23 06:28:49 -04:00
|
|
|
import struct
|
2014-11-02 05:23:40 -05:00
|
|
|
import sys
|
2021-07-19 16:51:55 -04:00
|
|
|
import tokenize
|
|
|
|
import urllib
|
|
|
|
import xml.etree.ElementTree as etree
|
|
|
|
from subprocess import DEVNULL
|
2017-06-11 14:50:32 -04:00
|
|
|
|
|
|
|
|
2021-07-19 16:51:55 -04:00
|
|
|
# HTMLParseError has been deprecated in Python 3.3 and removed in
|
|
|
|
# Python 3.5. Introducing dummy exception for Python >3.5 for compatible
|
|
|
|
# and uniform cross-version exception handling
|
|
|
|
class compat_HTMLParseError(Exception):
|
|
|
|
pass
|
2016-03-25 15:46:57 -04:00
|
|
|
|
|
|
|
|
2021-09-17 14:23:55 -04:00
|
|
|
# compat_ctypes_WINFUNCTYPE = ctypes.WINFUNCTYPE
|
|
|
|
# will not work since ctypes.WINFUNCTYPE does not exist in UNIX machines
|
2021-07-19 16:51:55 -04:00
|
|
|
def compat_ctypes_WINFUNCTYPE(*args, **kwargs):
|
|
|
|
return ctypes.WINFUNCTYPE(*args, **kwargs)
|
2016-05-22 13:34:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
class _TreeBuilder(etree.TreeBuilder):
|
|
|
|
def doctype(self, name, pubid, system):
|
|
|
|
pass
|
|
|
|
|
2016-11-17 06:42:56 -05:00
|
|
|
|
2021-07-19 16:51:55 -04:00
|
|
|
def compat_etree_fromstring(text):
|
|
|
|
return etree.XML(text, parser=etree.XMLParser(target=_TreeBuilder()))
|
2014-11-02 05:23:40 -05:00
|
|
|
|
2017-06-17 12:14:24 -04:00
|
|
|
|
|
|
|
compat_os_name = os._name if os.name == 'java' else os.name
|
|
|
|
|
|
|
|
|
|
|
|
if compat_os_name == 'nt':
|
2016-05-10 03:58:25 -04:00
|
|
|
def compat_shlex_quote(s):
|
2017-06-17 12:14:24 -04:00
|
|
|
return s if re.match(r'^[-_\w./]+$', s) else '"%s"' % s.replace('"', '\\"')
|
|
|
|
else:
|
2021-07-19 16:51:55 -04:00
|
|
|
from shlex import quote as compat_shlex_quote
|
2015-09-05 11:40:44 -04:00
|
|
|
|
|
|
|
|
2014-11-02 05:23:40 -05:00
|
|
|
def compat_ord(c):
|
2014-11-23 14:41:03 -05:00
|
|
|
if type(c) is int:
|
|
|
|
return c
|
|
|
|
else:
|
|
|
|
return ord(c)
|
2014-11-02 05:23:40 -05:00
|
|
|
|
|
|
|
|
2021-07-19 16:51:55 -04:00
|
|
|
def compat_setenv(key, value, env=os.environ):
|
|
|
|
env[key] = value
|
2014-11-02 05:23:40 -05:00
|
|
|
|
|
|
|
|
2020-02-08 07:36:55 -05:00
|
|
|
if compat_os_name == 'nt' and sys.version_info < (3, 8):
|
|
|
|
# os.path.realpath on Windows does not follow symbolic links
|
|
|
|
# prior to Python 3.8 (see https://bugs.python.org/issue9949)
|
|
|
|
def compat_realpath(path):
|
|
|
|
while os.path.islink(path):
|
|
|
|
path = os.path.abspath(os.readlink(path))
|
|
|
|
return path
|
|
|
|
else:
|
|
|
|
compat_realpath = os.path.realpath
|
|
|
|
|
|
|
|
|
2021-07-19 16:51:55 -04:00
|
|
|
def compat_print(s):
|
|
|
|
assert isinstance(s, compat_str)
|
|
|
|
print(s)
|
2015-01-10 13:55:36 -05:00
|
|
|
|
|
|
|
|
2019-03-09 07:14:41 -05:00
|
|
|
# Fix https://github.com/ytdl-org/youtube-dl/issues/4223
|
2014-11-19 12:21:58 -05:00
|
|
|
# See http://bugs.python.org/issue9161 for what is broken
|
|
|
|
def workaround_optparse_bug9161():
|
2014-11-20 01:21:12 -05:00
|
|
|
op = optparse.OptionParser()
|
|
|
|
og = optparse.OptionGroup(op, 'foo')
|
2014-11-19 12:21:58 -05:00
|
|
|
try:
|
2014-11-20 01:21:12 -05:00
|
|
|
og.add_option('-t')
|
2014-11-20 10:35:55 -05:00
|
|
|
except TypeError:
|
2014-11-19 12:21:58 -05:00
|
|
|
real_add_option = optparse.OptionGroup.add_option
|
|
|
|
|
|
|
|
def _compat_add_option(self, *args, **kwargs):
|
|
|
|
enc = lambda v: (
|
|
|
|
v.encode('ascii', 'replace') if isinstance(v, compat_str)
|
|
|
|
else v)
|
|
|
|
bargs = [enc(a) for a in args]
|
|
|
|
bkwargs = dict(
|
|
|
|
(k, enc(v)) for k, v in kwargs.items())
|
|
|
|
return real_add_option(self, *bargs, **bkwargs)
|
|
|
|
optparse.OptionGroup.add_option = _compat_add_option
|
|
|
|
|
2016-11-17 06:42:56 -05:00
|
|
|
|
2021-04-28 06:47:30 -04:00
|
|
|
try:
|
|
|
|
compat_Pattern = re.Pattern
|
|
|
|
except AttributeError:
|
|
|
|
compat_Pattern = type(re.compile(''))
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
compat_Match = re.Match
|
|
|
|
except AttributeError:
|
|
|
|
compat_Match = type(re.compile('').match(''))
|
|
|
|
|
|
|
|
|
2021-06-21 13:23:17 -04:00
|
|
|
try:
|
2021-07-19 16:51:55 -04:00
|
|
|
compat_asyncio_run = asyncio.run # >= 3.7
|
2021-06-21 13:23:17 -04:00
|
|
|
except AttributeError:
|
|
|
|
def compat_asyncio_run(coro):
|
|
|
|
try:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
loop.run_until_complete(coro)
|
|
|
|
|
|
|
|
asyncio.run = compat_asyncio_run
|
|
|
|
|
|
|
|
|
2021-09-15 15:04:18 -04:00
|
|
|
# Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl
|
|
|
|
# See https://github.com/yt-dlp/yt-dlp/issues/792
|
|
|
|
# https://docs.python.org/3/library/os.path.html#os.path.expanduser
|
|
|
|
if compat_os_name in ('nt', 'ce') and 'HOME' in os.environ:
|
|
|
|
_userhome = os.environ['HOME']
|
|
|
|
|
|
|
|
def compat_expanduser(path):
|
|
|
|
if not path.startswith('~'):
|
|
|
|
return path
|
|
|
|
i = path.replace('\\', '/', 1).find('/') # ~user
|
|
|
|
if i < 0:
|
|
|
|
i = len(path)
|
|
|
|
userhome = os.path.join(os.path.dirname(_userhome), path[1:i]) if i > 1 else _userhome
|
|
|
|
return userhome + path[i:]
|
|
|
|
else:
|
|
|
|
compat_expanduser = os.path.expanduser
|
|
|
|
|
|
|
|
|
2021-09-17 15:21:27 -04:00
|
|
|
try:
|
|
|
|
from Cryptodome.Cipher import AES as compat_pycrypto_AES
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
from Crypto.Cipher import AES as compat_pycrypto_AES
|
|
|
|
except ImportError:
|
|
|
|
compat_pycrypto_AES = None
|
|
|
|
|
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
def windows_enable_vt_mode(): # TODO: Do this the proper way https://bugs.python.org/issue30075
|
|
|
|
if compat_os_name != 'nt':
|
|
|
|
return
|
|
|
|
os.system('')
|
|
|
|
|
|
|
|
|
2021-07-19 16:51:55 -04:00
|
|
|
# Deprecated
|
|
|
|
|
|
|
|
compat_basestring = str
|
|
|
|
compat_chr = chr
|
|
|
|
compat_input = input
|
|
|
|
compat_integer_types = (int, )
|
|
|
|
compat_kwargs = lambda kwargs: kwargs
|
|
|
|
compat_numeric_types = (int, float, complex)
|
|
|
|
compat_str = str
|
|
|
|
compat_xpath = lambda xpath: xpath
|
|
|
|
compat_zip = zip
|
|
|
|
|
|
|
|
compat_HTMLParser = html.parser.HTMLParser
|
|
|
|
compat_HTTPError = urllib.error.HTTPError
|
|
|
|
compat_Struct = struct.Struct
|
|
|
|
compat_b64decode = base64.b64decode
|
|
|
|
compat_cookiejar = http.cookiejar
|
|
|
|
compat_cookiejar_Cookie = compat_cookiejar.Cookie
|
|
|
|
compat_cookies = http.cookies
|
|
|
|
compat_cookies_SimpleCookie = compat_cookies.SimpleCookie
|
|
|
|
compat_etree_Element = etree.Element
|
|
|
|
compat_etree_register_namespace = etree.register_namespace
|
|
|
|
compat_get_terminal_size = shutil.get_terminal_size
|
|
|
|
compat_getenv = os.getenv
|
|
|
|
compat_getpass = getpass.getpass
|
|
|
|
compat_html_entities = html.entities
|
|
|
|
compat_html_entities_html5 = compat_html_entities.html5
|
|
|
|
compat_http_client = http.client
|
|
|
|
compat_http_server = http.server
|
|
|
|
compat_itertools_count = itertools.count
|
|
|
|
compat_parse_qs = urllib.parse.parse_qs
|
|
|
|
compat_shlex_split = shlex.split
|
|
|
|
compat_socket_create_connection = socket.create_connection
|
|
|
|
compat_struct_pack = struct.pack
|
|
|
|
compat_struct_unpack = struct.unpack
|
|
|
|
compat_subprocess_get_DEVNULL = lambda: DEVNULL
|
|
|
|
compat_tokenize_tokenize = tokenize.tokenize
|
|
|
|
compat_urllib_error = urllib.error
|
|
|
|
compat_urllib_parse = urllib.parse
|
|
|
|
compat_urllib_parse_quote = urllib.parse.quote
|
|
|
|
compat_urllib_parse_quote_plus = urllib.parse.quote_plus
|
|
|
|
compat_urllib_parse_unquote = urllib.parse.unquote
|
|
|
|
compat_urllib_parse_unquote_plus = urllib.parse.unquote_plus
|
|
|
|
compat_urllib_parse_unquote_to_bytes = urllib.parse.unquote_to_bytes
|
|
|
|
compat_urllib_parse_urlencode = urllib.parse.urlencode
|
|
|
|
compat_urllib_parse_urlparse = urllib.parse.urlparse
|
|
|
|
compat_urllib_parse_urlunparse = urllib.parse.urlunparse
|
|
|
|
compat_urllib_request = urllib.request
|
|
|
|
compat_urllib_request_DataHandler = urllib.request.DataHandler
|
|
|
|
compat_urllib_response = urllib.response
|
|
|
|
compat_urlparse = urllib.parse
|
|
|
|
compat_urlretrieve = urllib.request.urlretrieve
|
|
|
|
compat_xml_parse_error = etree.ParseError
|
|
|
|
|
|
|
|
|
|
|
|
# Set public objects
|
|
|
|
|
2014-11-02 05:23:40 -05:00
|
|
|
__all__ = [
|
2017-06-11 15:35:57 -04:00
|
|
|
'compat_HTMLParseError',
|
2016-01-02 14:49:59 -05:00
|
|
|
'compat_HTMLParser',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_HTTPError',
|
2021-04-28 06:47:30 -04:00
|
|
|
'compat_Match',
|
|
|
|
'compat_Pattern',
|
2018-01-27 15:04:39 -05:00
|
|
|
'compat_Struct',
|
2021-06-21 13:23:17 -04:00
|
|
|
'compat_asyncio_run',
|
2018-01-23 09:53:01 -05:00
|
|
|
'compat_b64decode',
|
2015-02-01 05:36:59 -05:00
|
|
|
'compat_basestring',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_chr',
|
|
|
|
'compat_cookiejar',
|
2020-05-04 18:54:10 -04:00
|
|
|
'compat_cookiejar_Cookie',
|
2015-07-29 18:20:37 -04:00
|
|
|
'compat_cookies',
|
2021-04-10 12:47:11 -04:00
|
|
|
'compat_cookies_SimpleCookie',
|
2014-12-11 22:01:08 -05:00
|
|
|
'compat_ctypes_WINFUNCTYPE',
|
2019-03-05 13:18:52 -05:00
|
|
|
'compat_etree_Element',
|
2015-10-25 15:04:55 -04:00
|
|
|
'compat_etree_fromstring',
|
2017-02-01 14:15:25 -05:00
|
|
|
'compat_etree_register_namespace',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_expanduser',
|
2015-02-28 15:42:16 -05:00
|
|
|
'compat_get_terminal_size',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_getenv',
|
|
|
|
'compat_getpass',
|
|
|
|
'compat_html_entities',
|
2016-06-10 03:05:24 -04:00
|
|
|
'compat_html_entities_html5',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_http_client',
|
2015-01-29 20:57:37 -05:00
|
|
|
'compat_http_server',
|
2016-06-05 17:16:08 -04:00
|
|
|
'compat_input',
|
2018-06-17 16:22:08 -04:00
|
|
|
'compat_integer_types',
|
2015-07-10 06:58:12 -04:00
|
|
|
'compat_itertools_count',
|
2014-11-15 09:17:19 -05:00
|
|
|
'compat_kwargs',
|
2016-03-05 12:07:25 -05:00
|
|
|
'compat_numeric_types',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_ord',
|
2016-03-03 06:24:24 -05:00
|
|
|
'compat_os_name',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_parse_qs',
|
|
|
|
'compat_print',
|
2021-09-17 15:21:27 -04:00
|
|
|
'compat_pycrypto_AES',
|
2020-02-08 07:38:36 -05:00
|
|
|
'compat_realpath',
|
2016-05-09 11:55:03 -04:00
|
|
|
'compat_setenv',
|
2016-05-10 03:58:25 -04:00
|
|
|
'compat_shlex_quote',
|
2015-09-05 11:40:44 -04:00
|
|
|
'compat_shlex_split',
|
2015-01-10 13:55:36 -05:00
|
|
|
'compat_socket_create_connection',
|
2015-01-11 04:13:03 -05:00
|
|
|
'compat_str',
|
2016-05-03 04:50:16 -04:00
|
|
|
'compat_struct_pack',
|
|
|
|
'compat_struct_unpack',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_subprocess_get_DEVNULL',
|
2015-06-28 16:08:29 -04:00
|
|
|
'compat_tokenize_tokenize',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_urllib_error',
|
|
|
|
'compat_urllib_parse',
|
2020-10-27 06:37:21 -04:00
|
|
|
'compat_urllib_parse_quote',
|
|
|
|
'compat_urllib_parse_quote_plus',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_urllib_parse_unquote',
|
2015-07-17 13:28:34 -04:00
|
|
|
'compat_urllib_parse_unquote_plus',
|
2015-07-17 08:24:07 -04:00
|
|
|
'compat_urllib_parse_unquote_to_bytes',
|
2016-03-25 15:46:57 -04:00
|
|
|
'compat_urllib_parse_urlencode',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_urllib_parse_urlparse',
|
2020-10-27 06:37:21 -04:00
|
|
|
'compat_urllib_parse_urlunparse',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_urllib_request',
|
2015-10-17 11:15:01 -04:00
|
|
|
'compat_urllib_request_DataHandler',
|
|
|
|
'compat_urllib_response',
|
2014-11-02 05:23:40 -05:00
|
|
|
'compat_urlparse',
|
|
|
|
'compat_urlretrieve',
|
|
|
|
'compat_xml_parse_error',
|
2016-03-17 16:51:38 -04:00
|
|
|
'compat_xpath',
|
2017-09-05 22:24:34 -04:00
|
|
|
'compat_zip',
|
2021-10-08 15:11:59 -04:00
|
|
|
'windows_enable_vt_mode',
|
2014-11-19 12:21:58 -05:00
|
|
|
'workaround_optparse_bug9161',
|
2014-11-02 05:23:40 -05:00
|
|
|
]
|