2019-08-30 16:14:18 -07:00
|
|
|
#!/usr/bin/env python3
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2019-03-13 03:13:23 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
Mike's Backup Rotator
|
|
|
|
|
|
|
|
A simple script to help automatically rotate backup files
|
|
|
|
|
2024-06-09 06:43:02 -07:00
|
|
|
Copyright 2024 Mike Peralta; All rights reserved
|
2019-03-13 03:13:23 -07:00
|
|
|
|
2023-03-27 18:15:11 -07:00
|
|
|
Releasing to the public under the GNU GENERAL PUBLIC LICENSE v3 (See LICENSE file for more)
|
2019-03-13 03:13:23 -07:00
|
|
|
|
|
|
|
"""
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
|
|
|
|
from domain.Logger import Logger
|
2024-06-09 06:43:02 -07:00
|
|
|
from domain.config.Config import Config
|
|
|
|
from domain.Util import Util
|
2023-03-27 19:40:52 -07:00
|
|
|
|
|
|
|
|
2019-02-27 15:50:56 -08:00
|
|
|
import datetime
|
2024-06-09 07:50:34 -07:00
|
|
|
from pathlib import Path
|
2019-02-27 16:51:43 -08:00
|
|
|
import shutil
|
2019-02-27 15:50:56 -08:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2019-02-27 15:10:35 -08:00
|
|
|
class BackupRotator:
|
|
|
|
|
2024-06-09 06:51:51 -07:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
debug: bool = False,
|
|
|
|
systemd: bool = False,
|
|
|
|
write_to_syslog: bool = False
|
|
|
|
):
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2024-06-09 06:51:51 -07:00
|
|
|
self.__logger = Logger(
|
|
|
|
name=type(self).__name__,
|
|
|
|
debug=debug,
|
|
|
|
systemd=systemd,
|
|
|
|
write_to_syslog=write_to_syslog,
|
|
|
|
)
|
|
|
|
self.__config_helper = Config(
|
|
|
|
logger=self.__logger
|
|
|
|
)
|
2023-03-27 19:40:52 -07:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
self.__dry_run = False
|
|
|
|
self.__configs = []
|
|
|
|
self.__config_paths = []
|
|
|
|
self.__calculated_actions = []
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2022-01-31 12:35:08 -08:00
|
|
|
def run(self, configs, dry_run: bool = False):
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
self.info("Begin")
|
2022-01-31 12:35:08 -08:00
|
|
|
|
|
|
|
self.__dry_run = dry_run
|
|
|
|
self.__config_paths = configs
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self._consume_configs(self.__config_paths)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
# Rotate once per config
|
|
|
|
for config_index in range(len(self.__configs)):
|
|
|
|
|
|
|
|
#
|
|
|
|
config = self.__configs[config_index]
|
|
|
|
|
|
|
|
#
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Rotating for config {config_index + 1} of {len(self.__configs)} : {config['__path']}")
|
2022-01-31 14:35:48 -08:00
|
|
|
self._do_rotate(config)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def current_time():
|
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
now_s = now.strftime("%b-%d-%Y %I:%M%p")
|
|
|
|
return str(now_s)
|
|
|
|
|
2023-03-27 19:59:45 -07:00
|
|
|
def debug(self, s):
|
|
|
|
self.__logger.debug(s)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def info(self, s):
|
|
|
|
self.__logger.info(s)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def warn(self, s):
|
2024-06-09 07:50:34 -07:00
|
|
|
self.__logger.warning(s)
|
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def error(self, s):
|
|
|
|
self.__logger.error(s)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _consume_configs(self, paths: [Path] = None):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2023-03-27 19:59:45 -07:00
|
|
|
configs = self.__config_helper.gather_valid_configs(paths=paths)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
for config in configs:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
self._consume_config(path=config)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _consume_config(self, path: Path):
|
|
|
|
|
|
|
|
self.debug(f"Consuming config: {path}")
|
|
|
|
assert path.is_file(), (
|
|
|
|
f"Cannot consume config file because it isn't a file: {path}"
|
|
|
|
)
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
# Open the file
|
2024-06-09 07:50:34 -07:00
|
|
|
self.debug(f"Opening config file for consumption: {path}")
|
|
|
|
f = open(str(path))
|
2019-03-13 01:35:34 -07:00
|
|
|
if not f:
|
2024-06-09 07:50:34 -07:00
|
|
|
raise Exception(f"Unable to open config file: {path}")
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
# Parse
|
2024-06-09 07:50:34 -07:00
|
|
|
config_raw = yaml.safe_load(f)
|
|
|
|
assert config_raw is not None, (
|
|
|
|
f"Config file seems to be null or empty: {path}"
|
|
|
|
)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
# Add its own path
|
2024-06-09 07:50:34 -07:00
|
|
|
config_raw["__path"] = path
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
# Consume to internal
|
2024-06-09 07:50:34 -07:00
|
|
|
self.__configs.append(config_raw)
|
2023-05-29 13:25:03 -07:00
|
|
|
self.info(f"Consumed config from path: {path}")
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _do_rotate(self, config):
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self._rotate_paths(config)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _rotate_paths(self, config):
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
self.info("Begin rotating " + str(len(config["paths"])) + " paths")
|
2019-03-13 01:35:34 -07:00
|
|
|
for path in config["paths"]:
|
2022-01-31 14:35:48 -08:00
|
|
|
self._rotate_path(config, path)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _rotate_path(self, config, path: Path):
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
assert path.is_dir(), (
|
|
|
|
f"Path should be a directory: {path}"
|
|
|
|
)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Rotating path: {path}"
|
|
|
|
)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
found_any_rotation_keys = False
|
|
|
|
if "maximum-items" in config.keys():
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
found_any_rotation_keys = True
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
self._rotate_path_for_maximum_items(
|
|
|
|
config=config,
|
|
|
|
path=path,
|
|
|
|
max_items=config["maximum-items"]
|
|
|
|
)
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
if "maximum-age" in config.keys():
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
found_any_rotation_keys = True
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
self._rotate_path_for_maximum_age(
|
|
|
|
config=config,
|
|
|
|
path=path,
|
|
|
|
max_age_days=config["maximum-age"]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert found_any_rotation_keys is True, (
|
2022-01-31 14:35:48 -08:00
|
|
|
"Config needs one of the following keys: \"maximum-items\""
|
2024-06-09 07:50:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
def _rotate_path_for_maximum_items(self, config, path: Path, max_items: int):
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
assert path.is_dir(), f"Path should be a directory: {path}"
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(f"Rotating path for a maximum of {max_items} items: {path}")
|
2022-01-31 14:35:48 -08:00
|
|
|
|
|
|
|
children = self._gather_rotation_candidates(config, path)
|
2023-03-27 19:59:45 -07:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
minimum_items = self._determine_minimum_items(config)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
# Do we need to rotate anything out?
|
2022-01-31 17:38:30 -08:00
|
|
|
if len(children) < minimum_items:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Path only has {len(children)} items"
|
|
|
|
f", which does not meet the minimum threshold of {minimum_items} items."
|
|
|
|
" Won't rotate this path."
|
|
|
|
)
|
2023-03-26 03:38:27 -07:00
|
|
|
return
|
2022-01-31 17:38:30 -08:00
|
|
|
elif len(children) <= max_items:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Path only has {len(children)} items"
|
|
|
|
f", but needs more than {max_items} for rotation"
|
|
|
|
"; Won't rotate this path."
|
|
|
|
)
|
2019-02-27 16:25:10 -08:00
|
|
|
return
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
self.info(f"Found {len(children)} items to examine")
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
#
|
2022-01-31 17:38:30 -08:00
|
|
|
maximum_purge_count = len(children) - minimum_items
|
2019-02-27 16:25:10 -08:00
|
|
|
purge_count = len(children) - max_items
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(f"Want to purge {purge_count} items")
|
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
if purge_count > maximum_purge_count:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Reducing purge count from"
|
|
|
|
f" {purge_count} to {maximum_purge_count} items"
|
|
|
|
f" to respect minimum items setting ({minimum_items})"
|
|
|
|
)
|
2022-01-31 17:38:30 -08:00
|
|
|
purge_count = maximum_purge_count
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
children_to_purge = []
|
2019-02-27 16:25:10 -08:00
|
|
|
for purge_index in range(purge_count):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
#
|
2024-06-09 07:50:34 -07:00
|
|
|
item_to_purge, item_ctime, item_age_seconds, item_age = self._pick_oldest_item(
|
|
|
|
config, children
|
|
|
|
)
|
|
|
|
item_to_purge: Path
|
|
|
|
|
2019-02-27 16:25:10 -08:00
|
|
|
children.remove(item_to_purge)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
self.info(
|
|
|
|
f"Found next item to purge: ({purge_index + 1})"
|
|
|
|
f" {item_to_purge.name}"
|
|
|
|
f" ({item_age})"
|
|
|
|
)
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
#
|
2022-01-31 17:38:30 -08:00
|
|
|
children_to_purge.append(item_to_purge)
|
|
|
|
|
|
|
|
#
|
2023-03-27 19:40:52 -07:00
|
|
|
self.info("Removing items")
|
2022-01-31 17:38:30 -08:00
|
|
|
for child_to_purge in children_to_purge:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
child_to_purge: Path
|
|
|
|
|
|
|
|
self.debug(f"Purging item: {child_to_purge.name}")
|
|
|
|
|
2022-02-13 02:36:14 -08:00
|
|
|
self._remove_item(config, child_to_purge)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _rotate_path_for_maximum_age(self, config, path: Path, max_age_days: int):
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
assert path.is_dir(), f"Path should be a directory: {path}"
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Rotating path for max age of {max_age_days} days: {path}"
|
|
|
|
)
|
2022-01-31 14:35:48 -08:00
|
|
|
|
|
|
|
children = self._gather_rotation_candidates(config, path)
|
2023-03-26 03:38:27 -07:00
|
|
|
minimum_items = self._determine_minimum_items(config)
|
|
|
|
|
|
|
|
# Do we need to rotate anything out?
|
|
|
|
if len(children) < minimum_items:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Path only has {len(children)} items"
|
|
|
|
f", which does not meet the minimum threshold of {minimum_items} items."
|
|
|
|
f" Won't rotate this path."
|
|
|
|
)
|
2023-03-26 03:38:27 -07:00
|
|
|
return
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"Examining {len(children)} items for deletion"
|
|
|
|
)
|
2022-01-31 14:35:48 -08:00
|
|
|
children_to_delete = []
|
|
|
|
for child in children:
|
|
|
|
|
|
|
|
age_seconds = self._detect_item_age_seconds(config, child)
|
|
|
|
age_days = self._detect_item_age_days(config, child)
|
2024-06-09 06:43:02 -07:00
|
|
|
age_formatted = Util.seconds_to_time_string(age_seconds)
|
2022-01-31 14:35:48 -08:00
|
|
|
|
|
|
|
if age_days > max_age_days:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"[Old enough ] {child.name} ({age_formatted})"
|
|
|
|
)
|
2022-01-31 14:35:48 -08:00
|
|
|
children_to_delete.append(child)
|
|
|
|
else:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
f"[Not Old enough] {child.name} ({age_formatted})"
|
|
|
|
)
|
2023-03-27 20:27:35 -07:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
if len(children_to_delete) > 0:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
self.info("Removing old items ...")
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
for child_to_delete in children_to_delete:
|
|
|
|
self._remove_item(config, child_to_delete)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
else:
|
2023-03-27 19:40:52 -07:00
|
|
|
self.info("No old items to remove")
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
def _gather_rotation_candidates(self, config, path: Path):
|
2023-03-27 20:27:35 -07:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
self.debug(f"Begin gathering rotation candidates for: {path}")
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
candidates: [Path] = []
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
if "target-type" not in config.keys():
|
2019-02-27 16:25:10 -08:00
|
|
|
raise Exception("Please provide the configuration key: target-type")
|
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
for item_name in path.iterdir():
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
item_path = path / item_name
|
|
|
|
self.debug(f"Found an item: {item_name} ==> {item_path}")
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
if config["target-type"] == "file":
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
if not item_path.is_file():
|
|
|
|
self.debug(f"Not a file; Skipping: {item_name}")
|
2019-02-27 16:25:10 -08:00
|
|
|
continue
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
elif config["target-type"] == "directory":
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
if not item_path.is_dir():
|
|
|
|
self.debug(f"Not a directory; Skipping: {item_name}")
|
2019-02-27 16:25:10 -08:00
|
|
|
continue
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2019-02-27 16:25:10 -08:00
|
|
|
else:
|
2024-06-09 07:50:34 -07:00
|
|
|
raise Exception(
|
|
|
|
"Configuration key \"target-type\" must be \"file\" or \"directory\""
|
|
|
|
)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
candidates.append(item_path)
|
|
|
|
|
|
|
|
return candidates
|
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _pick_oldest_item(self, config, items) -> (Path, float, float, str):
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
best_item = None
|
2019-02-27 16:47:10 -08:00
|
|
|
best_ctime = None
|
2019-02-27 16:25:10 -08:00
|
|
|
for item in items:
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
ctime = self._detect_item_date(config, item)
|
|
|
|
if best_ctime is None or ctime < best_ctime:
|
|
|
|
best_ctime = ctime
|
|
|
|
best_item = item
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
age_seconds = self._detect_item_age_seconds(config, best_item)
|
2024-06-09 06:43:02 -07:00
|
|
|
age_string = Util.seconds_to_time_string(age_seconds)
|
2022-01-31 17:38:30 -08:00
|
|
|
|
|
|
|
return best_item, best_ctime, age_seconds, age_string
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
@staticmethod
|
2024-06-09 07:50:34 -07:00
|
|
|
def _detect_item_date(config, item: Path) -> datetime.datetime:
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
assert "date-detection" in config.keys(), (
|
|
|
|
"Please provide config key: \"date-detection\""
|
|
|
|
)
|
2022-01-31 14:35:48 -08:00
|
|
|
detection = config["date-detection"]
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
if detection == "file":
|
2024-06-09 07:50:34 -07:00
|
|
|
ctime = datetime.datetime.fromtimestamp(
|
|
|
|
item.stat().st_ctime, tz=datetime.timezone.utc
|
|
|
|
)
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
else:
|
2024-06-09 07:50:34 -07:00
|
|
|
raise AssertionError(
|
|
|
|
f"Invalid value for \"date-detection\""
|
|
|
|
"; Should be one of [file]: {detection}"
|
|
|
|
)
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
return ctime
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
def _detect_item_age_seconds(self, config, item: Path) -> float:
|
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
2022-01-31 14:35:48 -08:00
|
|
|
|
|
|
|
ctime = self._detect_item_date(config, item)
|
2024-06-09 07:50:34 -07:00
|
|
|
delta = now - ctime.now()
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
return delta.seconds
|
|
|
|
|
|
|
|
def _detect_item_age_days(self, config, item: Path) -> int:
|
2022-01-31 14:35:48 -08:00
|
|
|
|
|
|
|
age_seconds = self._detect_item_age_seconds(config, item)
|
|
|
|
age_days = int(age_seconds / 86400)
|
|
|
|
|
|
|
|
return age_days
|
2024-06-09 06:43:02 -07:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _remove_item(self, config, path: Path):
|
2022-01-31 14:35:48 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
if path.is_file():
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self._remove_file(config, path)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
elif path.is_dir():
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self._remove_directory(config, path)
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
else:
|
2024-06-09 07:50:34 -07:00
|
|
|
raise AssertionError(
|
|
|
|
f"Don't know how to remove this item: {path}"
|
|
|
|
)
|
|
|
|
|
|
|
|
def _remove_file(self, config, file_path: Path):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
if not file_path.is_file():
|
|
|
|
raise Exception(
|
|
|
|
f"Tried to remove a file, but this path isn't a file: {file_path}"
|
|
|
|
)
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
if self.__dry_run:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Won't purge file during global-level dry run: {file_path}")
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
elif "dry-run" in config.keys() and config["dry-run"] is True:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Won't purge file during config-level dry run: {file_path}")
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
else:
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Purging file: {file_path}")
|
2024-06-09 07:50:34 -07:00
|
|
|
file_path.unlink()
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _remove_directory(self, config, dir_path: Path):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
if not dir_path.is_dir():
|
|
|
|
raise Exception(
|
|
|
|
f"Tried to remove a directory"
|
|
|
|
f", but this path isn't a directory: {dir_path}"
|
|
|
|
)
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
if self.__dry_run:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Won't purge directory during global-level dry run: {dir_path}")
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
elif "dry-run" in config.keys() and config["dry-run"] is True:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Won't purge directory during config-level dry run: {dir_path}")
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
else:
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2023-03-27 20:27:35 -07:00
|
|
|
self.info(f"Purging directory: {dir_path}")
|
2019-03-13 01:35:34 -07:00
|
|
|
shutil.rmtree(dir_path)
|
2022-01-31 17:38:30 -08:00
|
|
|
|
2024-06-09 07:50:34 -07:00
|
|
|
def _determine_minimum_items(self, config) -> int:
|
2022-01-31 17:38:30 -08:00
|
|
|
|
|
|
|
minimum_items = 0
|
|
|
|
|
|
|
|
if "minimum-items" in config.keys():
|
2024-06-09 07:50:34 -07:00
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
minimum_items = config["minimum-items"]
|
2024-06-09 07:50:34 -07:00
|
|
|
|
|
|
|
self.info(
|
|
|
|
f"Won't delete anything unless a minimum of {minimum_items} items were found"
|
|
|
|
)
|
|
|
|
|
2022-01-31 17:38:30 -08:00
|
|
|
else:
|
2024-06-09 07:50:34 -07:00
|
|
|
self.info(
|
|
|
|
"No value found for \"minimum-items\""
|
|
|
|
"; Will not enforce minimum item constraint."
|
|
|
|
)
|
2022-01-31 17:38:30 -08:00
|
|
|
|
|
|
|
return minimum_items
|