]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Sorting given logfiles in lib/pp_admintools/app/get_from_addr.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 Apr 2024 14:07:10 +0000 (16:07 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 Apr 2024 14:07:10 +0000 (16:07 +0200)
lib/pp_admintools/app/get_from_addr.py

index dc294e69eadfb20c3f22776f99c173a4c4fc3712..0205c6469cc746db068cb68f2ef3aa0d6f09886f 100644 (file)
@@ -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):