csc-industry
[dtbartle/www.git] / industry / archives.cgi
1 #!/usr/bin/env python
2 import os
3 from md5 import md5
4 os.umask(0027)
5
6 ARCHIVE_LIST = 'csc-industry'
7 ARCHIVE_MBOX = '/var/lib/mailman/archives/private/%s.mbox/%s.mbox' % (ARCHIVE_LIST, ARCHIVE_LIST)
8 ARCHIVE_PCK = '/tmp/%s-%s.pck' % (ARCHIVE_LIST, md5(os.path.abspath(__file__)).hexdigest())
9
10 try:
11         mbox_mtime = os.stat(ARCHIVE_MBOX).st_mtime
12 except:
13         print 'Content-Type: text/plain\r\n\r\nUnable to stat archive.'
14         exit()
15
16 try:
17         stat = os.stat(ARCHIVE_PCK)
18         pickle_mtime = stat.st_mtime
19         if stat.st_uid != os.getuid():
20                 pickle_mtime = 0
21 except:
22         pickle_mtime = 0
23
24 import pickle, json
25
26 if pickle_mtime < mbox_mtime:
27         import mailbox
28         mbox = mailbox.UnixMailbox(open(ARCHIVE_MBOX, 'r'))
29         messages = []
30         for msg in mbox:
31                 messages.append({
32                         'subject': msg['subject'],
33                         'date': msg['date'],
34                         'from': msg['from']
35                 })
36         pck = open(ARCHIVE_PCK, 'w')
37         pickle.dump(messages, pck)
38 else:
39         pck = open(ARCHIVE_PCK, 'r')
40         messages = pickle.load(pck)
41
42 print 'Content-Type: application/json\r\n\r\n' + json.dumps(messages[-3:])