Browse Source

Made a cache_manager class since globals got gross

master
Raphael Roberts 5 years ago
parent
commit
9b0f1e62c5
  1. 19
      tibi_hardlinks/__init__.py
  2. 6
      tibi_hardlinks/backup_parser.py
  3. 2
      tibi_hardlinks/backups.py
  4. 26
      tibi_hardlinks/cache.py

19
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)

6
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

2
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)

26
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()
Loading…
Cancel
Save