Replace use of pickle for icon theme cache on Linux

This commit is contained in:
Kovid Goyal 2019-03-15 15:38:12 +05:30
parent 69210a7783
commit fae355fe65
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -6,13 +6,14 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import re, shlex, os, cPickle import re, shlex, os
from collections import defaultdict from collections import defaultdict
from calibre import walk, guess_type, prints, force_unicode from calibre import walk, guess_type, prints, force_unicode
from calibre.constants import filesystem_encoding, cache_dir from calibre.constants import filesystem_encoding, cache_dir
from calibre.utils.icu import numeric_sort_key as sort_key from calibre.utils.icu import numeric_sort_key as sort_key
from calibre.utils.localization import canonicalize_lang, get_lang from calibre.utils.localization import canonicalize_lang, get_lang
from calibre.utils.serialize import msgpack_dumps, msgpack_loads
from polyglot.builtins import string_or_bytes from polyglot.builtins import string_or_bytes
@ -90,7 +91,7 @@ def find_icons():
'/usr/share/pixmaps'] '/usr/share/pixmaps']
ans = defaultdict(list) ans = defaultdict(list)
sz_pat = re.compile(r'/((?:\d+x\d+)|scalable)/') sz_pat = re.compile(r'/((?:\d+x\d+)|scalable)/')
cache_file = os.path.join(cache_dir(), 'icon-theme-cache.pickle') cache_file = os.path.join(cache_dir(), 'icon-theme-cache.calibre_msgpack')
exts = {'.svg', '.png', '.xpm'} exts = {'.svg', '.png', '.xpm'}
def read_icon_theme_dir(dirpath): def read_icon_theme_dir(dirpath):
@ -114,8 +115,9 @@ def find_icons():
try: try:
with open(cache_file, 'rb') as f: with open(cache_file, 'rb') as f:
cache = cPickle.load(f) cache = f.read()
mtimes, cache = cache['mtimes'], cache['data'] cache = msgpack_loads(cache)
mtimes, cache = cache['mtimes'], cache['data']
except Exception: except Exception:
mtimes, cache = defaultdict(int), defaultdict(dict) mtimes, cache = defaultdict(int), defaultdict(dict)
@ -151,9 +153,10 @@ def find_icons():
changed = True changed = True
if changed: if changed:
data = msgpack_dumps({'data':cache, 'mtimes':mtimes})
try: try:
with open(cache_file, 'wb') as f: with open(cache_file, 'wb') as f:
cPickle.dump({'data':cache, 'mtimes':mtimes}, f, -1) f.write(data)
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()