From: Frank Brehm Date: Thu, 11 Apr 2024 14:07:10 +0000 (+0200) Subject: Sorting given logfiles in lib/pp_admintools/app/get_from_addr.py X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=ba07f297d7af872a5e11828fdc310e5d3b923e8d;p=pixelpark%2Fpp-admin-tools.git Sorting given logfiles in lib/pp_admintools/app/get_from_addr.py --- diff --git a/lib/pp_admintools/app/get_from_addr.py b/lib/pp_admintools/app/get_from_addr.py index dc294e6..0205c64 100644 --- a/lib/pp_admintools/app/get_from_addr.py +++ b/lib/pp_admintools/app/get_from_addr.py @@ -11,6 +11,7 @@ from __future__ import absolute_import # Standard modules import ipaddress import logging +import re from pathlib import Path # Third party modules @@ -21,7 +22,7 @@ from . import BaseDPXApplication from ..handler.pflogparse import PostfixLogfileParser from ..xlate import XLATOR -__version__ = '0.2.1' +__version__ = '0.2.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -116,37 +117,71 @@ class GetFromAddressesApp(BaseDPXApplication): self.show_address_results = True if self.args.logfiles: - for lfile in self.args.logfiles: - logfile = Path(lfile) - if logfile.exists(): - if logfile.is_file(): - if logfile not in self.logfiles: - self.logfiles.append(logfile) - else: - LOG.warn(_('File {!r} is not a regular file.').format(lfile)) - else: - log_dir = logfile.parent - if not log_dir.exists(): - LOG.warn(_('Directory {!r} does not exists.').format(str(log_dir))) - elif not log_dir.is_dir(): - LOG.warn(_('Path {!r} is not a directory.').format(str(log_dir))) - else: - found_files = log_dir.glob(logfile.name) - for f in found_files: - if f.is_file(): - if f not in self.logfiles: - self.logfiles.append(f) - else: - LOG.warn(_('File {!r} is not a regular file.').format(str(f))) - + self._eval_given_logfiles() else: - found_files = self.logdir.glob(self.logfile_pattern) - for f in found_files: - if f.is_file(): - if f not in self.logfiles: - self.logfiles.append(f) + self._eval_default_logfiles() + self._sort_sogfiles() + + # ------------------------------------------------------------------------- + def _eval_given_logfiles(self): + + for lfile in self.args.logfiles: + logfile = Path(lfile) + if logfile.exists(): + if logfile.is_file(): + if logfile not in self.logfiles: + self.logfiles.append(logfile) else: - LOG.warn(_('File {!r} is not a regular file.').format(str(f))) + LOG.warn(_('File {!r} is not a regular file.').format(lfile)) + else: + log_dir = logfile.parent + if not log_dir.exists(): + LOG.warn(_('Directory {!r} does not exists.').format(str(log_dir))) + elif not log_dir.is_dir(): + LOG.warn(_('Path {!r} is not a directory.').format(str(log_dir))) + else: + found_files = log_dir.glob(logfile.name) + for f in found_files: + if f.is_file(): + if f not in self.logfiles: + self.logfiles.append(f) + else: + LOG.warn(_('File {!r} is not a regular file.').format(str(f))) + + # ------------------------------------------------------------------------- + def _eval_default_logfiles(self): + + found_files = self.logdir.glob(self.logfile_pattern) + for f in found_files: + if f.is_file(): + if f not in self.logfiles: + self.logfiles.append(f) + else: + LOG.warn(_('File {!r} is not a regular file.').format(str(f))) + + # ------------------------------------------------------------------------- + def _sort_sogfiles(self): + + if self.verbose > 1: + LOG.debug(_('Sorting logfiles ...')) + sorted_logfiles = [] + logfiles_left = [] + + re_dateext = re.compile(r'log-20', re.IGNORECASE) + + for logfile in self.logfiles: + if re_dateext.search(logfile.name): + sorted_logfiles.append(str(logfile)) + else: + logfiles_left.append(logfile) + + sorted_logfiles.sort(key=str.lower) + self.logfiles = [] + for logfile in sorted_logfiles: + self.logfiles.append(Path(logfile)) + + for logfile in logfiles_left: + self.logfiles.append(logfile) # ------------------------------------------------------------------------- def _run(self):