[mhonarc] make make_index work, allow for no received headers



commit 96d69e0c3a891ea06319b915c7a48b512144092e
Author: Olav Vitters <olav vitters nl>
Date:   Tue Feb 12 22:37:09 2013 +0100

    make make_index work, allow for no received headers

 archive.py |   50 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 37 insertions(+), 13 deletions(-)
---
diff --git a/archive.py b/archive.py
index f55842a..3fef606 100755
--- a/archive.py
+++ b/archive.py
@@ -11,11 +11,30 @@ import re
 import datetime
 import email.utils
 import tempfile
+import shutil
 try:
         from cStringIO import StringIO
 except ImportError:
         from StringIO import StringIO
 
+def human_size(size):
+    suffixes = [("",2**10), ("K",2**20), ("M",2**30), ("G",2**40), ("T",2**50)]
+
+    for suf, lim in suffixes:
+        if size < lim:
+            break
+
+    sizediv = size/float(lim/2**10)
+    if suf == "":
+        fmt = "%0.0f%s"
+    elif sizediv > 100:
+        fmt = "%0.0f%s"
+    elif sizediv > 10:
+        fmt = "%0.1f%s"
+    else:
+        fmt = "%0.2f%s"
+
+    return fmt % (size/float(lim/2**10), suf)
 
 class Archiver:
     PUBLIC_ARCHIVE_DIR = "/var/lib/mailman/archives/public"
@@ -87,11 +106,11 @@ class Archiver:
         """Determines the received time of a message"""
         msg = email.message_from_file(fd)
         received_texts = msg.get_all('received')
+        if received_texts is None:
+            return None
 
         received_time = None
 
-        print received_texts
-
         # Determine received time
         for text in received_texts:
             if ';' not in text: continue
@@ -108,8 +127,6 @@ class Archiver:
                 received_time_text = text
                 break
 
-        print received_time
-
         if received_time is None:
             # XXX - do some debug stuff
             return None
@@ -183,8 +200,8 @@ class Archiver:
 
 
     @classmethod
-    def make_index(listname, private=False):
-        path = os.path.join(self.PRIVATE_ARCHIVE_DIR, listname)
+    def make_index(cls, listname, private=False):
+        path = os.path.join(cls.PRIVATE_ARCHIVE_DIR, listname)
 
         # XXX - if public add symlink from private to public
 
@@ -198,17 +215,24 @@ class Archiver:
             year = mo.group(1)
             month = mo.group(2).lower()
 
-            if month in self.MONTHS:
-                dirs[a_dir] = (long(year) * 100) + self.MONTHS[month]
+            if month in cls.MONTHS:
+                dirs[a_dir] = (long(year) * 100) + cls.MONTHS[month]
             else:
                 dirs[a_dir] = a_dir
 
-            with open(os.path.join(path, 'index.html', 'w')) as fp:
+            with open(os.path.join(path, 'index.html'), 'w') as fp:
                 for a_dir in sorted(dirs, key=dirs.get):
                     # XXX - write header 
-                    # XXX - determine mbox type
-
-                    fd.write('''
+                    for fmt in ("%s.txt", "%s.txt.gz"):
+                        mboxfile = fmt % a_dir
+                        if os.path.exists(os.path.join(path, mboxfile)):
+                            stat = os.stat(os.path.join(path, mboxfile))
+                            mboxsize = stat.st_size
+                            break
+                    else:
+                        mboxsize = 0
+
+                    fp.write('''
     <tr>
     <td>%s</td>
     <td>
@@ -217,7 +241,7 @@ class Archiver:
       <A href="%s/author.html">Author</a>
     </td>
     <td><A href="%s">%s</a></td>
-    </tr>''' % (a_dir, a_dir, a_dir, a_dir, mboxfile, mboxsize))
+    </tr>''' % (a_dir, a_dir, a_dir, a_dir, mboxfile, human_size(mboxsize)))
 
 
 def mkdate(datestr):


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