Atomically write cache files (#674)

This commit is contained in:
Anthony Sottile 2019-01-18 20:59:17 -08:00 committed by Jelle Zijlstra
parent bc7e5c949f
commit 4d3107233f
2 changed files with 9 additions and 5 deletions

View File

@ -14,6 +14,7 @@
import re
import signal
import sys
import tempfile
import tokenize
from typing import (
Any,
@ -3647,11 +3648,11 @@ def write_cache(
"""Update the cache file."""
cache_file = get_cache_file(line_length, mode)
try:
if not CACHE_DIR.exists():
CACHE_DIR.mkdir(parents=True)
CACHE_DIR.mkdir(parents=True, exist_ok=True)
new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
with cache_file.open("wb") as fobj:
pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
with tempfile.NamedTemporaryFile(dir=str(cache_file.parent), delete=False) as f:
pickle.dump(new_cache, f, protocol=pickle.HIGHEST_PROTOCOL)
os.replace(f.name, cache_file)
except OSError:
pass

View File

@ -13,7 +13,9 @@
"""
# Python imports
import os.path
import pickle
import tempfile
# Local imports
from . import token
@ -86,8 +88,9 @@ def __init__(self):
def dump(self, filename):
"""Dump the grammar tables to a pickle file."""
with open(filename, "wb") as f:
with tempfile.NamedTemporaryFile(dir=os.path.dirname(filename), delete=False) as f:
pickle.dump(self.__dict__, f, pickle.HIGHEST_PROTOCOL)
os.replace(f.name, filename)
def load(self, filename):
"""Load the grammar tables from a pickle file."""