Fix hyphenation dicts being re-extracted unnecessarily

This commit is contained in:
Kovid Goyal 2020-06-26 14:42:35 +05:30
parent 44923e9e45
commit b5ae91b6a6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -53,19 +53,29 @@ def dictionary_name_for_locale(loc):
return lmap[k] return lmap[k]
@lru_cache(maxsize=2)
def expected_hash():
return P('hyphenation/sha1sum', data=True, allow_user_override=False)
def extract_dicts(cache_path): def extract_dicts(cache_path):
dict_tarball = P('hyphenation/dictionaries.tar.xz', allow_user_override=False)
with TemporaryDirectory(dir=cache_path) as tdir: with TemporaryDirectory(dir=cache_path) as tdir:
try: try:
from calibre_lzma.xz import decompress from calibre_lzma.xz import decompress
except ImportError: except ImportError:
tf = tarfile.open(P('hyphenation/dictionaries.tar.xz')) tf = tarfile.open(dict_tarball)
else: else:
buf = BytesIO() buf = BytesIO()
decompress(P('hyphenation/dictionaries.tar.xz', data=True), outfile=buf) with lopen(dict_tarball, 'rb') as f:
data = f.read()
decompress(data, outfile=buf)
buf.seek(0) buf.seek(0)
tf = tarfile.TarFile(fileobj=buf) tf = tarfile.TarFile(fileobj=buf)
with tf: with tf:
tf.extractall(tdir) tf.extractall(tdir)
with open(os.path.join(tdir, 'sha1sum'), 'wb') as f:
f.write(expected_hash())
dest = os.path.join(cache_path, 'f') dest = os.path.join(cache_path, 'f')
with TemporaryDirectory(dir=cache_path) as trash: with TemporaryDirectory(dir=cache_path) as trash:
try: try:
@ -80,12 +90,12 @@ def extract_dicts(cache_path):
def is_cache_up_to_date(cache_path): def is_cache_up_to_date(cache_path):
if getattr(is_cache_up_to_date, 'updated', False): if getattr(is_cache_up_to_date, 'updated', False):
return True return True
hsh = P('hyphenation/sha1sum', data=True, allow_user_override=False)
try: try:
with open(os.path.join(cache_path, 'f', 'sha1sum'), 'rb') as f: with open(os.path.join(cache_path, 'f', 'sha1sum'), 'rb') as f:
if f.read() == hsh: actual_hash = f.read()
is_cache_up_to_date.updated = True if actual_hash == expected_hash():
return True is_cache_up_to_date.updated = True
return True
except EnvironmentError: except EnvironmentError:
pass pass
return False return False