gittime – a Pyblosxom plugin

Not much happening here. Since I moved my code away from Bazaar to git I haven't really taken the time to update these pages, or really publish new stuff. One of the reasons why this is is the bzrtime plugin I created.

I finally took the time to write a gittime plugin, that does the same job. It will look up the revision history of a given file and check its creation date.

import sys import subprocess import StringIO import datetime import re import stat # how to split Date: Fri ... from each other field_split = re.compile(r":\s*") def verify_installation(request): "Should check if the file is under git control" return 1 def cb_filestat(args): """Looks up the creation time of a file in git, and uses that for the publishing time in pyblosxom.""" filename = args['filename'] git = subprocess.Popen(["git", "log", "--date=raw", "--", filename], stdout=subprocess.PIPE) content = git.stdout.readlines() dates = [] for line in filter(lambda x: x.startswith("Date:"), content): line = line.strip() line = field_split.split(line, 1) dates.append(int(line[1][:-6])) dates.sort() mtime = list(args['mtime']) mtime[stat.ST_MTIME] = dates[0] args['mtime'] = tuple(mtime) return args

2009-05-16 13:59 | code | link

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

About me

www.flickr.com

Currently working as a software engineer at the Norwegian office for Yahoo!. Not long ago I graduated with a M.Sc. in computer science from NTNU where I wrote my Master's thesis on use of artificial neural networks as a monitoring tool for Linux hosts.

On my spare time I tend to read comic books, read regular books, ride bikes, swimming, and playing role-playing games. There's also quite a lot of time spent reading things on the Internet. In the last few years it has taken over as my main source of news and entertainment.

I also have a nice camera that I don't use enough and that should be used to take more pictures. The photos I find amusing or worthwhile I put up on my flickr page.

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