logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

make_lazy_extractors.py (3931B)


  1. from __future__ import unicode_literals, print_function
  2. from inspect import getsource
  3. import os
  4. from os.path import dirname as dirn
  5. import re
  6. import sys
  7. print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
  8. sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))
  9. lazy_extractors_filename = sys.argv[1]
  10. if os.path.exists(lazy_extractors_filename):
  11. os.remove(lazy_extractors_filename)
  12. # Py2: may be confused by leftover lazy_extractors.pyc
  13. if sys.version_info[0] < 3:
  14. for c in ('c', 'o'):
  15. try:
  16. os.remove(lazy_extractors_filename + 'c')
  17. except OSError:
  18. pass
  19. from devscripts.utils import read_file, write_file
  20. from youtube_dl.compat import compat_register_utf8
  21. compat_register_utf8()
  22. from youtube_dl.extractor import _ALL_CLASSES
  23. from youtube_dl.extractor.common import InfoExtractor, SearchInfoExtractor
  24. module_template = read_file('devscripts/lazy_load_template.py')
  25. def get_source(m):
  26. return re.sub(r'(?m)^\s*#.*\n', '', getsource(m))
  27. module_contents = [
  28. module_template,
  29. get_source(InfoExtractor.suitable),
  30. get_source(InfoExtractor._match_valid_url) + '\n',
  31. 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
  32. # needed for suitable() methods of Youtube extractor (see #28780)
  33. 'from youtube_dl.utils import parse_qs, variadic\n',
  34. ]
  35. ie_template = '''
  36. class {name}({bases}):
  37. _VALID_URL = {valid_url!r}
  38. _module = '{module}'
  39. '''
  40. make_valid_template = '''
  41. @classmethod
  42. def _make_valid_url(cls):
  43. return {valid_url!r}
  44. '''
  45. def get_base_name(base):
  46. if base is InfoExtractor:
  47. return 'LazyLoadExtractor'
  48. elif base is SearchInfoExtractor:
  49. return 'LazyLoadSearchExtractor'
  50. else:
  51. return base.__name__
  52. def build_lazy_ie(ie, name):
  53. valid_url = getattr(ie, '_VALID_URL', None)
  54. s = ie_template.format(
  55. name=name,
  56. bases=', '.join(map(get_base_name, ie.__bases__)),
  57. valid_url=valid_url,
  58. module=ie.__module__)
  59. if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
  60. s += '\n' + get_source(ie.suitable)
  61. if hasattr(ie, '_make_valid_url'):
  62. # search extractors
  63. s += make_valid_template.format(valid_url=ie._make_valid_url())
  64. return s
  65. # find the correct sorting and add the required base classes so that subclasses
  66. # can be correctly created
  67. classes = _ALL_CLASSES[:-1]
  68. ordered_cls = []
  69. while classes:
  70. for c in classes[:]:
  71. bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
  72. stop = False
  73. for b in bases:
  74. if b not in classes and b not in ordered_cls:
  75. if b.__name__ == 'GenericIE':
  76. exit()
  77. classes.insert(0, b)
  78. stop = True
  79. if stop:
  80. break
  81. if all(b in ordered_cls for b in bases):
  82. ordered_cls.append(c)
  83. classes.remove(c)
  84. break
  85. ordered_cls.append(_ALL_CLASSES[-1])
  86. names = []
  87. for ie in ordered_cls:
  88. name = ie.__name__
  89. src = build_lazy_ie(ie, name)
  90. module_contents.append(src)
  91. if ie in _ALL_CLASSES:
  92. names.append(name)
  93. module_contents.append(
  94. '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
  95. module_src = '\n'.join(module_contents)
  96. write_file(lazy_extractors_filename, module_src + '\n')
  97. # work around JVM byte code module limit in Jython
  98. if sys.platform.startswith('java') and sys.version_info[:2] == (2, 7):
  99. import subprocess
  100. from youtube_dl.compat import compat_subprocess_get_DEVNULL
  101. # if Python 2.7 is available, use it to compile the module for Jython
  102. try:
  103. # if Python 2.7 is available, use it to compile the module for Jython
  104. subprocess.check_call(['python2.7', '-m', 'py_compile', lazy_extractors_filename], stdout=compat_subprocess_get_DEVNULL())
  105. except Exception:
  106. pass