]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
Eigentliches Rotieren abgeschlossen (ohne Skriptereien)
authorFrank Brehm <frank@brehm-online.com>
Thu, 30 Jun 2011 16:28:29 +0000 (16:28 +0000)
committerFrank Brehm <frank@brehm-online.com>
Thu, 30 Jun 2011 16:28:29 +0000 (16:28 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@269 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateConfig.py
LogRotateHandler.py
LogRotateStatusFile.py
test/apache2

index f584fcfdc548874383a939f50bb16edd22c60d6c..5d0dbe9578029ec4bb098fc426e6eea7b1b21d82 100755 (executable)
@@ -407,7 +407,7 @@ class LogrotateConfigurationReader(object):
         self.default['period']        = 7
         self.default['dateext']       = False
         self.default['datepattern']   = '%Y-%m-%d'
-        self.default['delaycompress'] = False
+        self.default['delaycompress'] = None
         self.default['extension']     = ""
         self.default['ifempty']       = True
         self.default['mailaddress']   = None
index a020fe44477f9335d2cf75e2816c1ddedfbf7109..924b21cc7947509a98137f0d298c21ca8dc18d22 100755 (executable)
@@ -243,6 +243,8 @@ class LogrotateHandler(object):
         self.files_compress = {}
         '''
         @ivar: dictionary with all files, they have to compress
+               keys are the filenames, values are the index number
+               of the list self.config (for compress options)
         @type: dict
         '''
 
@@ -605,6 +607,9 @@ class LogrotateHandler(object):
                     "\n" + pp.pformat(definition)
             self.logger.debug(msg)
 
+        # re-reading of status file
+        self.state_file.read()
+
         for logfile in definition['files']:
             if self.verbose > 1:
                 msg = ( _("Performing logfile '%s' ...") % (logfile)) + "\n"
@@ -718,8 +723,8 @@ class LogrotateHandler(object):
             file_from = pair['from']
             file_to = pair['to']
             if pair['compressed']:
-                file_from + compress_extension
-                file_to + compress_extension
+                file_from += compress_extension
+                file_to += compress_extension
             msg = _("Moving file '%(from)s' => '%(to)s'.") \
                     % {'from': file_from, 'to': file_to }
             self.logger.info(msg)
@@ -847,16 +852,105 @@ class LogrotateHandler(object):
             for oldfile in files_delete:
                 self.files_delete[oldfile] = True
 
+        # get files to compress save them back in self.files_compress
+        files_compress = self._collect_files_compress(oldfiles, compress_extension, cur_desc_index)
+        if len(files_compress):
+            for oldfile in files_compress:
+                self.files_compress = cur_desc_index
+
+        # write back date of rotation into state file
+        self.state_file.set_rotation_date(logfile)
+        self.state_file.write()
+
         return True
 
+    #------------------------------------------------------------
+    def _collect_files_compress(self, oldfiles, compress_extension, cur_desc_index):
+        '''
+        Collects a list with all old logfiles, they have to compress.
+
+        @param oldfiles: a dict whith all found old logfiles as keys and
+                        their modification time as values
+        @type oldfiles:  dict
+        @param compress_extension: file extension for rotated and
+                                   compressed logfiles
+        @type compress_extension:  str
+        @param cur_desc_index: index of self.config for definition
+                               of logfile from configuration file
+        @type cur_desc_index:  int
+
+        @return: all old (and compressed) logfiles to delete
+        @rtype:  list
+        '''
+
+        definition = self.config[cur_desc_index]
+        _ = self.t.lgettext
+
+        if self.verbose > 2:
+            msg = _("Retrieving logfiles to compress ...")
+            self.logger.debug(msg)
+
+        result = []
+
+        if not definition['compress']:
+            if self.verbose > 3:
+                msg = _("No compression defined.")
+                self.logger.debug(msg)
+            return result
+
+        if not oldfiles.keys():
+            if self.verbose > 3:
+                msg = _("No old logfiles available.")
+                self.logger.debug(msg)
+            return result
+
+        no_compress = definition['delaycompress']
+        if no_compress is None:
+            no_compress = 0
+
+        ce = re.escape(compress_extension)
+        for oldfile in sorted(oldfiles.keys(), key=lambda x: oldfiles[x], reverse=True):
+
+            match = re.search(ce + r'$', oldfile)
+            if match:
+                if self.verbose > 2:
+                    msg = _("File '%s' seems to be compressed, skip it.") % (oldfile)
+                    self.logger.debug(msg)
+                continue
+
+            if oldfile in self.files_delete:
+                if self.verbose > 2:
+                    msg = _("File '%s' will be deleted, compression unnecessary.") % (oldfile)
+                    self.logger.debug(msg)
+                continue
+
+            if no_compress:
+                if self.verbose > 2:
+                    msg = _("Compression of file '%s' will be delayed.") % (oldfile)
+                    self.logger.debug(msg)
+                no_compress -= 1
+                continue
+
+            result.append(oldfile)
+
+        if self.verbose > 3:
+            if len(result):
+                pp = pprint.PrettyPrinter(indent=4)
+                msg = _("Found logfiles to compress:") + "\n" + pp.pformat(result)
+                self.logger.debug(msg)
+            else:
+                msg = _("No old logfiles to compress found.")
+                self.logger.debug(msg)
+        return result
+
     #------------------------------------------------------------
     def _collect_files_delete(self, oldfiles, cur_desc_index):
         '''
         Collects a list with all old (and compressed) logfiles, they have to delete.
 
-        @param oldfile: a dict whith all found old logfiles as keys and
+        @param oldfiles: a dict whith all found old logfiles as keys and
                         their modification time as values
-        @type oldfile:  dict
+        @type oldfiles:  dict
         @param cur_desc_index: index of self.config for definition
                                of logfile from configuration file
         @type cur_desc_index:  int
@@ -1079,6 +1173,8 @@ class LogrotateHandler(object):
                 self.logger.debug(msg)
             found_files = glob.glob(pattern) 
             for oldfile in found_files:
+                if os.path.samefile(oldfile, logfile):
+                    continue
                 statinfo = os.stat(oldfile)
                 result[oldfile] = statinfo.st_mtime
 
index 43132083c2776f1f3848f00ffe20bedcbd7d60a4..782a9a4468424220ded48a09a73dc4a0ec4c6162 100755 (executable)
@@ -209,7 +209,7 @@ class LogrotateStatusFile(object):
             self.logger.addHandler(ch)
 
         # Initial read and check for permissions
-        self._read(must_exists = False)
+        self.read(must_exists = False)
         self._check_permissions()
 
     #-------------------------------------------------------
@@ -265,7 +265,7 @@ class LogrotateStatusFile(object):
         '''
 
         if not self.was_read:
-            self._read(must_exists = False)
+            self.read(must_exists = False)
 
         rotate_date = datetime.min.replace(tzinfo=utc)
         if logfile in self.file_state:
@@ -297,7 +297,7 @@ class LogrotateStatusFile(object):
                 % {'file': logfile, 'date': date_utc.isoformat(' ') }
         self.logger.debug(msg)
 
-        #self._read(must_exists = False)
+        #self.read(must_exists = False)
         self.file_state[logfile] = date_utc
         self.has_changed = True
 
@@ -457,7 +457,7 @@ class LogrotateStatusFile(object):
         return True
 
     #-------------------------------------------------------
-    def _read(self, must_exists = True):
+    def read(self, must_exists = True):
         '''
         Reads the status file and put the results in the dict self.file_state.
         Puts back the absolute path of the status file in self.file_name on success.
index 5cced1a418abaf0aea7cf45bf6d977dedd55c974..656af09ed9034d3700e992fa3575425b204d7800 100644 (file)
@@ -30,12 +30,12 @@ endscript
     nomissingok
     notifempty
     sharedscripts
-    #rotate 20
-    rotate 2
+    rotate 20
     nodateext
+    delaycompress
     #start 1
     daily
-    maxage 10d
+    maxage 1y
     mail    test@uhu-banane.de
     noolddir
     postrotate apache_restart