def __init__( self, log_printer, project_dir: str, flush_cache: bool = False): """ Initialize FileCache. :param log_printer: An object to use for logging. :param project_dir: The root directory of the project to be used as a key identifier. :param flush_cache: Flush the cache and rebuild it. """ self.project_dir = project_dir self.current_time = int(time.time()) cache_data = pickle_load(None, project_dir, {}) last_time = -1 if 'time' in cache_data: last_time = cache_data['time'] if not flush_cache and last_time > self.current_time: logging.warning('It seems like you went back in time - your system ' 'time is behind the last recorded run time on this ' 'project. The cache will be force flushed.') flush_cache = True self.data = cache_data.get('files', {}) if flush_cache: self.flush_cache() # store the files to be untracked and then untrack them in the end # so that an untracked file is not tracked again by mistake in a # later section (which will happen if that file doesn't yield a # result in that section). self.to_untrack = set()
def __init__(self, log_printer, project_dir: str, flush_cache: bool = False): """ Initialize FileCache. :param log_printer: An object to use for logging. :param project_dir: The root directory of the project to be used as a key identifier. :param flush_cache: Flush the cache and rebuild it. """ self.project_dir = project_dir self.current_time = int(time.time()) cache_data = pickle_load(None, project_dir, {}) last_time = -1 if 'time' in cache_data: last_time = cache_data['time'] if not flush_cache and last_time > self.current_time: logging.warning( 'It seems like you went back in time - your system ' 'time is behind the last recorded run time on this ' 'project. The cache will be force flushed.') flush_cache = True self.data = cache_data.get('files', {}) if flush_cache: self.flush_cache() # store the files to be untracked and then untrack them in the end # so that an untracked file is not tracked again by mistake in a # later section (which will happen if that file doesn't yield a # result in that section). self.to_untrack = set()
def __init__( self, log_printer: LogPrinter, project_dir: str, flush_cache: bool=False): """ Initialize FileCache. :param log_printer: A LogPrinter object to use for logging. :param project_dir: The root directory of the project to be used as a key identifier. :param flush_cache: Flush the cache and rebuild it. """ self.log_printer = log_printer self.project_dir = project_dir self.current_time = int(time.time()) cache_data = pickle_load(log_printer, project_dir, {}) last_time = -1 if "time" in cache_data: last_time = cache_data["time"] if not flush_cache and last_time > self.current_time: log_printer.warn("It seems like you went back in time - your " "system time is behind the last recorded run " "time on this project. The cache will " "be force flushed.") flush_cache = True self.data = cache_data.get("files", {}) if flush_cache: self.flush_cache()
def __init__(self, log_printer: LogPrinterMixin, project_dir: str, flush_cache: bool = False): """ Initialize FileCache. :param log_printer: An object to use for logging. :param project_dir: The root directory of the project to be used as a key identifier. :param flush_cache: Flush the cache and rebuild it. """ self.log_printer = log_printer self.project_dir = project_dir self.current_time = int(time.time()) cache_data = pickle_load(log_printer, project_dir, {}) last_time = -1 if "time" in cache_data: last_time = cache_data["time"] if not flush_cache and last_time > self.current_time: log_printer.warn("It seems like you went back in time - your " "system time is behind the last recorded run " "time on this project. The cache will " "be force flushed.") flush_cache = True self.data = cache_data.get("files", {}) if flush_cache: self.flush_cache()
def test_corrupt_cache_files(self): file_path = get_data_path(self.log_printer, "corrupt_file") with open(file_path, "wb") as f: f.write(bytes([1] * 100)) self.assertTrue(os.path.isfile(file_path)) self.assertEqual( pickle_load(self.log_printer, "corrupt_file", fallback=42), 42)
def test_corrupt_cache_files(self): file_path = get_data_path(self.log_printer, "corrupt_file") with open(file_path, "wb") as f: f.write(bytes([1] * 100)) self.assertTrue(os.path.isfile(file_path)) self.assertEqual(pickle_load( self.log_printer, "corrupt_file", fallback=42), 42)
def test_time_travel(self): cache = FileCache(self.log_printer, 'coala_test2', flush_cache=True) cache.track_files({'file.c'}) cache.write() self.assertTrue('file.c' in cache.data) cache_data = pickle_load(self.log_printer, 'coala_test2', {}) # Back to the future :) cache_data['time'] = 2000000000 pickle_dump(self.log_printer, 'coala_test2', cache_data) cache = FileCache(self.log_printer, 'coala_test2', flush_cache=False) self.assertFalse('file.c' in cache.data)
def test_time_travel(self): cache = FileCache(self.log_printer, "coala_test2", flush_cache=True) cache.track_files({"file.c"}) cache.write() self.assertTrue("file.c" in cache.data) cache_data = pickle_load(self.log_printer, "coala_test2", {}) # Back to the future :) cache_data["time"] = 2000000000 pickle_dump(self.log_printer, "coala_test2", cache_data) cache = FileCache(self.log_printer, "coala_test2", flush_cache=False) self.assertFalse("file.c" in cache.data)