bzrtime – a Pyblosxom plugin

As any good computer geek I'm a small control freak. While I like the basic feature sets of Pyblosxom I didn't feel comfortable with its reliance on file timestamps to determine posting dates and order. In theory a good idea but in practice this is too brittle.

I like having my files under revision control, and for this blog/webpage they are all in Bazaar NG. I thought it would be a good idea to simply extract the timestamp for file comit of the latest revision from Bazaar instead of the filesystem.

To me this is an ideal solution, especially since Bazaar provides a nice (however not completely documented) Python API to access the Bazaar working tree and repository. Below follows the source code for the plugin.

# -*- encoding: utf-8 -*- """ This plugin assumes every posting in the blog has been checked into a bzr repository. It will use the checkin timestamp of the first file revision as the mtime for the story instead of the one from the file system. """ __author__ = u"Øyvind Grønnesby" __version___ = "version 0.1 2008-05-11" __url__ = "http://web.oyving.org/code/bzrtime" __description__ = "Fetches information about posts from bzr" try: import bzrlib import bzrlib.workingtree import stat except ImportError, e: import sys print "You do not have bzrlib in your Python path." sys.exit(1) def verify_installation(request): "Should check if file is acutally in bzr." return 1 def cb_filestat(args): filename = args['filename'] # create a bzr working tree and fetch information about the revisions # of the file. there has to be a better way than what we're doing now, # though. but the docs aren't that expansive... wt, relpath = bzrlib.workingtree.WorkingTree.open_containing(filename) relid = wt.path2id(relpath) revision_ids = set(filter( lambda x: x != "current:", map(lambda x: x[0], wt.annotate_iter(relid)))) if not revision_ids: return args revisions = wt.branch.repository.get_revisions(revision_ids) revisions.sort(lambda x,y: cmp(x.timestamp, y.timestamp)) mtime = list(args['mtime']) mtime[stat.ST_MTIME] = revisions[0].timestamp args['mtime'] = tuple(mtime) return args

What this does is scan each and every line of the post and fetch its revision ID. Then a set of unique revision IDs is created and sorted after their timestamp. This strikes me as rather inefficient; so if there is a better way to extract this information with the Bazaar API, I would be happy to hear from you.

2009-03-13 22:30 | code | link