#!/usr/bin/env python # encoding: utf-8 import sys, os, time, operator from stat import * from time import * from doghouseglobals import * def main(): # open the index file - truncating idxfile = open(os.path.join(kBlogDir,kBlogIndex), 'w') # collect and sort posts by date in list of (date,filename) tuples # try to use the date from the post; otherwise fall back to the # file modification timestamp. bposts = [] for fn in os.listdir(kBlogDir): if (fn.endswith(kBlogPostSuffix)): fileptr = open(os.path.join(kBlogDir,fn),'r') lastChange = os.lstat(os.path.join(kBlogDir,fn))[ST_MTIME] while 1: line = fileptr.readline() if not isHeader(line): break header = line.split(": ",1) # Date: 2007-02-14 16:15:44 -0400 if (header[0] == "Date"): try: try: tmp = strptime(header[1].rstrip(), "%Y-%m-%d %H:%M") lastChange = mktime(tmp) except ValueError: tmp = strptime(header[1].rstrip(), "%Y-%m-%d %H:%M:%S -0400") lastChange = mktime(tmp) except ValueError: pass fileptr.close() bposts.append( (fn,lastChange) ) bposts.sort(lambda x,y: cmp(y[1],x[1])) # write the posts to the index with their headers for i in range (len(bposts)) : idxfile.write("File-Filename: " + bposts[i][0] + "\n") idxfile.write("File-Modification: " + str(int(bposts[i][1])) + "\n") fileptr = open(os.path.join(kBlogDir,bposts[i][0]),'r') while 1 : line = fileptr.readline() if not isHeader(line) : break idxfile.write(line) fileptr.close() # this is a bit hackish - put two newlines between all # posts but the last. DoghouseIndex class depends on this. if (i < len(bposts)-1) : idxfile.write("\n\n") idxfile.close() if __name__ == '__main__': main()