]>
Commit | Line | Data |
---|---|---|
19a03940 | 1 | import contextlib |
a0e07d31 | 2 | import errno |
a0e07d31 PH |
3 | import json |
4 | import os | |
5 | import re | |
6 | import shutil | |
7 | import traceback | |
8 | ||
5e01315a | 9 | from .utils import expand_path, traverse_obj, version_tuple, write_json_file |
10 | from .version import __version__ | |
a0e07d31 PH |
11 | |
12 | ||
86e5f3ed | 13 | class Cache: |
a0e07d31 PH |
14 | def __init__(self, ydl): |
15 | self._ydl = ydl | |
16 | ||
17 | def _get_root_dir(self): | |
18 | res = self._ydl.params.get('cachedir') | |
19 | if res is None: | |
ac668111 | 20 | cache_root = os.getenv('XDG_CACHE_HOME', '~/.cache') |
7a5c1cfe | 21 | res = os.path.join(cache_root, 'yt-dlp') |
590bc6f6 | 22 | return expand_path(res) |
a0e07d31 PH |
23 | |
24 | def _get_cache_fn(self, section, key, dtype): | |
674c869a PH |
25 | assert re.match(r'^[a-zA-Z0-9_.-]+$', section), \ |
26 | 'invalid section %r' % section | |
27 | assert re.match(r'^[a-zA-Z0-9_.-]+$', key), 'invalid key %r' % key | |
a0e07d31 | 28 | return os.path.join( |
86e5f3ed | 29 | self._get_root_dir(), section, f'{key}.{dtype}') |
a0e07d31 PH |
30 | |
31 | @property | |
32 | def enabled(self): | |
33 | return self._ydl.params.get('cachedir') is not False | |
34 | ||
35 | def store(self, section, key, data, dtype='json'): | |
36 | assert dtype in ('json',) | |
37 | ||
38 | if not self.enabled: | |
39 | return | |
40 | ||
41 | fn = self._get_cache_fn(section, key, dtype) | |
42 | try: | |
43 | try: | |
44 | os.makedirs(os.path.dirname(fn)) | |
45 | except OSError as ose: | |
46 | if ose.errno != errno.EEXIST: | |
47 | raise | |
e6f21b3d | 48 | self._ydl.write_debug(f'Saving {section}.{key} to cache') |
5e01315a | 49 | write_json_file({'yt-dlp_version': __version__, 'data': data}, fn) |
a0e07d31 PH |
50 | except Exception: |
51 | tb = traceback.format_exc() | |
86e5f3ed | 52 | self._ydl.report_warning(f'Writing cache to {fn!r} failed: {tb}') |
a0e07d31 | 53 | |
d81ba7d4 | 54 | def _validate(self, data, min_ver): |
5e01315a | 55 | version = traverse_obj(data, 'yt-dlp_version') |
56 | if not version: # Backward compatibility | |
57 | data, version = {'data': data}, '2022.08.19' | |
d81ba7d4 | 58 | if not min_ver or version_tuple(version) >= version_tuple(min_ver): |
5e01315a | 59 | return data['data'] |
d81ba7d4 | 60 | self._ydl.write_debug(f'Discarding old cache from version {version} (needs {min_ver})') |
5e01315a | 61 | |
d81ba7d4 | 62 | def load(self, section, key, dtype='json', default=None, *, min_ver=None): |
a0e07d31 PH |
63 | assert dtype in ('json',) |
64 | ||
65 | if not self.enabled: | |
66 | return default | |
67 | ||
68 | cache_fn = self._get_cache_fn(section, key, dtype) | |
19a03940 | 69 | with contextlib.suppress(OSError): |
a0e07d31 | 70 | try: |
86e5f3ed | 71 | with open(cache_fn, encoding='utf-8') as cachef: |
e6f21b3d | 72 | self._ydl.write_debug(f'Loading {section}.{key} from cache') |
d81ba7d4 | 73 | return self._validate(json.load(cachef), min_ver) |
5e01315a | 74 | except (ValueError, KeyError): |
a0e07d31 PH |
75 | try: |
76 | file_size = os.path.getsize(cache_fn) | |
86e5f3ed | 77 | except OSError as oe: |
a0e07d31 | 78 | file_size = str(oe) |
86e5f3ed | 79 | self._ydl.report_warning(f'Cache retrieval from {cache_fn} failed ({file_size})') |
a0e07d31 PH |
80 | |
81 | return default | |
82 | ||
83 | def remove(self): | |
84 | if not self.enabled: | |
85 | self._ydl.to_screen('Cache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)') | |
86 | return | |
87 | ||
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 | ||
92 | self._ydl.to_screen( | |
93 | 'Removing cache dir %s .' % cachedir, skip_eol=True) | |
94 | if os.path.exists(cachedir): | |
95 | self._ydl.to_screen('.', skip_eol=True) | |
96 | shutil.rmtree(cachedir) | |
97 | self._ydl.to_screen('.') |