shithub: mkarchive

ref: 4a68f09fa1de6bf554c6a53a3addce194b29fc3e
dir: /mkarchive.py/

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

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("%b-%Y", 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:
            print "already added ", p
            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 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>
              <article><ul>""" % (lst, lst))
        for d in os.listdir(listdir):
            if os.path.isdir(os.path.join(listdir, d)):
                f.write('<li><a href=%s>%s</a></li>' % (d, d))
        f.write("""
              </ul>
             </article>
            <footer>Hey, look at me! I'm a footer!</footer>
            </body></html>""")

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

if __name__ == '__main__':
    main()