diff --git a/openwrt_backup/config.py b/openwrt_backup/config.py index 7e3996e..b3f9aba 100644 --- a/openwrt_backup/config.py +++ b/openwrt_backup/config.py @@ -18,6 +18,9 @@ CACHE_DIR = ensure_directory_exists(appdirs.user_cache_dir) CONFIG_FILE_PATH = CONFIG_DIR / "config.toml" PACKAGE_ROOT = Path(__file__).absolute().parent +HOSTNAME = 1 +NUMBER = 0 + if not CONFIG_FILE_PATH.exists(): shutil.copy(PACKAGE_ROOT / "config.toml", CONFIG_FILE_PATH) diff --git a/openwrt_backup/prune.py b/openwrt_backup/prune.py index 2c11236..43ae0c8 100644 --- a/openwrt_backup/prune.py +++ b/openwrt_backup/prune.py @@ -1,10 +1,14 @@ from pathlib import Path +from openwrt_backup.config import HOSTNAME, NUMBER + def get_all_but_last_n(p: Path, n: int, hostname): files = list(p.glob("*.tar.gz")) - hostname_files = list(filter(lambda f: f.name.split("__")[1] == hostname, files)) + hostname_files = list( + filter(lambda f: f.name.split("__")[HOSTNAME] == hostname, files) + ) sorted_files = sorted( - hostname_files, key=lambda f: int(f.name.split("__")[0]), reverse=True + hostname_files, key=lambda f: int(f.name.split("__")[NUMBER]), reverse=True ) return sorted_files[n:] diff --git a/openwrt_backup/utils.py b/openwrt_backup/utils.py index 203de94..ed132d0 100755 --- a/openwrt_backup/utils.py +++ b/openwrt_backup/utils.py @@ -5,6 +5,8 @@ from typing import Union import paramiko +from openwrt_backup.config import HOSTNAME, NUMBER + def client_from_config(name: str, ssh_config_root: Path): """Create a client from a Host entry in the .ssh/config file""" @@ -59,10 +61,10 @@ def get_next_number(backup_path: Path, hostname): f_names = (path.name for path in backup_path.glob("*.tar.gz")) f_names_fields = map(lambda s: s.split("__"), f_names) f_names_fields_filtered = filter( - lambda fields: fields[1] == hostname, f_names_fields + lambda fields: fields[HOSTNAME] == hostname, f_names_fields ) try: - return max(int(fields[0]) for fields in f_names_fields_filtered) + 1 + return max(int(fields[NUMBER]) for fields in f_names_fields_filtered) + 1 except ValueError: return 0