[mhonarc] add comments



commit f1989207087837a7da8aa99c877913d873eb7992
Author: Olav Vitters <olav vitters nl>
Date:   Wed Feb 13 13:43:12 2013 +0100

    add comments

 archive.py |   52 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 30 insertions(+), 22 deletions(-)
---
diff --git a/archive.py b/archive.py
index 448d6ea..7f6325c 100755
--- a/archive.py
+++ b/archive.py
@@ -83,35 +83,39 @@ class Archiver:
         self.end_time = end_time
 
     def output(self, newmsgs, archivepath):
+        """Call mhonarc to archive all messages contained in newmsgs to archivepath"""
         if archivepath is None:
             return False
 
-        path = os.path.join(self.PRIVATE_ARCHIVE_DIR, self.listname, archivepath)
+        try:
+            path = os.path.join(self.PRIVATE_ARCHIVE_DIR, self.listname, archivepath)
 
-        if not os.path.exists(path):
-            os.makedirs(path, 0755)
+            # Ensure path actually exists
+            if not os.path.exists(path):
+                os.makedirs(path, 0755)
 
-        rcfile = self.PRIVATE_RCFILE if self.private else self.PUBLIC_RCFILE
+            rcfile = self.PRIVATE_RCFILE if self.private else self.PUBLIC_RCFILE
 
-        # Call mhonarc for all messages in 'newmsgs'
-        with open(self.ERRORLOG, "a") as error_fd:
-            cmd = ['mhonarc', '-umask', '022', '-rcfile', rcfile, '-add', '-outdir', path, newmsgs.name,
-                              '-definevar', 'ARCHDATE=%s LISTNAME=%s' % (archivepath, self.listname)]
+            # Call mhonarc for all messages in 'newmsgs'
+            with open(self.ERRORLOG, "a") as error_fd:
+                cmd = ['mhonarc', '-umask', '022', '-rcfile', rcfile, '-add', '-outdir', path, newmsgs.name,
+                                  '-definevar', 'ARCHDATE=%s LISTNAME=%s' % (archivepath, self.listname)]
 
-            subprocess.call(cmd, stdout=error_fd, stderr=subprocess.STDOUT)
+                subprocess.call(cmd, stdout=error_fd, stderr=subprocess.STDOUT)
 
-        # Append message(s) to mbox (possibly gzipped)
-        if os.path.exists("%s.txt" % path):
-            archivedmsgs = open("%s.txt" % path, 'ab')
-        else:
-            archivedmsgs = gzip.GzipFile("%s.txt.gz" % path, 'ab')
-
-        newmsgs.seek(0)
-        shutil.copyfileobj(newmsgs, archivedmsgs)
-        archivedmsgs.close()
+            # Append message(s) to mbox (possibly gzipped)
+            if os.path.exists("%s.txt" % path):
+                archivedmsgs = open("%s.txt" % path, 'ab')
+            else:
+                archivedmsgs = gzip.GzipFile("%s.txt.gz" % path, 'ab')
 
-        newmsgs.seek(0)
-        newmsgs.truncate(0)
+            newmsgs.seek(0)
+            shutil.copyfileobj(newmsgs, archivedmsgs)
+            archivedmsgs.close()
+        finally:
+            # Always ensure newmsgs is cleared
+            newmsgs.seek(0)
+            newmsgs.truncate(0)
 
     def determine_received_time(self, fd):
         """Determines the received time of a message"""
@@ -142,7 +146,6 @@ class Archiver:
                 break
 
         if received_time is None:
-            # XXX - do some debug stuff
             if self.debug:
                  print sys.stderr, "Failed to parse time from the received headers!"
             return None
@@ -157,15 +160,20 @@ class Archiver:
         return received_time.strftime("%Y-%B")
 
     def handle_message(self, fd, newmsgs, archivepath):
+        """Process one message
+
+        Called from process_fd"""
         path = self.determine_received_time(fd)
 
         if path == False:
+            # Message should be ignored
             return archivepath
 
+        # If archivepath changes, send the messages to mhonarc
         if archivepath is not None and path is not None and archivepath != path:
             self.output(newmsgs, archivepath)
 
-        # Add message to list of messages
+        # Add new message to list of messages
         fd.seek(0)
         shutil.copyfileobj(fd, newmsgs)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]