You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

  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_expanduser
  10. from .utils import write_json_file
  11. class Cache(object):
  12. def __init__(self, ydl):
  13. self._ydl = ydl
  14. def _get_root_dir(self):
  15. res = self._ydl.params.get('cachedir')
  16. if res is None:
  17. cache_root = os.environ.get('XDG_CACHE_HOME', '~/.cache')
  18. res = os.path.join(cache_root, 'youtube-dl')
  19. return compat_expanduser(res)
  20. def _get_cache_fn(self, section, key, dtype):
  21. assert re.match(r'^[a-zA-Z0-9_.-]+$', section), \
  22. 'invalid section %r' % section
  23. assert re.match(r'^[a-zA-Z0-9_.-]+$', key), 'invalid key %r' % key
  24. return os.path.join(
  25. self._get_root_dir(), section, '%s.%s' % (key, dtype))
  26. @property
  27. def enabled(self):
  28. return self._ydl.params.get('cachedir') is not False
  29. def store(self, section, key, data, dtype='json'):
  30. assert dtype in ('json',)
  31. if not self.enabled:
  32. return
  33. fn = self._get_cache_fn(section, key, dtype)
  34. try:
  35. try:
  36. os.makedirs(os.path.dirname(fn))
  37. except OSError as ose:
  38. if ose.errno != errno.EEXIST:
  39. raise
  40. write_json_file(data, fn)
  41. except Exception:
  42. tb = traceback.format_exc()
  43. self._ydl.report_warning(
  44. 'Writing cache to %r failed: %s' % (fn, tb))
  45. def load(self, section, key, dtype='json', default=None):
  46. assert dtype in ('json',)
  47. if not self.enabled:
  48. return default
  49. cache_fn = self._get_cache_fn(section, key, dtype)
  50. try:
  51. try:
  52. with io.open(cache_fn, 'r', encoding='utf-8') as cachef:
  53. return json.load(cachef)
  54. except ValueError:
  55. try:
  56. file_size = os.path.getsize(cache_fn)
  57. except (OSError, IOError) as oe:
  58. file_size = str(oe)
  59. self._ydl.report_warning(
  60. 'Cache retrieval from %s failed (%s)' % (cache_fn, file_size))
  61. except IOError:
  62. pass # No cache available
  63. return default
  64. def remove(self):
  65. if not self.enabled:
  66. self._ydl.to_screen('Cache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)')
  67. return
  68. cachedir = self._get_root_dir()
  69. if not any((term in cachedir) for term in ('cache', 'tmp')):
  70. raise Exception('Not removing directory %s - this does not look like a cache dir' % cachedir)
  71. self._ydl.to_screen(
  72. 'Removing cache dir %s .' % cachedir, skip_eol=True)
  73. if os.path.exists(cachedir):
  74. self._ydl.to_screen('.', skip_eol=True)
  75. shutil.rmtree(cachedir)
  76. self._ydl.to_screen('.')