cache.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from __future__ import unicode_literals
  2. import errno
  3. import json
  4. import os
  5. import re
  6. import shutil
  7. import traceback
  8. from .compat import (
  9. compat_getenv,
  10. compat_open as open,
  11. )
  12. from .utils import (
  13. error_to_compat_str,
  14. expand_path,
  15. is_outdated_version,
  16. try_get,
  17. write_json_file,
  18. )
  19. from .version import __version__
  20. class Cache(object):
  21. _YTDL_DIR = 'youtube-dl'
  22. _VERSION_KEY = _YTDL_DIR + '_version'
  23. _DEFAULT_VERSION = '2021.12.17'
  24. def __init__(self, ydl):
  25. self._ydl = ydl
  26. def _get_root_dir(self):
  27. res = self._ydl.params.get('cachedir')
  28. if res is None:
  29. cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
  30. res = os.path.join(cache_root, self._YTDL_DIR)
  31. return expand_path(res)
  32. def _get_cache_fn(self, section, key, dtype):
  33. assert re.match(r'^[a-zA-Z0-9_.-]+$', section), \
  34. 'invalid section %r' % section
  35. assert re.match(r'^[a-zA-Z0-9_.-]+$', key), 'invalid key %r' % key
  36. return os.path.join(
  37. self._get_root_dir(), section, '%s.%s' % (key, dtype))
  38. @property
  39. def enabled(self):
  40. return self._ydl.params.get('cachedir') is not False
  41. def store(self, section, key, data, dtype='json'):
  42. assert dtype in ('json',)
  43. if not self.enabled:
  44. return
  45. fn = self._get_cache_fn(section, key, dtype)
  46. try:
  47. try:
  48. os.makedirs(os.path.dirname(fn))
  49. except OSError as ose:
  50. if ose.errno != errno.EEXIST:
  51. raise
  52. write_json_file({self._VERSION_KEY: __version__, 'data': data}, fn)
  53. except Exception:
  54. tb = traceback.format_exc()
  55. self._ydl.report_warning(
  56. 'Writing cache to %r failed: %s' % (fn, tb))
  57. def _validate(self, data, min_ver):
  58. version = try_get(data, lambda x: x[self._VERSION_KEY])
  59. if not version: # Backward compatibility
  60. data, version = {'data': data}, self._DEFAULT_VERSION
  61. if not is_outdated_version(version, min_ver or '0', assume_new=False):
  62. return data['data']
  63. self._ydl.to_screen(
  64. 'Discarding old cache from version {version} (needs {min_ver})'.format(**locals()))
  65. def load(self, section, key, dtype='json', default=None, min_ver=None):
  66. assert dtype in ('json',)
  67. if not self.enabled:
  68. return default
  69. cache_fn = self._get_cache_fn(section, key, dtype)
  70. try:
  71. try:
  72. with open(cache_fn, 'r', encoding='utf-8') as cachef:
  73. return self._validate(json.load(cachef), min_ver)
  74. except ValueError:
  75. try:
  76. file_size = os.path.getsize(cache_fn)
  77. except (OSError, IOError) as oe:
  78. file_size = error_to_compat_str(oe)
  79. self._ydl.report_warning(
  80. 'Cache retrieval from %s failed (%s)' % (cache_fn, file_size))
  81. except IOError:
  82. pass # No cache available
  83. return default
  84. def remove(self):
  85. if not self.enabled:
  86. self._ydl.to_screen('Cache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)')
  87. return
  88. cachedir = self._get_root_dir()
  89. if not any((term in cachedir) for term in ('cache', 'tmp')):
  90. raise Exception('Not removing directory %s - this does not look like a cache dir' % cachedir)
  91. self._ydl.to_screen(
  92. 'Removing cache dir %s .' % cachedir, skip_eol=True)
  93. if os.path.exists(cachedir):
  94. self._ydl.to_screen('.', skip_eol=True)
  95. shutil.rmtree(cachedir)
  96. self._ydl.to_screen('.')