From a04c19c7605d31298cc17338e9da18c3937692e7 Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Mon, 24 Jul 2017 13:34:35 +0100 Subject: [PATCH] Try clearing the cache iff the summary pickle doesn't load. --- tests/__main___test.py | 5 ++--- vigil/__main__.py | 22 +++++++--------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/tests/__main___test.py b/tests/__main___test.py index a8b3486..0ee8d48 100755 --- a/tests/__main___test.py +++ b/tests/__main___test.py @@ -219,13 +219,12 @@ class MainTestCase(unittest.TestCase): # tmp_total = _tmp_total() foo_path = os.path.join(root_path, "foo") open(foo_path, "w").close() - __main__.manage_cache(root_path) with __main__.chdir(root_path): with contextlib.redirect_stdout(io.StringIO()): __main__.main(root_path, loop, worker_count=2, is_being_tested=True) - for file_name in ["summary.pickle", "creation_time", "log", - "foo-metadata", "foo-contents"]: + for file_name in ["summary.pickle", "log", "foo-metadata", + "foo-contents"]: self.assertTrue(os.path.exists(".vigil/" + file_name)) self.assertEqual(_mount_total(), mount_total) # self.assertEqual(_tmp_total(), tmp_total) diff --git a/vigil/__main__.py b/vigil/__main__.py index 137a662..f754ae1 100755 --- a/vigil/__main__.py +++ b/vigil/__main__.py @@ -916,7 +916,13 @@ def load_state(pickle_path, jobs_added_event, appearance_changed_event, try: with gzip.open(pickle_path, "rb") as file_: screen = pickle.load(file_) - except FileNotFoundError: + except (FileNotFoundError, AttributeError): + cache_path = os.path.join(root_path, tools.CACHE_PATH) + if os.path.exists(cache_path): + print("Vigil has been updated, so clearing the cache and" + " recalculating all results...") + shutil.rmtree(cache_path, ignore_errors=True) + os.mkdir(cache_path) summary = Summary(root_path, jobs_added_event) log = Log(appearance_changed_event) screen = Screen(summary, log, appearance_changed_event, loop) @@ -993,19 +999,6 @@ def chdir(path): os.chdir(old_cwd) -def manage_cache(root_path): - cache_path = os.path.join(root_path, tools.CACHE_PATH) - timestamp_path = os.path.join(cache_path, "creation_time") - if os.path.exists(cache_path) and \ - os.stat(__file__).st_mtime > os.stat(timestamp_path).st_mtime: - print("Vigil has been updated, so clearing the cache and" - " recalculating all results...") - shutil.rmtree(cache_path) - if not os.path.exists(cache_path): - os.mkdir(cache_path) - open(timestamp_path, "w").close() - - def check_arguments(): cmdline_help = __doc__ + USAGE.replace("*", "") arguments = docopt.docopt(cmdline_help, help=False) @@ -1042,7 +1035,6 @@ def check_arguments(): def entry_point(): root_path, worker_count, editor_command, theme = check_arguments() with terminal.console_title("vigil: " + os.path.basename(root_path)): - manage_cache(root_path) with chdir(root_path): # FIX: Don't change directory if possible. loop = asyncio.get_event_loop() main(root_path, loop, worker_count, editor_command, theme)