]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
Dateiglobbing auf definierte Logdateien angewendet
authorFrank Brehm <frank@brehm-online.com>
Thu, 23 Jun 2011 15:01:32 +0000 (15:01 +0000)
committerFrank Brehm <frank@brehm-online.com>
Thu, 23 Jun 2011 15:01:32 +0000 (15:01 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@257 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateConfig.py
po/LogRotateConfig.de.po
po/LogRotateConfig.pot

index bd61a0641f55b33ab32b45939d3d8cc0bfb1e2f4..5362b2e25e5206556acd686890ebff6436ef8363 100755 (executable)
@@ -21,6 +21,7 @@ import os
 import os.path
 import pwd
 import grp
+import glob
 
 from LogRotateCommon import split_parts, email_valid, period2days, human2bytes
 
@@ -325,6 +326,12 @@ class LogrotateConfigurationReader(object):
         @type: list
         '''
 
+        self.defined_logfiles = {}
+        '''
+        @ivar: all even defined logfiles after globing of file patterns
+        @type: dict
+        '''
+
         self.logger.debug( _("Logrotate config reader initialised") )
 
     #------------------------------------------------------------
@@ -338,22 +345,39 @@ class LogrotateConfigurationReader(object):
         '''
 
         pp = pprint.PrettyPrinter(indent=4)
-        structure = {
-            'config':          self.config,
-            'config_file':     self.config_file,
-            'config_files':    self.config_files,
-            'config_was_read': self.config_was_read,
-            'default':         self.default,
-            'new_log':         self.new_log,
-            'local_dir':       self.local_dir,
-            'search_path':     self.search_path,
-            'scripts':         self.scripts,
-            'shred_command':   self.shred_command,
-            'taboo':           self.taboo,
-            'verbose':         self.verbose,
-        }
+        structure = self.as_dict()
         return pp.pformat(structure)
 
+    #-------------------------------------------------------
+    def as_dict(self):
+        '''
+        Transforms the elements of the object into a dict
+
+        @return: structure as dict
+        @rtype:  dict
+        '''
+
+        res = {
+            'config':           self.config,
+            'config_file':      self.config_file,
+            'config_files':     self.config_files,
+            'config_was_read':  self.config_was_read,
+            'default':          self.default,
+            'defined_logfiles': self.defined_logfiles,
+            'global_option':    self.global_option,
+            'logger':           self.logger,
+            'local_dir':        self.local_dir,
+            'new_log':          self.new_log,
+            'search_path':      self.search_path,
+            'scripts':          self.scripts,
+            'shred_command':    self.shred_command,
+            't':                self.t,
+            'taboo':            self.taboo,
+            'verbose':          self.verbose,
+        }
+
+        return res
+
     #------------------------------------------------------------
     def _reset_defaults(self):
         '''
@@ -795,7 +819,9 @@ class LogrotateConfigurationReader(object):
                     )
                 if self.verbose > 3:
                     self.logger.debug( ( _("New logfile definition:") + "\n" + pp.pformat(self.new_log)))
-                self.config.append(self.new_log)
+                found_files = self._assign_logfiles()
+                if found_files > 0:
+                    self.config.append(self.new_log)
                 in_fd = False
                 in_logfile_list = False
 
@@ -1807,6 +1833,56 @@ class LogrotateConfigurationReader(object):
         for script_type in script_directives:
             self.new_log[script_type] = None
 
+    #------------------------------------------------------------
+    def _assign_logfiles(self):
+        '''
+        Finds all existing logfiles of self.new_log according to the
+        shell matching patterns in self.new_log['file_patterns'].
+        If a logfile was even defined, a warning is omitted and the
+        new definition will thrown away.
+
+        @return: number of found logfiles according to self.new_log['file_patterns']
+        @rtype:  int
+        '''
+
+        _ = self.t.lgettext
+
+        if len(self.new_log['file_patterns']) <= 0:
+            msg = _("No logfile pattern defined.")
+            self.logger.warning(msg)
+            return 0
+
+        for pattern in self.new_log['file_patterns']:
+            if self.verbose > 1:
+                msg = _("Find all logfiles for shell matching pattern '%s' ...") \
+                       % (pattern)
+                self.logger.debug(msg)
+            logfiles = glob.glob(pattern)
+            if len(logfiles) <= 0:
+                msg = _("No logfile found for pattern '%s'.") % (pattern)
+                if self.new_log['missingok']:
+                    self.logger.debug(msg)
+                else:
+                    self.logger.warning(msg)
+                continue
+            for logfile in logfiles:
+                if self.verbose > 1:
+                    msg = _("Found logfile '%(file)s for pattern '%(pattern)s'.") \
+                           % {'file': logfile, 'pattern': pattern }
+                    self.logger.debug(msg)
+                if logfile in self.defined_logfiles:
+                    msg = _("Logfile '%s' is even defined and so not taken a second time.") \
+                           % (logfile)
+                    self.logger.warning(msg)
+                    continue
+                if self.verbose > 1:
+                    msg = _("Logfile '%s' will taken.") \
+                            % (logfile)
+                self.defined_logfiles[logfile] = True
+                self.new_log['files'].append(logfile)
+
+        return len(self.new_log['files'])
+
 #========================================================================
 
 if __name__ == "__main__":
index 5efa8b8ac8787771ef21be9448a88cc9bfde2658..1d14bd455fdb2a1fd660bb562fb00cc08787d860 100644 (file)
@@ -666,3 +666,33 @@ msgstr "Die Direktive »%(directive)s« ist nicht außerhalb einer Logdateidefin
 #: LogRotateConfig.py:1757
 msgid "Starting a new log directive with default values."
 msgstr "Beginn einer neuen Logdateidefinition mit Vorgabewerten."
+
+#: LogRotateConfig.py:1848
+msgid "No logfile pattern defined."
+msgstr "Keine Logdatei bzw. Dateisuchmuster definiert."
+
+#: LogRotateConfig.py:1854
+#, python-format
+msgid "Find all logfiles for shell matching pattern '%s' ..."
+msgstr "Finde alle Logdateien für das Shell-Suchmuster »%s« ..."
+
+#: LogRotateConfig.py:1862
+#, python-format
+msgid "No logfile found for pattern '%s'."
+msgstr "Keine Logdateien zum Suchmuster »%s« gefunden."
+
+#: LogRotateConfig.py:1870
+#, python-format
+msgid "Found logfile '%(file)s for pattern '%(pattern)s'."
+msgstr "Logdatei »%(file)s« zum Suchmuster »%(pattern)s« gefunden."
+
+#: LogRotateConfig.py:1874
+#, python-format
+msgid "Logfile '%s' is even defined and so not taken a second time."
+msgstr "Die Logdatei »%s« wurde bereits definiert und damit nicht noch einmal berücksichtigt."
+
+#: LogRotateConfig.py:1879
+#, python-format
+msgid "Logfile '%s' will taken."
+msgstr "Die Logdatei »%s« wird berücksichtigt."
+
index fd5fe46f896af5b21571859df9bb4b7c7123e784..950279ef8e1105f8f21e54da4da277c7ecab374c 100644 (file)
@@ -556,3 +556,33 @@ msgstr ""
 #: LogRotateConfig.py:1759
 msgid "Starting a new log directive with default values."
 msgstr ""
+
+#: LogRotateConfig.py:1848
+msgid "No logfile pattern defined."
+msgstr ""
+
+#: LogRotateConfig.py:1854
+#, python-format
+msgid "Find all logfiles for shell matching pattern '%s' ..."
+msgstr ""
+
+#: LogRotateConfig.py:1862
+#, python-format
+msgid "No logfile found for pattern '%s'."
+msgstr ""
+
+#: LogRotateConfig.py:1870
+#, python-format
+msgid "Found logfile '%(file)s for pattern '%(pattern)s'."
+msgstr ""
+
+#: LogRotateConfig.py:1874
+#, python-format
+msgid "Logfile '%s' is even defined and so not taken a second time."
+msgstr ""
+
+#: LogRotateConfig.py:1879
+#, python-format
+msgid "Logfile '%s' will taken."
+msgstr ""
+