Bug squashing

This commit is contained in:
mike 2024-06-09 09:41:00 -07:00
parent dbbe3b88af
commit 2f706bacf6
3 changed files with 61 additions and 39 deletions

View File

@ -128,36 +128,29 @@ class BackupRotator:
f"Rotating path: {path}" f"Rotating path: {path}"
) )
found_any_rotation_keys = False self._rotate_path_for_maximum_items(
if config.maximum_items: config=config,
path=path,
)
found_any_rotation_keys = True self._rotate_path_for_maximum_age(
config=config,
self._rotate_path_for_maximum_items( path=path,
config=config,
path=path,
)
if config.maximum_age:
found_any_rotation_keys = True
self._rotate_path_for_maximum_age(
config=config,
path=path,
)
assert found_any_rotation_keys is True, (
"Config needs one of the following keys: \"maximum-items\""
) )
def _rotate_path_for_maximum_items(self, config: ConfigFile, path: Path): def _rotate_path_for_maximum_items(self, config: ConfigFile, path: Path):
assert path.is_dir(), f"Path should be a directory: {path}" assert path.is_dir(), f"Path should be a directory: {path}"
self.info( if config.maximum_items:
f"Rotating path for a maximum of {config.maximum_items} items: {path}" self.info(
) f"Rotating path for a maximum of {config.maximum_items} items: {path}"
)
else:
self.info(
f"Not configured to rotate for maximum number of items."
)
return
candidate_items = self._gather_rotation_candidates(config=config, path=path) candidate_items = self._gather_rotation_candidates(config=config, path=path)
@ -230,9 +223,15 @@ class BackupRotator:
assert path.is_dir(), f"Path should be a directory: {path}" assert path.is_dir(), f"Path should be a directory: {path}"
self.info( if config.maximum_age:
f"Rotating path for max age of {config.maximum_age} days: {path}" self.info(
) f"Rotating path for max age of {config.maximum_age} days: {path}"
)
else:
self.info(
f"Not configured to rotate for a maximum number of days."
)
return
candidate_items = self._gather_rotation_candidates(config=config, path=path) candidate_items = self._gather_rotation_candidates(config=config, path=path)
minimum_items = self._determine_minimum_items(config=config) minimum_items = self._determine_minimum_items(config=config)

View File

@ -15,7 +15,7 @@ class Config:
self.__logger = logger self.__logger = logger
self.__config_files_paths: [Path] = config_files_paths self.__config_files_paths: [Path] = config_files_paths
self.__configs: {} = None self.__configs = {}
self.__scanner = Scanner( self.__scanner = Scanner(
logger=self.__logger logger=self.__logger
@ -23,9 +23,11 @@ class Config:
self._consume_configs() self._consume_configs()
def _consume_configs(self, paths: [Path] = None): def _consume_configs(self):
config_paths = self.__scanner.gather_valid_config_paths(paths=paths) config_paths = self.__scanner.gather_valid_config_paths(
paths=self.__config_files_paths
)
for config_path in config_paths: for config_path in config_paths:

View File

@ -53,19 +53,19 @@ class ConfigFile:
s = "" s = ""
s += "*** Config File ***" s += "*** Config File ***"
s += f"> Path: {self.__path}" s += f"\n> Path: {self.__path}"
s += f"> Dry run: " + ("Yes" if self.__dry_run else "No") s += f"\n> Dry run: " + ("Yes" if self.__dry_run else "No")
s += f"> Minimum items: {self.__minimum_items}" s += f"\n> Minimum items: {self.__minimum_items}"
s += f"> Maximum items: {self.__maximum_items}" s += f"\n> Maximum items: {self.__maximum_items}"
s += f"> Maximum age (in days): {self.__maximum_age}" s += f"\n> Maximum age (in days): {self.__maximum_age}"
s += f"> Target type: {self.__target_type}" s += f"\n> Target type: {self.__target_type}"
s += f"> Date detection: {self.__date_detection}" s += f"\n> Date detection: {self.__date_detection}"
s += f"> Rotatable paths: " s += f"\n> Rotatable paths: "
if len(self.__rotatable_paths) > 0: if len(self.__rotatable_paths) > 0:
for p in self.__rotatable_paths: for p in self.__rotatable_paths:
s += f">> {p}" s += f"\n>> {p}"
else: else:
s += ">> [none]" s += "\n>> [none]"
return s return s
@ -124,6 +124,14 @@ class ConfigFile:
f"No minimum-items option found; Will use default: {self.__minimum_items}" f"No minimum-items option found; Will use default: {self.__minimum_items}"
) )
assert (
"maximum-items" in options.keys()
or
"maximum-age" in options.keys()
), (
"Options should include either maximum-items or maximum-age"
)
if "maximum-items" in options.keys(): if "maximum-items" in options.keys():
maximum_items = options["maximum-items"] maximum_items = options["maximum-items"]
@ -183,6 +191,8 @@ class ConfigFile:
self.warning( self.warning(
f"Option date-detection not found; Will use default: {self.__date_detection}" f"Option date-detection not found; Will use default: {self.__date_detection}"
) )
else:
self.warning(f"No options key found!")
assert "paths" in self.__data, ( assert "paths" in self.__data, (
f"Could not find 'paths' key" f"Could not find 'paths' key"
@ -193,6 +203,17 @@ class ConfigFile:
assert isinstance(rotatable_paths, list), ( assert isinstance(rotatable_paths, list), (
"Rotatable 'paths' key must be a string or list" "Rotatable 'paths' key must be a string or list"
) )
for i in range(len(rotatable_paths)):
p = rotatable_paths[i]
if isinstance(p, Path):
continue
elif isinstance(p, str):
rotatable_paths[i] = Path(p)
else:
raise AssertionError(
f"All rotatable paths must be strings or pathlib::Path objects"
)
self.__rotatable_paths = rotatable_paths self.__rotatable_paths = rotatable_paths
self.info(f"Found {len(self.__rotatable_paths)} rotatable paths") self.info(f"Found {len(self.__rotatable_paths)} rotatable paths")