"""Abstract parent class of guds modules"""
from abc import abstractmethod
def __init__(self, dry_run: bool, logger: logging.Logger):
def run_module(self, homedir: str) -> dict:
raise NotImplementedError
def calc_bytes(cls, size_check: Path) -> int:
return size_check.stat().st_size
for root, _, files in walk(size_check):
filename = Path(root, name)
total_size += filename.stat().st_size
def get_size(cls, size_check: Path) -> str:
"""Find the size of provided directory or file"""
total_size = cls.calc_bytes(size_check)
for suffix in ['B', 'KiB', 'MiB', 'GiB', 'TiB']:
return f"{total_size:3.1f} {suffix}"
return f"{total_size:3.1f} PiB"
"""returns each line of output from the command as an item in a list"""
# ensure maxdepth is set first, if specified
cmd.extend(['-maxdepth', str(maxdepth)])
# optimization: prune and exclude ~/mail
cmd.extend(['-not', '(', '-path', f"{homedir}/mail", '-prune', ')'])
# regextype must be set before regex
cmd.extend(['-regextype', 'posix-extended', '-regex', regex])
for key, val in kwargs.items():
cmd.extend([f'-{key}', str(val)])
self.logger.debug(shlex.join(cmd))
cmd, stdout=subprocess.PIPE, encoding='utf-8', check=False
return [Path(x) for x in ret.stdout.split('\0') if x]
def delete_items(self, backuplist: list[Path]):
for backup in backuplist:
self.logger.info("Removing %s (%s)", backup, self.get_size(backup))
shutil.rmtree(str(backup))