shithub: mkarchive

ref: 5778a58b4eba8d99cfcc06d731a35d7358a3a113
dir: /mkarchive.py/

View raw version
#!/usr/bin/env python
import argparse
import os
import errno
import subprocess
import time
import distutils.dir_util
import datetime
from datetime import datetime

p = argparse.ArgumentParser(description = 'Generate a mail archive')
p.add_argument('-o', '--outdir', metavar='outdir', help='generate archive under this directory')
p.add_argument('-m', '--mlmmjdir', metavar='listdir', help='generate archives for this list')
p.add_argument('-M', '--mhonarc', metavar='mhonarc', default='/usr/bin/mhonarc', help='path to mhonarc binary')
p.add_argument('-c', '--configdir', metavar='mhonarc', help='path to mhonarc config dir')
p.add_argument('listname', help='', nargs='?')
opts = p.parse_args()

def mailmonth(mail):
    return time.strftime("%Y-%b", time.gmtime(os.path.getmtime(mail)))

def runmailadd(lst, mail):
    outdir = os.path.join(opts.outdir, lst, mailmonth(mail))
    mhonarc_cmd = [
            opts.mhonarc,
            '-title', lst,
            '-rcfile', os.path.join(opts.configdir, 'mhonarc.rc'),
            '-outdir', outdir,
            '-add'
    ]

    distutils.dir_util.mkpath(outdir)
    print 'runing mhonarc with command %s' % mhonarc_cmd
    mhonarc = subprocess.Popen(mhonarc_cmd, stdin=subprocess.PIPE)
    with open(mail) as f:
        mhonarc.communicate(f.read())
    mhonarc.wait()

def addmail(lst):
    listdir = os.path.join(opts.outdir, lst)
    lastadded = os.path.join(opts.outdir, lst, '.lastindex')
    lastindex = 0
    if os.access(lastadded, os.R_OK):
        with open(lastadded, 'r') as f:
            lastindex = int(f.read())
    maxindex = lastindex
    maildir = os.path.join(opts.mlmmjdir, lst, "archive")
    print maildir
    for p in os.listdir(maildir):
        if int(p) <= lastindex:
            continue
        mailfile = os.path.join(maildir, p)
        runmailadd(lst, mailfile)
        maxindex = max(lastindex, int(p))
    with open(lastadded, 'w') as f:
        f.write(str(maxindex))

def datesorted(dates):
    def datecmp(a, b):
        a = datetime.strptime(a, '%Y-%b')
        b = datetime.strptime(b, '%Y-%b')
        return b.toordinal() - a.toordinal()

    return sorted(dates, cmp=datecmp)

def genindex(lst):
    listdir = os.path.join(opts.outdir, lst)
    with open(os.path.join(listdir, 'index.html'), 'w') as f:
        f.write("""
            <html>
               <head>
                <title>List Archive: %s</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <link rel="stylesheet" href="/style.css" />
               </head>
            <body>
              <header><h1>Eigenstate: %s list archives</h1></header>
		<div id=main>
                <nav>
                  <a href="/">Home</a><br>
                  <ul>
                    <li> <a href="/lists" > Mailing Lists/ </a> </li>
                  </ul>
                </nav>
              <article><ul>""" % (lst, lst))
	dirs = filter(lambda d: os.path.isdir(os.path.join(listdir, d)), os.listdir(listdir))
        for d in datesorted(dirs):
            f.write('<li><a href=%s>%s</a></li>' % (d, d))
        f.write("""
              </ul>
             </article>
	     </div>
            <footer>Hey, look at me! I'm a footer!</footer>
            </body></html>""")

def main():
    addmail('myrddin-dev')
    genindex('myrddin-dev')

if __name__ == '__main__':
    main()