From 9b0f1e62c57d99e256b589bca1db5f441cd49945 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Fri, 7 May 2021 23:42:35 -0500 Subject: [PATCH] Made a cache_manager class since globals got gross --- tibi_hardlinks/__init__.py | 19 +++++++++++++++++++ tibi_hardlinks/backup_parser.py | 6 +++--- tibi_hardlinks/backups.py | 2 ++ tibi_hardlinks/cache.py | 26 +++++++++++++++++++++----- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/tibi_hardlinks/__init__.py b/tibi_hardlinks/__init__.py index ab3d419..457529b 100644 --- a/tibi_hardlinks/__init__.py +++ b/tibi_hardlinks/__init__.py @@ -5,6 +5,7 @@ import yaml from tibi_hardlinks.config import CONFIG_DATA, CONFIG_FILE_PATH from tibi_hardlinks.backups import Backup, create_backup_directory_name +from tibi_hardlinks.cache import cache_manager def find_all_property_files(): @@ -25,15 +26,33 @@ def hardlink_all_files(files, dry=False): else: dest_dir.mkdir(parents=True, exist_ok=True) backup.hardlink_to(dest_dir, dry) + already_processed = cache_manager.tibi_cache["already_processed"] + cache_manager.tibi_cache["already_processed"] = already_processed + cache_manager.tibi_cache.sync() def main(): parser = ArgumentParser() parser.add_argument("-d", "--dry", action="store_true", help="Don't do anything") parser.add_argument("--dump-config", action="store_true", help="Show config") + parser.add_argument( + "-c", + "--clear-already-processed", + action="store_true", + help="Clear the cache of property files that were already linked", + ) + parser.add_argument( + "--clear-all", + action="store_true", + help="Clear the cache entirely", + ) args = parser.parse_args() if args.dump_config: print(CONFIG_FILE_PATH) print(yaml.dump(CONFIG_DATA)) exit(0) + if args.clear_all: + cache_manager.invalidate_all() + elif args.clear_already_processed: + cache_manager.invalidate_already_processed() hardlink_all_files(find_all_property_files(), args.dry) diff --git a/tibi_hardlinks/backup_parser.py b/tibi_hardlinks/backup_parser.py index d5aee65..b8f50a1 100644 --- a/tibi_hardlinks/backup_parser.py +++ b/tibi_hardlinks/backup_parser.py @@ -4,7 +4,7 @@ import re from dateutil.parser import parse as datetime_parse, ParserError import pytz -from tibi_hardlinks.cache import tibi_cache +from tibi_hardlinks.cache import cache_manager tzinfos = {name: pytz.timezone(name) for name in pytz.all_timezones} @@ -30,10 +30,10 @@ def parse_property_text(text): def read_backup_properties(property_file: Path): try: - data = tibi_cache[property_file.name] + data = cache_manager.tibi_cache[property_file.name] return data except KeyError: with open(property_file) as backup_properties: data = parse_property_text(backup_properties.read()) - tibi_cache[property_file.name] = data + cache_manager.tibi_cache[property_file.name] = data return data diff --git a/tibi_hardlinks/backups.py b/tibi_hardlinks/backups.py index 382ad61..3864b88 100644 --- a/tibi_hardlinks/backups.py +++ b/tibi_hardlinks/backups.py @@ -3,6 +3,7 @@ import datetime from tibi_hardlinks.backup_parser import read_backup_properties from tibi_hardlinks.exceptions import ConfigurationException +from tibi_hardlinks.cache import cache_manager HEIRACHY = ( "sys_ro.product.model", @@ -76,3 +77,4 @@ class Backup: print(f'link: "{filepath}" => "{dest_dir / filename}"') else: filepath.link_to(dest_dir / filename) + cache_manager.tibi_cache["already_processed"].add(self.property_file) diff --git a/tibi_hardlinks/cache.py b/tibi_hardlinks/cache.py index e6943cb..ed2756e 100644 --- a/tibi_hardlinks/cache.py +++ b/tibi_hardlinks/cache.py @@ -5,10 +5,26 @@ from tibi_hardlinks.config import CACHE_DIR import shelve -tibi_cache = shelve.open(str((Path(CACHE_DIR) / "cache").absolute()), writeback=True) -atexit.register(tibi_cache.close) +CACHE_FILE = (Path(CACHE_DIR) / "cache").absolute() -def invalidate(): - tibi_cache.clear() - tibi_cache.sync() +class CacheManager: + def __init__(self): + self.tibi_cache = None + + def open_cache(self): + self.tibi_cache = shelve.open(str(CACHE_FILE), writeback=True) + atexit.register(self.tibi_cache.close) + + def invalidate_all(self): + self.tibi_cache.close() + CACHE_FILE.unlink() + self.open_cache() + + def invalidate_already_processed(self): + self.tibi_cache["already_processed"] = set() + self.tibi_cache.sync() + + +cache_manager = CacheManager() +cache_manager.open_cache()