cache.py 3.7 KB

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