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.

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