Compare commits

..

No commits in common. "master" and "v1.4.0" have entirely different histories.

6 changed files with 17 additions and 59 deletions

View File

@ -7,7 +7,7 @@ Suppose you have a third party backup program regularly dropping backup files in
# License
Copyright 2024 Mike Peralta; All rights reserved
Copyright 2023 Mike Peralta; All rights reserved
Releasing to the public under the GNU GENERAL PUBLIC LICENSE v3 (See LICENSE file for more)

View File

@ -31,17 +31,13 @@ class BackupRotator:
config_paths: [Path] = None,
debug: bool = False,
systemd: bool = False,
write_to_syslog: bool = False,
do_test_logs: bool = True,
write_to_syslog: bool = False
):
self.__do_test_logs = do_test_logs
self.__logger = Logger(
name=type(self).__name__,
debug=debug,
systemd=systemd,
write_to_syslog=write_to_syslog,
do_test_logs=do_test_logs,
)
self.__config = Config(
@ -326,12 +322,7 @@ class BackupRotator:
best_ctime = None
for item in items:
try:
ctime = Util.detect_item_creation_date(config, item)
except FileNotFoundError as e:
self.__logger.error(f"File disappeared while trying to check ctime: {item}")
continue
ctime = Util.detect_item_creation_date(config, item)
if best_ctime is None or ctime < best_ctime:
best_ctime = ctime
best_item = item

View File

@ -12,15 +12,13 @@ class Logger:
name: str,
debug: bool = False,
write_to_syslog: bool = False,
systemd: bool = False,
do_test_logs: bool = True,
systemd: bool = False
):
self.__name = name
self.__debug = debug
self.__write_to_syslog = write_to_syslog
self.__systemd = systemd
self.__do_test_logs = do_test_logs
self._init_logger()
@ -75,11 +73,10 @@ class Logger:
self.__logger.addHandler(handler)
# This is annoying inside cron
if self.__do_test_logs:
self.debug("Test debug log")
self.info("Test info log")
self.warn("Test warn log")
self.error("Test error log")
self.debug("Test debug log")
self.info("Test info log")
self.warn("Test warn log")
self.error("Test error log")
def debug(self, s):
self.__logger.debug(s)

View File

@ -42,8 +42,6 @@ class Util:
# print("got mtime")
stat = item.stat().st_birthtime
# print("got btime")
except FileNotFoundError as e:
raise e
except AttributeError:
pass

View File

@ -18,10 +18,6 @@ class ConfigFile:
"file"
]
__DEFAULT_MINIMUM_ITEMS = 0
__DEFAULT_MAXIMUM_ITEMS = None
__DEFAULT_MAXIMUM_AGE = None
def __init__(
self, logger: Logger,
path: Path,
@ -43,9 +39,9 @@ class ConfigFile:
self.__rotatable_paths: [Path] = []
self.__minimum_items = self.__DEFAULT_MINIMUM_ITEMS
self.__minimum_items: int = 0
# noinspection PyTypeChecker
self.__maximum_items: int = self.__DEFAULT_MAXIMUM_ITEMS
self.__maximum_items: int = None
# noinspection PyTypeChecker
self.__maximum_age: int = None
@ -123,11 +119,8 @@ class ConfigFile:
minimum_items = options["minimum-items"]
self.info(f"Found minimum-items option: {minimum_items}")
if minimum_items is None:
minimum_items = self.__DEFAULT_MINIMUM_ITEMS
assert isinstance(minimum_items, int), (
f"Option minimum-items must be an integer,"
f" but got: {type(minimum_items).__name__} ({minimum_items})"
f"Option minimum-items must be int, but got: {minimum_items}"
)
self.__minimum_items = minimum_items
else:
@ -147,10 +140,10 @@ class ConfigFile:
maximum_items = options["maximum-items"]
self.info(f"Found maximum-items option: {maximum_items}")
assert maximum_items is None or isinstance(maximum_items, int), (
f"Option maximum-items must be integer, but got: {maximum_items}"
assert isinstance(maximum_items, int), (
f"Option maximum-items must be int, but got: {maximum_items}"
)
assert maximum_items is None or maximum_items > 0, (
assert maximum_items > 0, (
f"Option maximum-items is zero, which doesn't make sense."
)
self.__maximum_items = maximum_items
@ -163,11 +156,10 @@ class ConfigFile:
maximum_age = options["maximum-age"]
self.info(f"Found maximum-age option (max age in days): {maximum_age}")
assert maximum_age is None or isinstance(maximum_age, int), (
f"Option maximum-age must be None or an integer,"
f" but got: {type(maximum_age).__name__} ({maximum_age})"
assert isinstance(maximum_age, int), (
f"Option maximum-age must be int, but got: {maximum_age}"
)
assert maximum_age is None or maximum_age > 0, (
assert maximum_age > 0, (
f"Option maximum-age is zero, which doesn't make sense."
)
self.__maximum_age = maximum_age

20
main.py
View File

@ -42,25 +42,6 @@ def main():
)
)
parser.add_argument(
"--no-test-logs",
default=False,
dest="do_test_logs",
action="store_false",
help=(
"Pass if you do not want to see test logs for all log levels."
)
)
parser.add_argument(
"--test-logs",
default=True,
dest="do_test_logs",
action="store_true",
help=(
"Pass if you want to see test logs for all log levels."
)
)
parser.add_argument(
"--config", "-c",
dest="config_paths",
@ -87,7 +68,6 @@ def main():
debug=args.debug,
systemd=args.systemd,
write_to_syslog=args.write_to_syslog,
do_test_logs=args.do_test_logs,
)
rotator.run(
global_dry_run=args.global_dry_run